ls
List directory contents
By CMD Script Team · 3 min read · Last updated
ls [OPTIONS] [FILE...]Options
| Flag | Description |
|---|---|
-a | Show all entries, including hidden files (names starting with a dot) |
-l | Use long listing format: permissions, owner, size, and modification date |
-h | With -l, show sizes in human-readable units (K, M, G) |
-R | List subdirectories recursively |
-t | Sort by modification time, newest first |
-S | Sort by file size, largest first |
-r | Reverse 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 directoryls /etc— list a specific directoryls -a— include hidden files (dotfiles)ls -l— long format showing permissions, owner, size, and datels -lh— long format with human-readable sizes (e.g.4.2Kinstead of4200)
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
grepto filter results:ls -la | grep ".conf"
ls -lhSr /var/log
Common mistakes
- Assuming
lsshows hidden files by default — it doesn't; you need-a. - Reading raw byte sizes from
ls -lfor large files instead of adding-hfor human-readable units. - Expecting
ls -Routput 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 -Fto 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 -lhover parsing rawlsoutput by eye in interactive use. - For scripts, avoid parsing
lsoutput entirely — usefindor shell globs instead, since filenames can contain characters that break naivelsparsing. - When auditing permissions across many files, pair
ls -lwithgrep/awkrather 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 -landls -la?-aadditionally shows hidden files (names starting with.), which-lalone omits. - How would you list files sorted by size?
ls -lS(largest first); add-rto reverse it. - Why shouldn't you parse
lsoutput in scripts? Filenames can contain spaces, newlines, or special characters that makelsoutput ambiguous to parse;find -print0or 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.