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
- File Type:
-(file),d(directory),l(link). - Permissions:
rwxr-xr-x. - Hard Links: Number of hard links.
- Owner: User who owns the file.
- Group: Group who owns the file.
- Size: Size in bytes.
- Timestamp: Last modification time.
- 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 parselsoutput in scripts (filenames can contain newlines). Usefindor shell globs instead. - Aliases:
llis a common alias forls -alF.lais oftenls -A.