What ls does
ls shows the contents of a directory. By default it lists the current directory, but you can give it paths or patterns.
ls # list current folder
ls /etc # list a specific folder
ls *.py # list files matching a pattern (shell expands the *.py)
Most-used options (with tiny examples)
ls -a # include hidden files (names starting with .)
ls -A # like -a but hides . and ..
ls -l # “long” listing (permissions, owner, size, time)
ls -lh # long listing with human-readable sizes (K, M, G)
ls -la # long + all (common)
ls -alh # order of flags doesn’t matter
ls -t # sort by modified time (newest first)
ls -tr # reverse (oldest first)
ls -S # sort by size (largest first)
ls -X # sort by extension
ls -U # don’t sort (fast on huge dirs)
ls -R # recursive listing (subdirectories too)
ls -d */ # list directory names themselves (not their contents)
ls -F # classify: add / for dirs, * for executables, @ for symlinks
ls --color=auto # colored output (often already default on Linux)
ls --group-directories-first # puts folders before files (GNU ls)
Tip: Many systems define ll as an alias for ls -alF. Try type ll to see.
Reading the long listing (ls -l)
Example line:
-rwxr-x--- 1 alice devs 1256 2025-10-08 script.sh
^ ^^^ ^^^ ^ ^ ^ ^ ^ ^
| | | | | | | | └─ name
| | | | | | | └─ modified time
| | | | | | └─ size (bytes; add -h for KB/MB)
| | | | | └─ group
| | | | └─ owner
| | | └─ hard link count
| | └─ permissions for group (r=read, w=write, x=execute)
| └─ permissions for owner
└─ type/others: '-' file, 'd' directory, 'l' symlink, 'c' char dev, 'b' block dev, 's' socket, 'p' fifo
Practical combos you’ll actually use
ls -lah # everything, detailed, human sizes
ls -lt | head # 10 most recently modified items
ls -l --time-style=long-iso # ISO timestamps (GNU ls)
ls -al --group-directories-first
ls -1 # one entry per line (good for scripts)
Patterns (globbing) & quoting
- *.c matches all .c files (the shell expands this before ls runs).
- Quote to prevent the shell from expanding (to see the literal pattern):
· ls "*.c" # passes the asterisk to ls literally
Hidden files and “dotfiles”
- Files starting with . are hidden (e.g., .bashrc, .gitignore).
- Use -a or -A to see them:
· ls -A ~
Performance & safety tips
- On very large folders, unsorted is faster: ls -U.
- Avoid parsing ls in scripts (spaces/newlines in names can break logic). Prefer:
· find . -maxdepth 1 -print0 | xargs -0 -I{} printf '%s\n' "{}"
- If names contain spaces, quote variables: ls "$dir".
Common gotchas (Linux vs macOS)
- Linux (GNU coreutils) supports --group-directories-first, --time-style.
- macOS/BSD ls differs: color uses -G (not --color), and some GNU options aren’t available. You can install GNU coreutils (gls) via Homebrew if needed.
Mini-lab (10–15 minutes)
mkdir -p ~/lab/ls && cd ~/lab/ls
touch a.txt b.txt ".hidden" script.sh
chmod +x script.sh
dd if=/dev/zero of=big.bin bs=1M count=5 2>/dev/null
ls
ls -a
ls -lh
ls -lt
ls -S
ls -F
ls -d */ # create a dir and try again: mkdir data; ls -d */
Quick reference
- List all: ls -a
- Detailed: ls -l (add -h for sizes)
- Newest first: ls -lt
- Largest first: ls -lS
- Folders first: ls --group-directories-first (GNU)
- Recursive: ls -R
- Show types: ls -F
Want this as a one-page printable sheet for your class? I can format it nicely.