Skip to content

ls Command Cheat Sheet

ls lists information about files and directories. It is likely the most frequently used command in the Linux terminal.


Synopsis

ls [OPTION]... [FILE]...

Basic Listings

List Current Directory

ls

List Specific Path

ls /var/log

Show Hidden Files (-a)

Lists files starting with . (dotfiles), including . (current) and .. (parent).

ls -a

Almost All (-A)

Lists hidden files but excludes . and ... cleaner output.

ls -A

Long Listing Format (-l)

The most useful mode for details.

ls -l

Output Breakdown: drwxr-xr-x 2 user group 4096 Jan 1 12:00 Music

  1. File Type: - (file), d (directory), l (link).
  2. Permissions: rwxr-xr-x.
  3. Hard Links: Number of hard links.
  4. Owner: User who owns the file.
  5. Group: Group who owns the file.
  6. Size: Size in bytes.
  7. Timestamp: Last modification time.
  8. Name: Filename.

Human Readable Sizes (-h)

Shows sizes in KB, MB, GB instead of bytes.

ls -lh

Sorting Options

Sort by Time (-t)

Newest files first.

ls -lt

Sort by Size (-S)

Largest files first.

ls -lS

Sort by Extension (-X)

Groups files by extension (e.g., all .jpg together).

ls -lX

Reverse Order (-r)

Reverses any sort. - ls -ltr: Oldest files first (useful for logs). - ls -lSr: Smallest files first.


Display Options

Recursive Listing (-R)

List subdirectories recursively. Warning: Can produce huge output.

ls -R

List Directories Themselves (-d)

By default, ls dir lists the contents of dir. ls -d dir lists the directory entry itself.

ls -ld /etc
# Output: drwxr-xr-x 1 root root ... /etc

Show Inode Numbers (-i)

Useful for identifying hard links.

ls -i

One File Per Line (-1)

Forces output to be a single column (useful for scripts/pipes).

ls -1

Classify Output (-F)

Appends a character to reveal type: - / Directory - * Executable - @ Symlink - = Socket - | FIFO

ls -F

Time Formatting

Full ISO Date

ls -l --time-style=long-iso
# Output: 2024-01-01 12:00

Full Exact Timestamp

ls -l --full-time

Color Output

Most distros alias ls to ls --color=auto.

  • Blue: Directory
  • Green: Executable
  • Cyan: Symlink
  • Red: Archive (.tar, .zip)
  • Magenta: Image/Media

To disable color (e.g., for piping):

ls --color=never

Advanced: Globbing

ls supports shell wildcards (globs).

ls *.jpg        # All jpg files
ls app_?.conf   # app_1.conf, app_A.conf (single char)
ls [A-Z]*       # Files starting with capital letter

Exit Status

Code Meaning
0 Success
1 Minor problems (e.g., cannot access subdirectory)
2 Serious trouble (e.g., command line argument error)

Notes

  • Parsing ls: Do NOT parse ls output in scripts (filenames can contain newlines). Use find or shell globs instead.
  • Aliases: ll is a common alias for ls -alF. la is often ls -A.