Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 sort and head to produce the classic “top 10 biggest directories” report in one line.

du counts 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-readable prints K, M, G instead 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 / --summarize prints 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 -h ranks 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 .cache and .local — often the actual space hogs on a desktop.
  • 2>/dev/null hides 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=0 reports 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-size reports the file’s logical length (what ls -l shows) 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=PATTERN skips 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 — du prints one line per argument, so you can weigh them side by side before choosing what to remove with rm -rf.

Common Pitfalls

  • du and df disagree after large deletes. Deleted-but-still-open files keep their blocks: du no longer sees them, df still counts them. Restart the holding process (often a log daemon) to reclaim the space.
  • Default du units are 1 KiB blocks, not bytes. On macOS the default unit is 512-byte blocks. Always add -h when reading numbers by eye.
  • du -sh * misses hidden files. Add a pass over .* — .cache and .local are frequently the real answer.
  • Running du on a whole disk is slow and noisy. Limit depth (-d 1) and exclude mounts (-x) or virtual filesystems before pointing it at /.

CommandDescription
dfFree and used space per filesystem
ncduInteractive, navigable disk-usage browser
findLocate the files du is counting
rmDelete what you’ve found
rsyncMove the space elsewhere before deleting

du Cheatsheet

CommandWhat It Does
du file dirSize of each argument
du -h dirHuman-readable sizes
du -sh dirOne summary line per argument
du -sh *Size of everything visible in current directory
du -h -d 1 . | sort -hDirectories ranked by size
du -sh .* 2>/dev/nullInclude hidden files
du --apparent-size -sh fLogical size instead of allocated blocks
du -sh --exclude='*.iso' .Skip matching files
du -x -sh /Stay on one filesystem