Biggest Directories
Problem statement
Find the three folders using the most disk space, biggest first, with du, and see why sort -h is needed for sizes like 2M and 1.5G. When a disk fills up, this is how you find where the space went, one level at a time.
The script builds this tree with real data (random bytes, so nothing is compressed or sparse):
tree
logs/ 5.5 MiBcache/ 2.5 MiBbackups/ 9.5 MiBdocs/ 0.5 MiB- Print the three biggest folders, in MiB, biggest first.
- Show how
sort -nandsort -horder the sizes900K,2Mand1.5G.
Expected output:
== three biggest folders (MiB, rounded up) == 10 MiB backups/ 6 MiB logs/ 3 MiB cache/== sort -n reads only the number ==1.5G 2M 900K== sort -h understands K, M and G ==900K 2M 1.5GHints
du -sm */ prints one line per folder with its total size in MiB (rounded up). sort -rn puts the biggest first and head -n 3 keeps three.Approach
Optimal: du -s with sort -rn
Covers: du, du -s, du -h, du -m, --max-depth=1 / -d 1, -x, sort -h vs sort -n, drilling down, du vs df.
du measures what is really on disk. du (disk usage) adds up the space used by files under a folder. With -s (summary) it prints one total per folder you name, instead of a line for every subfolder.
| Command | Prints |
|---|---|
du -sh folder |
one total, human readable, like 9.6M |
du -sm */ |
one total per subfolder, in MiB |
du -h --max-depth=1 /var |
each folder one level under /var (GNU) |
du -h -d 1 /var |
the same, shorter form (GNU and macOS) |
du -xh -d 1 / |
the same, but stay on one disk |
*/ is a shell pattern for "every folder here", so du -sm */ gives one line per folder.
Drill down, one level at a time. You rarely find the culprit in one step. Look at the top level, go into the biggest folder, and repeat:
-x matters at /: without it, du also walks into other disks mounted below, like /data or network shares, which is slow and mixes up the numbers.
sort -h understands sizes. du -h prints sizes with letters. sort -n reads only the leading number, so 1.5G (1.5) comes before 2M (2) and 900K (900) comes last, which is backwards. sort -h (human numeric) knows K < M < G < T:
Walking through the code. The # Setup: lines only fill four folders with random data, so skip past them.
du -sm */totals each folder in MiB, rounding up, so 9.5 MiB shows as 10.sort -rnandhead -n 3keep the three biggest, and awk formats each line.- The two
printf | sortlines show the same three sizes sorted both ways.
Edge cases. du only counts files you can read; run it with sudo on system folders, and add 2>/dev/null to hide "Permission denied". Hard-linked files are counted once per du run. If du totals much less than df says is used, the space may be held by deleted files that are still open, covered on the Deleted Files page.
# Setup: fill four folders with random data (so sizes are real), in a fresh temporary folder
cd "$(mktemp -d)"
mkdir logs cache backups docs
head -c 5632K /dev/urandom > logs/app.log
head -c 2560K /dev/urandom > cache/blob.bin
head -c 9728K /dev/urandom > backups/db.dump
head -c 512K /dev/urandom > docs/manual.pdf
echo "== three biggest folders (MiB, rounded up) =="
du -sm */ | sort -rn | head -n 3 | awk '{ printf "%3d MiB %s\n", $1, $2 }'
echo "== sort -n reads only the number =="
printf '900K\n2M\n1.5G\n' | sort -n | paste -sd' ' -
echo "== sort -h understands K, M and G =="
printf '900K\n2M\n1.5G\n' | sort -h | paste -sd' ' -RecapThe whole problem in a few lines, for the night before
- Spot it: "what is eating the disk"
- Idea:
du -xh -d 1 /path | sort -rh | head, then repeat inside the biggest - Cost: walks every file under the path
- Trap: sorting human sizes with
sort -n; usesort -h
Interview follow-ups
Find the biggest folders under /var, but only on the root disk, as fast as possible.
sudo du -xh -d 1 /var 2>/dev/null | sort -rh | head -n 10.-xstays on one filesystem,-d 1lists only the first level, andsort -rhputs the biggest human sizes first. The last line ofduoutput is the total for/varitself, which sorts to the top; that is expected. Then run the same command on the biggest folder it shows, and keep going down.
Frequently asked questions
They measure different things. df asks the filesystem how many blocks are in use, while du walks the folders and adds up the files it can see. du misses files deleted while a program still has them open (they have no name to find), files hidden under a mount point, and folders it cannot read. A small gap is normal; a big one usually means deleted but open files, which lsof +L1 shows.
ncdu is an interactive du: it scans once, then lets you move through folders sorted by size and delete from inside it, which is great on a full server if you can install it. du itself is fast enough for most folders. For huge trees, run it once on the top level with -d 1, then only on the biggest branch, rather than on everything at once.
du -h uses powers of 1024, so M is a MiB (1,048,576 bytes) and it rounds up to keep one decimal. --si uses powers of 1000 instead, matching how disk makers count. du shows space used on disk, which is counted in blocks (often 4 KiB), so a 1-byte file uses 4K. For file content size instead of disk use, add --apparent-size (GNU).