Chapter 30: Bash Directory Usage (du)
What does du actually do? (super simple first)
du = disk usage It tells you how much space each folder (and its contents) is using on your disk.
Key difference from df:
- df → shows total/used/available per partition/drive (big picture)
- du → shows how much space each folder/file/subfolder is using (detailed breakdown)
Without du, you see /home is 200 GB full in df, but you don’t know which folder inside /home is the big culprit (Downloads? Videos? .cache? node_modules?).
du helps you hunt down space hogs so you can clean up safely.
1. Basic usage (try right now!)
|
0 1 2 3 4 5 6 |
du ~ |
This shows space used by every folder in your home directory (and subfolders) in 1K-blocks (1024 bytes each) – very hard to read!
Better – always use these flags together:
|
0 1 2 3 4 5 6 7 8 |
du -sh ~ # or du --summary --human-readable ~ |
- -s = summary (only show total for the folder you asked)
- -h = human-readable (shows KB, MB, GB, TB instead of blocks)
Example output you might see:
|
0 1 2 3 4 5 6 |
48G /home/arman |
2. Most useful combinations (you’ll use these 95% of time)
| What you want to see | Command you type | What it shows |
|---|---|---|
| Total space used by your home folder | du -sh ~ or du -sh $HOME | One nice line (e.g. 48G) |
| Biggest folders in current directory | du -sh * |
sort -hr |
| Top 10 biggest folders in /home | du -sh /home/* |
sort -hr |
| Detailed breakdown of one folder | du -h –max-depth=1 ~/.cache | sort -hr |
| Find huge files (not folders) | du -ah ~ | sort -hr |
| Check entire root (careful – takes time) | sudo du -sh /* |
sort -hr |
| Ignore permission errors | sudo du -shx /* 2>/dev/null |
sort -hr |
Try this right now (very useful!):
|
0 1 2 3 4 5 6 |
du -sh ~/* | sort -hr | head -10 |
Example output:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
28G /home/arman/Videos 12G /home/arman/Downloads 5.2G /home/arman/.cache 3.1G /home/arman/node_modules 1.8G /home/arman/Pictures ... |
Now you know exactly where to clean first!
3. Very common flags explained (learn these!)
| Flag | Meaning | When to use |
|---|---|---|
| -h | Human-readable (GB, MB, etc.) | Always! |
| -s | Summary – only total | Quick total check |
| -a | All – show files too (not just folders) | Find big single files |
| –max-depth=N | Limit depth of display | Avoid thousands of lines |
| -c | Grand total at bottom | When listing many folders |
| -x | Stay on one filesystem (skip mounted drives) | Faster on / |
| –apparent-size | Show apparent size (not disk blocks) | When symlinks/hard links confuse |
| -k / -m | Force KB / MB output | Old scripts |
4. Real-life examples (Hyderabad developer style)
- Laptop saying disk full when downloading movies?
Bash01234567du -sh ~/Downloads/* | sort -hr | head -5# → delete old .zip or .torrent files
- VS Code / Node.js project eating space?
Bash01234567du -sh node_modules | sort -hr# → 4.8G node_modules → rm -rf node_modules && npm ci
- Where are all my Python virtualenvs?
Bash0123456du -sh ~/.virtualenvs/* | sort -hr | head -8
- Check Docker images / containers / volumes
Bash01234567sudo du -sh /var/lib/docker/* | sort -hr# → if huge → docker system prune -a --volumes
- Find biggest log files
Bash0123456sudo du -ah /var/log | sort -hr | head -15
- Quick space check before big git clone
Bash0123456du -sh .git 2>/dev/null || echo "No git repo here"
5. Quick cheat-sheet table
| Goal | Command example | Notes |
|---|---|---|
| Total home usage | du -sh ~ | Fastest check |
| Top 10 biggest things in current dir | du -sh * |
sort -hr |
| Biggest folders in /home | sudo du -sh /home/* | sort -hr |
| Detailed 1-level deep | du -h --max-depth=1 ~/.cache |
sort -hr |
| All files + folders sorted | du -ah ~ |
sort -hr |
| Skip mounted drives (faster on /) | sudo du -shx /* | sort -hr |
| Total + list | du -sch * | sort -hr |
| Find old large backups | du -sh ~/backup_* | sort -hr` |
6. Pro tips from daily use
- du is slow on huge folders (e.g. millions of files) – be patient or use –max-depth
- Combine with sort -hr almost always (human-readable + reverse + numeric)
- Use sudo when checking /var, /usr, /opt
- du shows disk blocks used (can be more than file size due to block size) → use –apparent-size if you want actual file size
- Alternative modern tools: ncdu (install it – interactive ncurses du!)
Bash012345678sudo apt install ncduncdu ~# → arrow keys, delete files inside it!
Now open your terminal and try these 3 right now:
|
0 1 2 3 4 5 6 7 8 |
du -sh ~ du -sh ~/* | sort -hr | head -8 du -sh ~/.cache | sort -hr |
Tell me:
- What’s the biggest folder in your home right now? (e.g. 28G Videos)
- Or ask: “How to find all files bigger than 1GB?” or “du vs df – when to use which?” or “How to use ncdu?”
We’ll solve it together! 😄
