List Directory Contents

List files and directories in the current or specified directory with various formatting options.

filesystem
navigation
basic
linux

Command

ls

Explanation

`ls` is one of the most commonly used commands in Linux. Without arguments, it lists non-hidden files in the current directory. Flags like `-l` show detailed information, `-a` includes hidden files (those starting with `.`), and `-h` makes file sizes human-readable when used with `-l`.

Common Use Cases

  • View directory contents
  • Inspect file permissions and ownership
  • Display hidden files and directories
  • Check file modification times

Best Practices

  • Use `ls -lh` for human-readable file sizes
  • Combine `-a` and `-l` as `ls -la` for detailed views
  • Pipe output to `less` for viewing long listings

Common Mistakes to Avoid

  • Forgetting to use `-a` to show hidden files
  • Misinterpreting permissions or ownership in `ls -l` output
  • Using `ls` without quotes for paths containing spaces

Troubleshooting

Problem: Output truncated or hard to read

Solution: Pipe output to `less` (e.g., `ls -l | less`) for easier scrolling.

Problem: Permission denied on directory

Solution: Use `sudo ls` if you have administrative privileges.

Examples

List files in current directory

ls

Long listing format with details

ls -l

Show hidden files

ls -a