Back to Explore
GuideVibeFix editorialUpdated Nov 26, 2026

Linux Disk Space from Zero: df, du, and Finding the Big Files, Measured Live

Measured live: /tmp is 512M with 1015738 inodes; four files sum 37M while du reports the tree at 38M; a sparse file is 100M apparent but 0 on disk; deleting the 20M clip.dat drops the tree from 38M to 18M; du and df both exit 1 on a missing path.

Real photograph of the inside of an opened hard disk drive, metal platter and read-write head in close-up (illustrative photo)

Introduction: there are two ways for a disk to fill up

A disk usually fills up in one of two ways. The first way is blunt: a deploy fails halfway through, a log file cannot be written, a service stops dead with no space left. The second way is slower and more irritating: the system says the disk is full, but you cannot tell who is using the space. The log directory does not look that big, the cache is hiding somewhere, you delete a few files, and nothing changes. Those two ways of failing are two different questions: how much disk is left, and who is using it. On Linux, two old commands answer them: df and du.

df answers "how much disk is left", looking at the whole filesystem. du answers "how much does this directory use", walking the directory tree and adding up one file at a time. Both ship in GNU coreutils, zero third-party packages, present on any Linux machine out of the box. This guide does not ask you to memorize definitions. It works from a real machine: first build a tree of files with sizes fixed in advance, then measure it step by step with df and du, and every number quoted comes from a demo script that was actually run. The full record is attached to the guide. By the end you should be able to repeat the same workflow on your own machine: use df to decide which disk is full, use du plus sorting to pin down the exact directory, and only then decide what to delete and what to keep.

1. First, the split: df looks at the disk, du looks at a directory

Most people get stuck on the very first step of a disk-space investigation: should I use df or du? One sentence settles it: to know how much disk is left, use df; to know how much a directory uses, use du. df reads filesystem metadata and reports the total, used, available, and use percentage of a whole disk in an instant. du walks a directory tree and adds up the usage of every file inside it. The bigger the directory, the slower du runs, but the answer is precise down to the file.

The two commands often disagree, and that is not a bug. They measure different things. df measures one disk; du measures one directory tree. A tree may be only part of a disk, so du reports less than df. And even for the same directory on the same disk, df used space and the du total often differ. Section 8 goes through the four usual reasons in detail; for now remember the conclusion: a mismatch between df and du is normal. Use df to decide whether the situation is fatal, then use du to find the culprit.

One more trap for beginners: units. Both df and du report sizes in 1024-based units by default, so 1K is 1024 bytes and 1M is 1024K. Running df without -h prints long bare numbers counted in 1K blocks, hard to read; du without -h does the same. In practice both commands are almost always run with -h for human-readable output: df -h to look at disks, du -sh to look at a directory total. The -s flag means --summarize, report only the total and do not expand subdirectories, and it is the way du is most often used.

2. The environment, and how to read the output

Before touching anything, here is the environment. The demo ran on one Linux machine with du and df from GNU coreutils 9.4. The demo script is demo256_g1.sh, run once in Chinese and once in English, both exiting 0, with identical numbers in both transcripts. The working directory is /tmp/disk_demo256, wiped and rebuilt on every run, so the numbers below are reproducible, not a one-off accident.

First, look at the /tmp disk. The output of df -h /tmp is: tmpfs 512M 184K 512M 1% /tmp. Read it as: a tmpfs filesystem, total size 512M, used 184K, available 512M, use 1%. tmpfs is a memory filesystem; what lives in /tmp lives in memory and is gone after a reboot, which is also why the demo lives in /tmp: it cleans up after itself. Notice one detail: used is 184K, yet available still shows 512M, because 184K is negligible next to 512M and df -h rounds it away. That is the price of -h: readable, but small numbers get rounded off. For exact work, look at the raw numbers or use --block-size=1K.

df can also show inodes. The output of df -i /tmp is: tmpfs 1015738 36 1015702 1% /tmp. Total inodes 1015738, used 36, free 1015702, use 1%. An inode is an entry in the filesystem register: every file and every directory takes one. A disk can run out of inodes while still having free space, and then creating a new file fails with "No space left on device", exactly the same error as a full disk. That is the easiest misdiagnosis in disk troubleshooting. Always check df -h and df -i together: one watches space, the other watches inodes. On this machine /tmp has used only 36 inodes, nowhere near exhaustion.

