du From Zero: Disk Full? First See Who Is Taking the Space
df says the disk is full; it never says who took the space. du does. This guide runs every command for real on GNU coreutils du 9.4: -sh totals, -d drilling, block rounding (a 100-byte file takes 4.0K), sparse files (100M apparent, 0 on disk), -b vs --apparent-size, --exclude, and the three reasons du and df disagree — plus a full-disk investigation flow.

When a disk fills up, the first instinct is usually to run df and see how much is left — but df only tells you "it is full," not "what is taking the space." To answer the second question, the command is du. Its manual says it in one sentence: "du estimates file space usage." The word "estimates" is not modesty, it is honesty: du does not measure how many bytes are in a file, it measures how much room the file takes up on disk. Those two numbers are often different, and understanding why is the whole of du. Every command in this guide was run for real on GNU coreutils 9.4 du, with output pasted verbatim. The demonstration uses a fixed directory tree built for the purpose, with exactly controlled file sizes, so every number is reproducible: src/a.txt is 100 bytes, src/b.bin is 2,048 bytes, assets/big.dat is 1,048,576 bytes (exactly 1 MiB), assets/readme.md is 6 bytes, sparse.bin is a 100MB sparse file, and node_modules/pkg/index.js is 512 bytes. The block size on this machine's filesystem is 4,096 bytes, and all numbers are read against it.
Start with the most-used command: du -sh. The -s option shows only a total (summarize), and -h shows it in human-readable units. Before that, a baseline for apparent size — the output of du -b src/a.txt src/b.bin:
100 src/a.txt 2048 src/b.bin
In this guide's demo, du -sh . over the whole tree gives:
1.1M .
The whole tree takes 1.1M on disk. That number is easy to account for: big.dat alone takes 1 MiB, and everything else together is under 20KB, so the total is essentially big.dat's 1 MiB plus a little extra. The -h units are powers of 1024: 1K is 1,024 bytes, 1M is 1,048,576 bytes; for powers of 1000, use --si. Without -h, the default unit is 1,024 bytes — unless POSIXLY_CORRECT is set, in which case it is 512. The -k option is the same as a 1K block size, and -m the same as 1M. The easiest trap for a du beginner is running du without -s: it prints every subdirectory, hundreds of lines, and finding the total means scrolling to the last line. Build the habit: -sh for the total, and drill down only when you want the distribution.
Drilling down uses -d (--max-depth). The command du -h -d 1 . means: show results only one level down. The output of the recorded run:
4.0K ./node_modules 1.1M ./assets 8.0K ./src 1.1M .
At a glance: assets takes 1.1M and is the bulk of it; src takes only 8.0K; node_modules only 4.0K. The option --max-depth=0 is the same as -s, showing only the total; -d 2 drills one level deeper, showing big.dat and readme.md inside assets separately. Investigating a full disk is basically this command repeated from the top down: du -sh / for the total, then du -h -d 1 / for the biggest top-level directory, then drill into it, until you find the multi-gigabyte log file or cache directory. One level at a time, and the output never drowns you in hundreds of lines.
The 8.0K for src is worth pausing over to do the arithmetic. Inside src there are two files: a.txt at 100 bytes and b.bin at 2,048 bytes, 2,148 bytes together, under 3KB. But du says src takes 8.0K. This is the first gap between du and "how big is the file": disks allocate in blocks, and one block here is 4,096 bytes, so a file takes a whole block no matter how small it is. The 100-byte a.txt takes one 4.0K block, the 2,048-byte b.bin takes one 4.0K block, and the two together are exactly 8.0K. The way to verify is du -B 1: -B sets the block size to 1 byte, so it shows disk usage in bytes. The recorded run:
4096 src/a.txt 4096 src/b.bin 1048576 assets/big.dat 4096 assets/readme.md 0 sparse.bin
And the whole src directory's disk usage is:
8192 src
The 100 bytes of a.txt take 4,096 on disk, the 2,048 bytes of b.bin also take 4,096, and the 6-byte readme.md takes 4,096 as well; only big.dat, at 1,048,576 bytes, is an exact multiple of 4,096, so its apparent size and its disk usage are identical. Remember the rule: the more small files, the more waste — ten thousand 100-byte files look like 1MB but take 40MB on disk. That is one reason node_modules looks so frightening: it is full of small files.
The second gap is the sparse file, and sparse.bin exists to show it. Built with truncate -s 100M, sparse.bin is logically 100MB, all zeros, but takes almost no room on disk. Three faces of du show it clearly:
0 sparse.bin 104857600 sparse.bin 100M sparse.bin
The first line is du -h sparse.bin: disk usage 0. The second is du -b sparse.bin: apparent size 104,857,600 bytes. The third is du --apparent-size -h sparse.bin: apparent size in human-readable units, 100M. The -b option means "--apparent-size --block-size=1": apparent size, in bytes. The --apparent-size option means "print apparent sizes rather than device usage." The manual adds a warning worth quoting: although the apparent size is usually smaller, it may be larger, due to holes in sparse files, internal fragmentation, and indirect blocks. The command ls -l shows the apparent size, so ls says 100M and du says 0, and both are right — they are measuring different things. Virtual-machine disk images, preallocated database files, and half-downloaded large files are sparse files you meet in real life; looking only at ls makes you think the disk is nearly full, looking only at du makes you think there is plenty of room, and only the two together give the complete picture.
Next, totals and per-file listings. The output of du -h -c src:
8.0K src 8.0K total
The -c (--total) option adds a grand total line at the end. With a single directory, the total line repeats the directory's number and looks redundant; with several directories, it is their sum. The output of du -ah src:
4.0K src/b.bin 4.0K src/a.txt 8.0K src
The -a (--all) option lists files as well, not just directories. Note the order: output follows directory traversal order, not size order. To sort by size, the output has to go to another command — but that is sort's topic, not this guide's, so one sentence is enough: du does not sort, it only measures. The options -s and -c combine, and -a and -d combine too (-d limits the depth, -a decides whether files are listed), so together they build compound queries like "drill to depth N, list the files too, and add a grand total."
Investigations often need to leave some directories out, and --exclude does that. The output of du -h -d 1 --exclude=node_modules .:
1.1M ./assets 8.0K ./src 1.1M .
The node_modules line is gone, and the total is still 1.1M — because it was only 4.0K and never mattered to the big picture. After --exclude comes a shell-style pattern, not a regular expression: * matches any string, ? matches any one character. The manual's example is du --exclude='*.o', which skips every file and subdirectory ending in .o. Quote the pattern so the shell does not expand it first. There is a companion option, -X (--exclude-from=FILE), which reads the patterns from a file, one per line, for when there are too many rules to type. A classic disk-investigation usage is du -h -d 1 --exclude=proc / and similar combinations — /proc is the kernel's virtual filesystem, its numbers are generated live and take no disk, so du-ing it is meaningless, and excluding it removes a lot of noise.
The relationship between du and df is a boundary this guide has to draw clearly, because the two often disagree. df reports filesystem-level numbers: how big it is in total, how much is used, how much is left. du reports the sum of the files. When they disagree, it is usually one of three situations. First, a file was deleted but a process still holds it open: the file is gone from its directory, so du cannot count it, but the disk space is only freed when the process exits and the file descriptor closes, so df still shows full. Finding the process that holds a deleted file is lsof's job (lsof has its own guide). Second, something is hidden under a mount point: a directory is covered by another filesystem mounted on top of it, du cannot drill into it, and df counts it under the other filesystem; du -x (--one-file-system) keeps du from crossing filesystems and is the option for exactly this case. Third, sparse files and block rounding: du counts by blocks and df counts by filesystem blocks, and the two round differently, so a directory full of small files can differ by a few percent. The correct order for investigating "the disk is full" is: df first, to see which filesystem is full, then du -x -h -d 1 to drill into that filesystem. Each tool owns one stretch of the job; do not use du's numbers to doubt df, or df's numbers to dismiss du.
Hard links and symbolic links are the last two traps in du. A hard link is a second name for the same file on disk: one file with two hard links is counted once by default, because it really does take only one share of disk. To count it every time, use -l (--count-links). Symbolic links are not followed by default: when du meets a symbolic link, it counts only the tiny space the link itself takes and does not drill into the directory it points to. To follow links, there are three options of increasing strength: -D (--dereference-args) follows only links listed on the command line, -H is the same as -D, and -L (--dereference) follows all of them. Be careful with -L: a loop of links (A pointing to B, B pointing back to A) makes du chase its own tail — coreutils detects the loop and reports an error, but the output is already confused by then. Not following links by default is the safest choice, and most investigations never need to change it.
A few rarely mentioned options are worth listing here for the day they save you. The --time option adds the last modification time of everything in the directory to each line, which is useful for finding "which directory grew recently"; add --time=atime or ctime, or --time-style=long-iso, to change the time and the format. The -t (--threshold) option filters by size: -t 100M shows only entries above 100M, -t -100M only entries below 100M, which finds large files faster than scanning by eye. The --inodes option counts inodes instead of bytes — when too many small files exhaust the inodes, the disk still has space but writing a file fails with "No space left on device," and this is the option that finds it. The -S (--separate-dirs) option makes directories not include the size of their subdirectories, answering "how much does this directory itself take, not counting what is under it." None of these were demonstrated one by one in this guide; when you need them, the manual's matching section has them all by name.
Here is a complete investigation flow that ties the earlier sections together. One morning, monitoring reports the root partition at 98 percent. Step one: df -h to see which filesystem is full, confirming it is / rather than /home or a data disk. Step two: du -x -h -d 1 / to drill into the root partition, with -x making sure not to cross into other mount points; the biggest entry is usually /var or /usr. Step three: follow the biggest directory downward, du -h -d 1 /var, then du -h -d 1 /var/log, until you find the log file of several gigabytes. Step four: look at what the file is — a log growing out of control (find the service writing it and check whether log rotation is configured), a cache directory (clear it and rebuild it), or a sparse file misjudged (compare ls -l with du --apparent-size). Step five: before deleting anything, confirm the file is not still in use by a process (lsof), that it can be rebuilt after deletion, and that deleting it will actually free the space (a deleted-but-held file only frees its space when the service restarts). In this flow, du's role is always "measure." It gives you the numbers; whether to delete, and how, always requires seeing clearly first — deleting the wrong file when a disk is full is worse than the full disk itself.
One trap hides in shell expansion: du -sh * and du -sh . look similar but measure different things. The asterisk is expanded by the shell before du runs, and it expands to every name in the current directory that does not start with a dot — hidden files and directories (like .cache or .git) are not expanded and are never measured. But du -sh . measures the whole directory, hidden parts included. A .cache holding several gigabytes makes du -sh * report "not much" while du -sh . tells the truth. When investigating a disk, always pass the directory directly, as in du -sh <directory>, and never use asterisk expansion. Similarly, when du is given several directories, each gets its own line, and -c can add a grand total — but if the directories nest inside each other (passing both . and ./assets), the inner one is counted twice. du does not deduplicate for you; it measures each argument you give it, one by one.
Unit conversion deserves its own careful treatment, because du's default unit is the most misread part of it. With no unit option, du's default unit is 1,024 bytes — unless the POSIXLY_CORRECT environment variable is set, in which case the default becomes 512 bytes. That sentence in the manual is often skipped, and it is the reason the same du -s can differ by a factor of two between two machines. The -h option displays in powers of 1024 (1K is 1,024, 1M is 1,048,576); --si displays in powers of 1000 (1K is 1,000, 1M is 1,000,000), using the same unit letters, so check which system you are reading before trusting the number. The -B (--block-size) option sets the unit yourself: -B 1 counts bytes, -BM counts MiB, -B 1K counts 1,024-byte units. The -k option is -B 1K, -m is -B 1M, and -b is --apparent-size --block-size=1. The relationships are: -B sets the "ruler," --apparent-size sets "what to measure," and -h and --si are "convert the ruler into something a human can read." Keep those three jobs apart and du's units will never confuse you again.
Why does the manual open by calling du an "estimate" rather than a measurement? Because what du measures is not a property of a file alone, but a joint product of the file and its filesystem. The same file on a 4,096-byte-block filesystem is one number, and on a 1,024-byte-block filesystem it is another; a directory entry itself also takes blocks — on this machine, the src directory itself reports 0 blocks, so du -B 1 -s src gives 8,192, exactly one block per file, but on a filesystem like ext4 a directory usually takes a block of its own, counted in the directory's own total. Different filesystems and mount options (compression, deduplication) all change du's numbers for the same file. So always read du's numbers with the qualifier "on this machine, on this filesystem" attached; when comparing sizes across machines, align block size and filesystem type first, or you are comparing two different quantities.
When there are very many files, du has a less-traveled path: --files0-from. It reads a NUL-separated list of filenames from a file, with du --files0-from=list, or from standard input with du --files0-from=-. Together with -0 (which also separates output with NUL instead of newlines), it connects seamlessly to find -print0: find /var/log -name '*.log' -print0 | du --files0-from=- -c -h. Why go to this trouble? Because filenames can contain newlines, spaces, and non-ASCII characters, and a newline-separated list hands du one filename split into two, while a NUL-separated list never does. Everyday investigations do not need it, but when a script has to summarize hundreds of thousands of files one by one, this is the only robust way. The companions --exclude-from=FILE (exclusion rules in a file) and --time (with modification times) are batch options built for scripts in the same way.
Finally, put the four easily confused numbers in one table, using sparse.bin as the example: ls -l shows 104857600 (apparent size, in bytes); du -b shows 104857600 (the same as ls — apparent size); du -h shows 0 (disk usage); du --apparent-size -h shows 100M (apparent size, human-readable). And with a.txt: ls -l shows 100; du -b shows 100; du -h shows 4.0K; du -B 1 shows 4096. The rule is: ls -l and du -b are always equal (both apparent size), du and du -B 1 are always equal (both disk usage, only the unit differs), and --apparent-size is the switch between the two. The next time ls says 100M and du says 0, or a 100-byte file shows up as 4.0K in du, check this table first — the answer is in it, without opening the manual again.
One last word on speed. du computes live: it walks the whole directory tree, asking every file for its block count, and with hundreds of thousands of files it is slow — tens of seconds or minutes for du -sh on a huge tree. That is normal, not a hung command. For interactive drilling — looking and deleting by hand — ncdu is the interactive version of du: the same numbers, with a cursor and a delete key, made for cleaning a disk from a terminal. This guide does not cover ncdu's operation; one sentence is enough: du is for "measuring numbers into a script," ncdu is for "a person looking and cleaning by hand," and both measure the same thing. Which one to use depends on whether you want automation or hands-on work.
Here is a cheat sheet to keep by hand: du -sh <directory> for the total; du -h -d 1 <directory> for the breakdown; -d N to drill to N levels; -ah to list files too; -c for a grand total; -b or --apparent-size for apparent size; -B 1 for disk usage in bytes; --exclude='<pattern>' to leave directories out; -x to stay on one filesystem; -t 100M to find large files; --inodes -s when you suspect the inodes are exhausted. And the three easiest mistakes to remember: du measures disk usage by default, not file bytes; small files round up to whole blocks, so 100 bytes is 4.0K; and when ls -l and du disagree, first ask "one is apparent, one is disk usage," and only then ask anything else — that single question resolves most of the confusion people have with this command.
Boundary note: this guide covers only du (estimating file space usage), based on the man7.org du(1) manual and the GNU coreutils upstream documentation, in an environment running GNU coreutils 9.4 with a filesystem block size of 4,096 bytes. It is distinct from previously published guides as follows: the df guide covers filesystem-level free space, while du covers per-file usage distribution, and the three reasons the two disagree (deleted but still held, hidden under a mount point, rounding differences) are explained in this article; the free guide covers memory, and du never touches memory; the wc guide covers counting file contents (lines, words, bytes), and although du -b is also measured in bytes, it measures "how big" rather than "what is inside," so the two should not be mixed up; the find/xargs guide covers finding files by condition, while du's --exclude only excludes by name and does no conditional search; and the sort+uniq guide covers sorting and deduplication, while du does not sort at all — sorting its output belongs to sort. The demo script demo284_du.sh and its transcript demo284_du_transcript.txt are delivered with this guide; the script exits 0 and ends by printing --- done (work dir removed, no file left) ---, and its work directory /tmp/du-demo284 is removed when it ends, leaving nothing behind.
Disk usage is not only a server problem; it shows up constantly in everyday development work, and du is the tool that answers it there too. A build directory that grows with every run, a node_modules folder that nobody remembers installing, a Docker build context that takes ten minutes to send — all of them are du questions. In a continuous-integration pipeline, du -sh on the build artifacts tells you whether the artifact you are about to upload is 50MB or 500MB, and du -h -d 1 on the workspace tells you which step of the build produced the bulk of it. A common pattern is to run du -sh before and after a build step and compare the two numbers: if a step that should only produce a few megabytes adds hundreds, something is being copied that should not be — test fixtures, source maps, an entire dependency tree vendored into the output. The same pattern works locally: clone a repository, run du -sh, and you know immediately whether the checkout is a reasonable size or whether it carries a history of large binaries. None of this needs a monitoring dashboard. It needs one command, run twice, and a subtraction.
There is one more reason df and du disagree that deserves its own paragraph, because it confuses even experienced operators: reserved blocks. On ext4 filesystems, five percent of the blocks are reserved for the root user by default, so df reports "available" as less than "free" — the difference is space that exists but that ordinary users cannot use. A disk can show 0 bytes available in df while du insists there are still gigabytes of files that could be deleted, and both are right: the space is there, but it belongs to the reservation. The fix is not to delete files at random; it is to understand which number you are reading. du tells you how much the files take; df tells you how much the filesystem has left for you, after reservations and after filesystems overheads. Disk quotas add a similar wrinkle: a user can hit their quota while the filesystem as a whole is nearly empty, and du run as that user will show a small total while df shows a nearly empty disk. When the numbers do not add up, check the reservation and the quota before assuming either tool is lying — each one is reporting a different truth, and the investigation is a matter of finding which truth applies to your situation.
Used in scripts, du earns its keep in a different way: as a number that other tools can act on. A nightly job can run du -s --block-size=1M on a data directory and alert when the number crosses a threshold, which is simpler and more reliable than parsing df output for the same purpose, because du measures the thing you own — your directory — rather than the whole filesystem you share with everyone else. A backup script can run du -sh on the staging area before compressing it, and skip the upload when the staging area is empty. A deployment script can compare du -s before and after a release and roll back when the new release is unexpectedly large. The useful shape is always the same: du with -s or -B 1 for a single machine-readable number, no -h, no header, no extra lines — du -s --block-size=1 /var/data prints one number and one path, and the number is the whole interface. Pair it with --threshold for the filtering step, or with --time when the question is "what changed recently," and the script stays short: measure, compare, act. That is the whole Unix philosophy in one line — a small tool that prints one trustworthy number, composed with other small tools, each doing exactly one job well. The command's simplicity is the point. It does one thing, it does it the same way every time, and the number it prints is the same number a human would read off du -sh.

Comments (0)
Sources
Related articles
Which process is eating memory, who started whom, which one died but was never collected? ps answers all of it with one snapshot. This guide runs every command for real on procps-ng ps 4.0.4: default selection, Unix vs BSD vs GNU options, -o custom columns, --sort, --forest trees, the full STAT alphabet, a live zombie demo, and headerless output for scripts — plus the four classic traps.
How many lines, how many words, how many bytes — wc answers all three, and it is easy to get all three wrong. This guide runs every command for real on GNU coreutils wc 9.4: -l counts newlines (not lines), -c and -m diverge on Chinese text, --total=only, multi-file totals, and piping — with output pasted verbatim and the two classic traps explained.
Is there enough memory? free answers the question — and the column most people read is the least useful one. This guide runs every command for real on procps-ng free 4.0.4: total, used, free, shared, buff/cache and available, -w, -t, --si, -s sampling, and a live 200MB allocation that shows the kernel handing cache back. The rule to remember: judge memory by available, never by free.