>_cmd.script

ls

List directory contents

Files

By CMD Script Team · 3 min read · Last updated

SYNTAX
ls [OPTIONS] [FILE...]

Options

Command options and flags
FlagDescription
-aShow all entries, including hidden files (names starting with a dot)
-lUse long listing format: permissions, owner, size, and modification date
-hWith -l, show sizes in human-readable units (K, M, G)
-RList subdirectories recursively
-tSort by modification time, newest first
-SSort by file size, largest first
-rReverse whatever sort order is in effect

Distribution compatibility

  • Ubuntu
  • Debian
  • Fedora
  • Arch
  • macOS (BSD variant, some flags differ)

What it does

ls lists the contents of a directory: files, subdirectories, and — with the right options — details like permissions, ownership, size, and modification time. Run without arguments, it lists the current working directory; give it one or more paths and it lists each in turn.

Beginner examples

  • ls — list the current directory
  • ls /etc — list a specific directory
  • ls -a — include hidden files (dotfiles)
  • ls -l — long format showing permissions, owner, size, and date
  • ls -lh — long format with human-readable sizes (e.g. 4.2K instead of 4200)
ls -la ~/projects

Advanced examples

  • Sort by modification time, newest first: ls -lt
  • Sort by size, largest first: ls -lS
  • Reverse any sort order: ls -ltr (oldest first)
  • Recursively list subdirectories: ls -R
  • Combine with grep to filter results: ls -la | grep ".conf"
ls -lhSr /var/log

Common mistakes

  • Assuming ls shows hidden files by default — it doesn't; you need -a.
  • Reading raw byte sizes from ls -l for large files instead of adding -h for human-readable units.
  • Expecting ls -R output to be sorted meaningfully across directories — it lists depth-first per directory, not alphabetically across the whole tree.
  • Confusing ls -l's first character (file type: -, d, l, etc.) with a permission bit.

Tips

  • Add --color=auto (or rely on your distro's default alias) to visually distinguish files, directories, and symlinks.
  • Use ls -F to append a marker (/, *, @) to entries indicating their type without needing the full long format.
  • ls -d */ lists only directories in the current path.

Best practices

  • Prefer ls -lh over parsing raw ls output by eye in interactive use.
  • For scripts, avoid parsing ls output entirely — use find or shell globs instead, since filenames can contain characters that break naive ls parsing.
  • When auditing permissions across many files, pair ls -l with grep/awk rather than eyeballing long output.

Try it yourself

A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.

Real-world use cases

  • Quickly checking file permissions before running chmod.
  • Auditing a directory for unexpectedly large files with ls -lhS.
  • Verifying a deployment script wrote the expected files by listing the target directory immediately after.

Common interview questions

  • What's the difference between ls -l and ls -la? -a additionally shows hidden files (names starting with .), which -l alone omits.
  • How would you list files sorted by size? ls -lS (largest first); add -r to reverse it.
  • Why shouldn't you parse ls output in scripts? Filenames can contain spaces, newlines, or special characters that make ls output ambiguous to parse; find -print0 or shell globbing is safer.

Frequently Asked Questions

How do I show hidden files?

Use ls -a. Hidden files are any entry whose name starts with a dot, such as .bashrc.

How do I sort files by size?

Use ls -S to sort largest-first, or ls -Sr to reverse it and sort smallest-first.

How can I see file permissions with ls?

Use ls -l. The first column of each line shows the file type and permission bits, e.g. -rwxr-xr-x.

Cheat sheet

Download a quick-reference cheat sheet for ls.