When judging whether a disk is "full", do not look at the percentage alone. A use rate of 1% is obviously safe, but is 90% necessarily dangerous? Not always. What matters is the absolute amount of available space and how fast it is growing. A 2T disk with 10% left still has 200G, enough for a long time; a 512M tmpfs with 10% left has only 50M, and one large file fills it. The right way is: use Use% to set priority, use the absolute Avail figure to decide whether it is fatal, and use the growth trend to decide when it becomes fatal. Logs growing 1G a day with 10G left means trouble in ten days. That is the signal worth acting on.

3. Build a tree of known sizes, then measure it with du

Parameters alone do not stick; you have to measure something yourself. The demo script built a tree under /tmp/disk_demo256 (inside the tree directory): media/clip.dat 20M, logs/app.log 10M, logs/old.log 5M, cache/thumb.dat 2M. The four files were written at exact sizes with dd from /dev/zero, plus one small file, tree/notes.txt, containing a single line of text. The four files add up: 20M + 10M + 5M + 2M = 37M.

The result of du -sh tree is 38M. Four files total 37M, so why does du report 38M? Because du measures more than file contents: it also counts the disk blocks used by the directories themselves. The four directories tree, logs, cache, and media each take a block, plus a small block for notes.txt, and that pushes 37M up to 38M. The 1M difference is not an error; it is the definition of du. It measures disk usage, not the sum of file sizes. The next time du on your own machine reports a total slightly larger than the files add up to, think directory entries before doubting the command.

Look closely at how du -h prints sizes. Measured one by one: clip.dat 20M, app.log 10M, old.log 5.0M, thumb.dat 2.0M. Note the .0 after 5M and 2M, but not after 20M and 10M. That is not a typo; it is how GNU du -h prints: numbers below 10 keep one decimal place, numbers 10 and above print as integers. Measured by directory: tree/media is 20M, tree/logs is 15M, tree/cache is 2.0M. The logs total of 15M is exactly app.log at 10M plus old.log at 5M; a directory total is the total of its files plus a small block for the directory itself. The arithmetic checks out.

Two concepts need separating here: file size and disk usage. ls -lh shows file size, the number of bytes in the file. du shows disk usage, the number of disk blocks the file actually occupies. For ordinary files the two are close, differing only by block-alignment rounding. For a sparse file they can differ by orders of magnitude, and Section 5 measures exactly that. When investigating a full disk, always trust du, because whether a disk is full depends on usage, not on size.

Bar chart: the four files in the demo tree and the du total, clip.dat 20M, app.log 10M, old.log 5M, thumb.dat 2M, tree total 38M

4. Rank the space hogs: du plus sort, plus find

Knowing a directory total is not enough. The real question in an investigation is: who is biggest? du does not sort, so pipe it into sort -rh. The -r flag reverses the order, and -h sorts by human-readable sizes. Without -h, sort compares strings, and 100M sorts before 20M, the most common beginner mistake here. Measured: du -h --max-depth=1 tree | sort -rh, first line: 38M tree. The --max-depth=1 flag expands only one level of subdirectories, so you see the big picture first and decide which level to drill into.

