ls — List and Inspect Directory Contents
Learn ls inside out: show hidden files, read the long format output, sort by size and time, and combine options for powerful directory insights.
ls (list) is probably the command you'll run more than any other in your Linux career. It shows you what's in a directory — but with the right options, it reveals permissions, sizes, ownership, modification times, and file types, giving you a complete picture of any directory at a glance.
Basic Usage
ls # List current directory
ls /path/to/dir # List a specific directory
ls /etc /var # List multiple directories
ls file1.txt # Info about a specific fileMost Important Options
ls Options
| Option | Long Form | Description |
|---|---|---|
-a | --all | Show all files including hidden (. files) |
-A | --almost-all | Show hidden but NOT . and .. |
-l | Long format: permissions, size, date | |
-h | --human-readable | Human-readable sizes (K, M, G) — use with -l |
-t | Sort by modification time (newest first) | |
-S | Sort by file size (largest first) | |
-r | --reverse | Reverse the sort order |
-R | --recursive | List subdirectories recursively |
-d | --directory | List the directory itself, not its contents |
-F | --classify | Add / after dirs, * after executables |
-1 | One file per line |
Showing Hidden Files
Files whose names start with . are hidden by default — the OS doesn't show them in a normal listing. They're not protected or secret, just visually hidden to reduce clutter. Configuration files (dotfiles) use this convention: .bashrc, .gitignore, .ssh/.
ls # Normal listing — hidden files not shown
ls -a # Show ALL files (includes . and ..)
ls -A # Show hidden files EXCEPT . and ..
# In your home directory:
ls -a ~
# . .. .bashrc .bash_history .gitconfig .ssh Documents Downloads ProjectsThe Long Format (-l)
The -l flag unlocks the richest output. Each line tells you seven things about a file:
ls -l
# Example output:
-rw-r--r-- 1 sagar staff 4096 Jan 28 10:30 notes.txt
drwxr-xr-x 3 sagar staff 96 Jan 27 09:15 Projects/
lrwxrwxrwx 1 root root 11 Sep 12 08:00 log -> /var/log
#│ │ │ │ │ │ │
#│ │ │ │ │ │ └── filename
#│ │ │ │ │ └── last modified
#│ │ │ │ └── size in bytes
#│ │ │ └── group owner
#│ │ └── user owner
#│ └── number of hard links
#└── permissions (10 characters)The First Character: File Type
| Char | Type |
|---|---|
- | Regular file |
d | Directory |
l | Symbolic link |
b | Block device (disk) |
c | Character device (terminal, serial) |
p | Named pipe |
s | Socket |
# Long format combinations you'll actually use:
ls -l # Long format
ls -lh # Long + human-readable sizes (shows "4.0K" not "4096")
ls -la # Long + show hidden files
ls -lah # Long + hidden + human-readable (most complete)
ls -lS # Long + sorted by size (largest first)
ls -lt # Long + sorted by time (newest first)
ls -ltr # Long + sorted by time, reversed (oldest first — great for logs!)Sorting
# Sort by modification time — newest files at the top
ls -lt /var/log
# Sort by time, reversed — oldest first (see most recent changes at bottom)
ls -ltr /var/log
# Sort by file size — largest first
ls -lS ~/Downloads
# Sort by size, reversed — find smallest files
ls -lSr ~/DownloadsUseful Patterns
# ─── Filtering ───────────────────────────────────────────────
ls *.txt # Only .txt files
ls *.{jpg,png,gif} # Multiple extensions
ls -d */ # Directories only (trailing /)
ls -p | grep -v / # Files only (exclude dirs)
# ─── Counting ────────────────────────────────────────────────
ls -1 | wc -l # Count files in current dir
ls -1 *.log | wc -l # Count .log files
# ─── Recursive ───────────────────────────────────────────────
ls -R ~/Projects # List everything recursively
ls -R | grep ".ts$" # Find all .ts files recursively
# ─── Full paths ──────────────────────────────────────────────
ls -d "$PWD"/* # Absolute paths of all files here
# ─── Indicators ──────────────────────────────────────────────
ls -F # dirs/ executables* links@You want to find the 5 largest files in `~/Downloads`. Which command is most direct?
In `ls -l` output, what does the first character `d` mean?
Practice with ls:
- List your home directory showing hidden files in long human-readable format
- Sort
/var/logby modification time (newest first) and identify the most recently changed log file - Count how many files are in
/usr/bin - List only directories inside
/etc