ls — List Directory Contents

List directory contents with ls — permissions, owner, size and modification time, plus sorting, filtering and useful flag combinations.

ls lists the contents of directories, which makes it one of the commands you type most often in the terminal. Without options it shows only names, but the flags are what make it genuinely useful: -l gives the long format with permissions, owner, size and modification date, -a reveals hidden files, -h makes sizes readable, and -t or -S sort by time or size respectively. The options shown here refer to GNU ls from coreutils, the default on Linux – BSD/macOS ls lacks some flags or assigns them differently. In practice you simply combine the letters, e.g. ls -lahS, and pipe onward to head, wc or grep.

Basic Usage

ls — List files and directories in the current directory.

ls

ls <dir> — List contents of a specific directory.

ls /var/log

ls <file1> <file2> — List specific files or multiple paths.

ls index.html style.css

ls -l — Long format. Show permissions, owner, size, and modification date.

ls -l

ls -la — Long format including hidden files (dotfiles).

ls -la ~/

ls -lh — Long format with human-readable file sizes (K, M, G).

ls -lh /var/log/

Showing Hidden Files

ls -a — Show all files including hidden (dotfiles), . and ..

ls -a

ls -A — Show hidden files but exclude . and .. entries.

ls -A

ls -d .* — List only hidden files and directories.

ls -d .*

Sorting

ls -t — Sort by modification time, newest first.

ls -lt

ls -tr — Sort by modification time, oldest first (reversed).

ls -ltr

ls -S — Sort by file size, largest first.

ls -lSh

ls -Sr — Sort by file size, smallest first.

ls -lSrh

ls -X — Sort alphabetically by file extension.

ls -lX

ls -v — Natural sort of version numbers within filenames.

ls -v

ls -U — Do not sort. List entries in directory order.

ls -U

ls -r — Reverse the sort order.

ls -lr

Display Formats

ls -1 — List one file per line.

ls -1

ls -m — List files as comma-separated values.

ls -m

ls -C — List in columns (default when output is a terminal).

ls -C

ls -x — List entries by lines instead of columns.

ls -x

ls -Q — Enclose filenames in double quotes.

ls -Q

ls --color=auto — Colorize output. Directories, executables, links get distinct colors.

ls --color=auto

File Information

ls -i — Show inode number of each file.

ls -li

ls -n — Long format with numeric UID and GID instead of names.

ls -ln

ls -o — Long format without group information.

ls -o

ls -g — Long format without owner information.

ls -g

ls --full-time — Long format with full date and time including nanoseconds.

ls --full-time

ls -s — Show allocated block size for each file.

ls -ls

Directories & Recursion

ls -d <dir> — List directory entry itself, not its contents.

ls -ld /var

ls -d */ — List only directories in the current directory.

ls -d */

ls -R — List contents recursively through all subdirectories.

ls -R /etc/nginx

ls -R <dir> | grep ':$' — List only subdirectory names from recursive output.

ls -R /var | grep ':$'

ls -l <link> — Show where a symbolic link points to.

ls -l /usr/bin/python3

ls -L — Show information about the target of a symlink, not the link itself.

ls -lL /usr/bin/python3

ls -F — Append indicator to entries: / directory, @ symlink, * executable.

ls -F

ls -p — Append / indicator to directories only.

ls -p

Filtering with Globs

ls *.txt — List all files with .txt extension.

ls *.txt

ls *.{jpg,png,gif} — List files matching multiple extensions using brace expansion.

ls -lh *.{jpg,png,gif}

ls [A-Z]* — List files starting with an uppercase letter.

ls [A-Z]*

ls -I '<pattern>' — Exclude files matching a pattern from the listing.

ls -I '*.bak' -I '*.tmp'

ls --hide='<pattern>' — Hide files matching pattern (overridden by -a).

ls --hide='*.pyc'

Common Combinations

ls -lahS — All files, long format, human-readable, sorted by size.

ls -lahS

ls -latr — All files, long format, sorted by time (oldest first).

ls -latr

ls -lhS | head -n 10 — Show the 10 largest files in a directory.

ls -lhS | head -n 10

ls -lt | head -n 5 — Show the 5 most recently modified files.

ls -lt | head -n 5

ls -1 | wc -l — Count the number of files in a directory.

ls -1 | wc -l

ls -1 | sort -f — List files sorted case-insensitively.

ls -1 | sort -f

ls -d .* 2>/dev/null — List dotfiles only, suppressing errors if none exist.

ls -ld .* 2>/dev/null

ls -lR | grep '^-' | wc -l — Count all files recursively (excluding directories).

ls -lR | grep '^-' | wc -l

Conclusion

ls is deceptively simple: its real value comes from combining options. For everyday work, ls -lah (everything, readable, with detail), ls -ltr (time-sorted, newest at the bottom) and a pipe to head, wc -l or grep cover most needs. Keep in mind that the flags shown assume GNU coreutils – on macOS or BSD, --full-time, -X or --hide behave differently or are missing entirely; there, gls from coreutils often helps. ls shows ownership and permissions only at a glance – for the full metadata reach for stat, for the real file type reach for file, and to search across whole trees reach for find.

Further Reading

  • stat – full file metadata beyond ls -l
  • file – determine the real file type by content
  • find – search and filter files recursively