DEV Community

Cameroon Reeves
Cameroon Reeves

Posted on • Originally published at commandinline.com

ls Command Cheat Sheet — All Flags & Usage Examples

Originally published at commandinline.com.

The ls command is the first thing every Linux user types and the last one they ever stop tuning. It lists directory contents, but its real value is in the dozens of flags that turn a plain listing into a permissions audit, a size report, or a sort by modification time. Pair it with advanced find searches for filtered hunts and stat for deep file metadata when ls output is not enough.

Quick Reference

ls prints the names of files and directories. Reach for it any time you need to see what is in a path, when a file last changed, who owns it, or how much disk it occupies. With the right flags it replaces a half-dozen ad hoc commands at once.

Syntax & Common Options

The general form is ls [OPTION]... [FILE].... With no path, ls lists the current directory. Combine flags freely — order does not matter.

ls -lah --color=auto /var/log
# -l   long listing: perms, links, owner, group, size, mtime
# -a   include dotfiles
# -h   human-readable sizes (1K, 234M, 2G)
# --color=auto  colorize when stdout is a TTY
Enter fullscreen mode Exit fullscreen mode

Syntax

The minimal invocation is just ls. Pass one or more paths to list specific targets, and stack short flags together (-lah) for compact commands.

ls                  # current directory
ls /etc /var        # multiple paths
ls -d */            # list directories themselves, not their contents
Enter fullscreen mode Exit fullscreen mode

Quick Reference Table

The flags you will reach for daily, mapped to what they actually do.

-l    long format
-a    show hidden (dotfiles)
-A    like -a but skip . and ..
-h    human-readable sizes (use with -l or -s)
-S    sort by file size, largest first
-t    sort by modification time, newest first
-r    reverse the sort
-R    recursive into subdirectories
-1    one entry per line (script-friendly)
-i    show inode numbers
Enter fullscreen mode Exit fullscreen mode

Common Flags

For day-to-day use, -lh is the workhorse. Add -t when you want freshly modified files first, or -S when hunting disk hogs.

ls -lhSr        # sort by size ascending, human-readable
ls -lt | head   # 10 most recently modified entries
ls -ld /etc     # show directory's own metadata, not its contents
Enter fullscreen mode Exit fullscreen mode

Practical Examples

Real workflows: find the biggest files, audit permissions, list only directories, or feed a clean filename list into a pipeline.

# Top 5 largest files in current dir
ls -lhS | head -n 6

# All files modified in the last few minutes (combine with stat for precision)
ls -lt | head

# Only directories, with trailing slash
ls -d */

# Machine-readable: one filename per line, no decoration
ls -1 --color=never
Enter fullscreen mode Exit fullscreen mode

Tips and Tricks

A few flag combinations every sysadmin keeps in muscle memory.

# Show hidden + sort by size + human readable
ls -lAhS

# Recursive listing piped to grep for a quick search
ls -R /etc | grep -i ssh

# Quote names with spaces or special chars
ls -lQ

# Long format including SELinux/ACL indicators
ls -lZ          # SELinux context
ls -l           # trailing '+' marks ACLs present
Enter fullscreen mode Exit fullscreen mode

Common Pitfalls

  • ls: cannot access: No such file or directory — the path argument does not exist or is misspelled. Tab-complete to verify, or quote globs that may have failed to expand.
  • Permission denied listing /root — the directory has mode 700 and you are not its owner. Re-run with sudo ls /root or check ownership with ls -ld /root.

Pro Tips

  • Alias ll='ls -lah' in ~/.bashrc — it is on every veteran's shell.
  • Use ls --group-directories-first on GNU coreutils to keep folders at the top.
  • Pipe ls -1 (digit one) into xargs only for filenames without spaces; otherwise prefer find -print0 | xargs -0.
  • Replace ls with eza or exa for git-aware listings and tree mode.

Related Commands

Bookmark this sheet and try the flag combos on a test box right now — your future self will thank you when a 500-file directory needs sorting at 2 a.m.

Top comments (0)