du Command Guide for Estimating File Space Usage
du reports how much disk space files and directories occupy, so you can find what’s filling up your disk before it fills up completely.
What is du, and Why Use It?
du (“disk usage”) walks through files and directories and estimates the space each one consumes. Point it at a directory and it totals everything inside; point it at a few candidates and it tells you which one is the hog.
du answers the question “how big is this thing?” — the complement of df, which answers “how much room is left?”
Why Learn du?
- Your disk is full and you need to find the culprit.
- You’re about to delete or archive something and want to know what it weighs.
- Backups and quotas depend on knowing per-directory sizes.
- It pairs with
sortandheadto produce the classic “top 10 biggest directories” report in one line.
ducounts allocated blocks, which is why its numbers can differ slightly from a file’s “size on disk” in a file manager — and why sparse files can report smaller than their logical contents.
1. Check the Size of a File or Directory
du file.txt
du photos/
- Prints the size of each argument in the default unit (usually 1K blocks):
4.0K file.txt
1.2G photos
2. Human-Readable Sizes
du -h photos/
-h/--human-readableprintsK,M,Ginstead of raw block counts. This is the flag you almost always want.
3. Summarise a Directory (One Number, Not a List)
du -sh photos/
-s/--summarizeprints only the total for each argument — no per-file breakdown.du -sh *is the standard “how big is everything in here?” opener.
Example:
du -sh ~/Downloads/* | sort -h
4. Find the Biggest Items in a Directory
du -h --max-depth=1 . | sort -h
--max-depth=1(or-d 1) limits recursion to one level — direct children only.- Piping through
sort -hranks them, biggest last. The biggest directory in the list is your next target.
Example:
du -h -d 1 /var | sort -h | tail -5
- Shows the five largest first-level directories under
/var— a classic “what filled the server?” move.
5. Include Hidden Files and All Entries
du -sh .* 2>/dev/null
.*catches dotfiles and dot-directories like.cacheand.local— often the actual space hogs on a desktop.2>/dev/nullhides errors from entries like.and..that don’t resolve cleanly.
6. See Sizes Without Recursing Into Subdirectories
du -s --max-depth=0 *
-d 0/--max-depth=0reports just the named items, nothing inside them.
7. Count Only One Kind of Entry
du -sh --files0-from=- . # GNU: sizes from a NUL-separated file list
du --apparent-size -sh big.log # logical size, not allocated blocks
--apparent-sizereports the file’s logical length (whatls -lshows) rather than blocks allocated on disk — the number you want when comparing against a transfer size or a cloud bill.
8. Exclude Paths You Don’t Care About
du -sh --exclude='*.iso' --exclude='./snap' .
--exclude=PATTERNskips matching files or directories — useful when snapshots, caches, or media collections would drown the signal.
9. Spot Discrepancies Between Apparent and Allocated Size
du -h --apparent-size sparse.img
du -h sparse.img
- If the first number is much bigger than the second, the file is sparse: mostly zeros that the filesystem doesn’t actually store. Common with virtual disk images and database files.
10. Measure a Directory Tree Before Deleting It
du -sh ./build ./node_modules ./target
- Pass several candidates at once and compare —
duprints one line per argument, so you can weigh them side by side before choosing what to remove withrm -rf.
Common Pitfalls
duanddfdisagree after large deletes. Deleted-but-still-open files keep their blocks:duno longer sees them,dfstill counts them. Restart the holding process (often a log daemon) to reclaim the space.- Default
duunits are 1 KiB blocks, not bytes. On macOS the default unit is 512-byte blocks. Always add-hwhen reading numbers by eye. du -sh *misses hidden files. Add a pass over.*—.cacheand.localare frequently the real answer.- Running
duon a whole disk is slow and noisy. Limit depth (-d 1) and exclude mounts (-x) or virtual filesystems before pointing it at/.
Related Commands
| Command | Description |
|---|---|
| df | Free and used space per filesystem |
| ncdu | Interactive, navigable disk-usage browser |
| find | Locate the files du is counting |
| rm | Delete what you’ve found |
| rsync | Move the space elsewhere before deleting |
du Cheatsheet
| Command | What It Does |
|---|---|
du file dir | Size of each argument |
du -h dir | Human-readable sizes |
du -sh dir | One summary line per argument |
du -sh * | Size of everything visible in current directory |
du -h -d 1 . | sort -h | Directories ranked by size |
du -sh .* 2>/dev/null | Include hidden files |
du --apparent-size -sh f | Logical size instead of allocated blocks |
du -sh --exclude='*.iso' . | Skip matching files |
du -x -sh / | Stay on one filesystem |