Cut it another way: du -sh tree/* | sort -rh, first line: 20M tree/media. The glob tree/* measures each direct child of tree and sorts them, and media comes first with its single 20M clip.dat inside. Both cuts agree: the biggest thing in this tree is the media directory, which is clip.dat. Real investigations work the same way, one level at a time: --max-depth=1 to find the big level, go into it, measure again, until you reach the exact file. Each level costs one command, and ten minutes is enough to turn a full machine inside out.

When the target is a single big file rather than a directory, find is more direct. The command find tree -type f -size +1M -printf '%s %f\n' | sort -rn gives first line: 20971520 clip.dat. The number 20971520 is bytes: 20 times 1024 times 1024 is exactly 20971520, so the arithmetic checks out. There are 4 files over 1M in tree, exactly the four files we created, no more and no less. With -size, + means larger than, - means smaller than, and no sign means approximately equal; +1M, +100M, and +1G are the three most useful cutoffs in practice.

Put the two moves together and you have the standard space-hog workflow: df -h first to decide which disk is full, du -h --max-depth=1 with sort -rh to find the big directory, drill down one level at a time to the big file, then cross-check with find -size to make sure nothing was missed. The workflow itself is four commands. The hard part is not the commands; it is the habit of locating before deleting. Deleting before locating is the most common source of second accidents.

5. Sparse files: 100M on paper, almost nothing on disk

This section holds the most counterintuitive result in the whole guide. The command truncate -s 100M tree/sparse.img creates a 100M file out of thin air. Look at it with ls -lh and it is 100M. But du -sh tree/sparse.img reports 0. The 0 is not a mistake, and the file is not missing: the file is there, its size really is 100M, but it occupies almost nothing on disk.

This is a sparse file. truncate only changes the recorded length; it writes no data, so the middle of the file is all holes, and the filesystem allocates no disk blocks for holes. That is why du reports 0, while du -sh --apparent-size tree/sparse.img reports 100M. The --apparent-size flag means "apparent size", the number ls shows. Put the two numbers side by side and the sparse file stands revealed: apparent 100M, actual usage 0.

Now look at the whole tree. After adding the sparse file, du -sh tree is still 38M, unchanged; but du -sh --apparent-size tree has become 138M. The 38M is real usage, the 138M is apparent size, and the 100M in between is the hole in sparse.img. The conclusion is blunt: to judge whether a disk is full, look at du and its 38M, not at the apparent 138M. The other direction matters too: copying, backing up, or transferring files can fill in the holes. cp fills holes by default, so a sparse file that uses 0 on disk can really take 100M after copying. Use cp --sparse=always to keep the holes, or rsync --sparse. Before migrating large files, ask whether any sparse files are hiding in there.

Sparse files are not a rare disease. Virtual machine disk images, preallocated database files, half-downloaded large files, and holes left by truncating logs are all common sources. When ls shows a big file and du shows a small one, do not rush to delete it. Ask first: is this a sparse file? Compare du with --apparent-size and you have the answer in one second.

6. Delete a file and the space comes back, right?

Once the big file is located, the next step is deletion. Measured: running rm media/clip.dat inside the tree directory deletes the 20M clip.dat, and du -sh tree then reports 18M. From 38M down to 18M, exactly 20M less, the arithmetic is clean. The apparent size falls too: du -sh --apparent-size tree drops from 138M to 118M, again exactly 20M less. And du -sh /tmp/disk_demo256 reports 18M as well: the whole demo directory is down to 18M.

But deleting a file does not always free space immediately, and this is where beginners get confused. If a process still holds the file open, for example a service still writing into a deleted log, rm only removes the directory entry. The file itself stays on disk, the space is not freed, and df used space does not drop. The correct order is to find the process holding the file, stop the service or make it reopen its logs, and then delete. Or do it the other way round: truncate the log first, then delete. That is what log rotation tools do: instead of rm, they truncate the log file, the service keeps writing to the same file descriptor, space is freed at once, and no restart is needed.

There is another case where deleting frees nothing: the file is gone, but the directory sits under a mount point, or the filesystem itself holds reserved blocks. ext4 reserves 5% of space for the root user by default, so an ordinary user can see "full" while root can still write. That is design, not failure. After rm, always re-check with df -h at once: the job is done only when the space is back. If it is not back, look for files that are deleted but still held open, instead of deleting something else.

7. Errors and exit codes: paths that do not exist

Scripts that investigate disks need to tell success from failure, and that is what exit codes are for. Measured: du -sh /nonexistent-256 exits with code 1, and df /nonexistent-256 also exits with code 1. Both commands fail on a missing path, and both return 1. In a script you can write du -sh "$dir" 2>/dev/null || echo "directory does not exist" and branch on the exit code instead of parsing the error text. Error text changes with the locale; the exit code does not.

One more du behavior worth knowing: du walks down the tree, and when it meets a directory it cannot read it prints "Permission denied" and skips that directory, so the final total is incomplete. If a disk investigation shows a wall of Permission denied, do not panic; run it again as root or with sudo. If you can only run as an ordinary user, remember the total is an underestimate and discount the conclusion accordingly. This demo ran as root and hit no permission errors; that boundary is recorded in Section 10.

8. Four usual reasons du and df disagree

Back to the open question from Section 1: why does df used space so often differ from the du total? The four most common reasons, in the order worth checking, cover almost every case.

First, deleted but still open. Section 6 covered this: the file is deleted, a process still holds it, df counts the space as used, du can no longer see the file, so df used is larger than the du total. This is the most common source of the gap, and the first thing to check: look for files that are deleted but still held open by a process.

Second, mount-point shadowing. Another disk is mounted on top of a directory. du measures the directory tree above the mount point, while df measures the disk below it. Worse, files that used to live under the mount point are covered by the new mount: du cannot see them, df does not count them on the new disk, both sides are blind, and the space is still really used. When investigating, read the mount table with df -h first, then decide which tree du should measure.

Third, sparse files and block size. Section 5 measured it: a file with apparent size 100M and usage 0 makes ls and du differ by 100M. There is also block overhead: a 1-byte file still takes a whole block, usually 4K. A directory with millions of tiny files makes the du total much larger than the sum of file sizes. When a directory is full of small files, suspect block overhead first.

Fourth, filesystem overhead of its own. The inode table, the journal area, and reserved blocks belong to no directory: du cannot see them, df counts them as used. A freshly formatted empty disk still shows hundreds of megabytes used in df. That is the filesystem's own overhead; do not go looking for "who used it", it was always there.

9. Cheat sheet and checklist

The whole guide compresses into one cheat sheet. df -h: which disk is full, read Use% and Avail. df -i: are the inodes full? du -sh directory: how much does this directory use in total? du -h --max-depth=1 directory | sort -rh: which level below is biggest? du -sh directory/* | sort -rh: sort the direct children. find directory -type f -size +100M: find single files over 100M. du -sh --apparent-size file: compare with the apparent size and judge whether it is a sparse file. After rm, re-check with df -h: confirm the space really came back.

The investigation checklist runs in this order. Step one, df -h decides life or death: which disk is full, how much is left. Step two, df -i rules out inode exhaustion. Step three, du -h --max-depth=1 with sort -rh, drill down level by level to the big directory. Step four, find -size pins down the exact big file. Step five, before touching anything, confirm: can this file be deleted? Will a service stop? Is there a backup? Step six, after deleting, re-check with df -h; the job is done only when the space is back. Step seven, if the space did not come back, check deleted-but-open files and mount-point shadowing. Step eight, write down what happened: what grew, how fast, and when it will fill up again.

One last note for vibe coding projects. Many personal projects hit a full disk the first time they deploy to a small cloud host: logs never rotated, build caches never cleared, dependencies installed twice, temporary upload files never deleted. Three lines in the deploy script are enough: df -h to look first, du -sh to measure, and log rotation configured. A full disk is not a hard technical problem; it is a habit problem, and habits are checklists.

10. What this guide did not verify

An honest boundary list. First, ncdu is not installed on this machine, so this guide does not demonstrate ncdu interactive investigation; it only covers the du plus sort combination, and ncdu usage should follow its own official documentation. Second, locating deleted-but-open files was not demonstrated on this machine; Section 6 explains the mechanism and the direction to look, but it carries no measured command output for that step. Third, the demo ran as root and did not measure how much du totals undercount for an ordinary user hitting Permission denied; re-check before running as another user. Fourth, the demo used /tmp, a tmpfs with only 512M total, so the numbers must be re-checked on a real disk partition, especially reserved blocks and block size, which differ between filesystems. Fifth, this guide does not cover LVM expansion, btrfs subvolumes, or overlayfs; when a disk is full enough to need expansion, follow the official documentation for that filesystem instead of expanding from memory.

Even so, the core workflow never changes: df decides life or death, du finds the culprit, sort ranks by size, confirm before deleting, re-check after deleting. Four commands, eight steps, ten minutes, enough to turn a full machine inside out. The rest is writing that workflow into your own checklist, so the next time a disk alarm goes off, you do not have to go digging through directories by feel.

Views 0Comments 0

Comments (0)

ME
0/1000
Loading comments...

Sources

Browse projectsPublish your project

Related articles