The disk is full and nobody knows why. You delete the obvious things and
the number barely moves, so the next step is to stop guessing and measure.
The tool for that is du, which walks a directory, adds up the
blocks under every path, and prints the total. It's the command I run before
deleting anything, on a laptop and on a node, because it turns "something is
big" into a directory name you can act on.
Run du -sh path for
one total, and du -h --max-depth=1 dir | sort -h to rank the
directories inside it by size. The biggest offenders come out last, which is
where you start looking.
What a byte and a block actually are
A file's contents are measured in bytes. The filesystem hands out space
in blocks, and a block is much bigger. On this box it's 4096 bytes, so four
kilobytes. A file never gets part of a block, it gets whole blocks, and the
leftover space inside the last one is wasted.
Two different numbers are in play here and they're easy to confuse.
du prints in 1024 byte units by default, which is a display
choice it makes, not the block size of the disk. The filesystem's own block
size is whatever stat -f %S reports, and that varies by
filesystem. On this box both happen to land on four kilobytes.
$ stat -c '%s bytes, %b blocks of %B' f6.bin
6 bytes, 8 blocks of 512
$ du -B1 f6.bin
4096 f6.bin
Six bytes of content, four kilobytes of disk. stat reports
the size in bytes and the allocation in 512 byte sectors, which is why the
two numbers look unrelated. du adds the allocation up and
prints it in bytes, so 4096 is the honest answer to what deleting that file
frees.
The picture is easier than the arithmetic. Each cell below is one four
kilobyte block, and the shaded cells are the ones the file occupies.
Shaded cells are what the file occupies. One extra byte buys a whole second block.
Four kilobytes fits in one block.
One byte more needs a second block, so the file doubles on disk while its
content grew by a byte.
Twenty thousand bytes of content take five blocks and twenty thousand
four hundred and eighty bytes of disk. That's why a directory of a million
small files can be gigabytes even when the files themselves are tiny, and
why the count of files matters as much as their size.
Sparse files look like a bug the first time. A file can claim a size and
occupy nothing.
$ truncate -s 10M sparse.bin$ du -h sparse.bin
0 sparse.bin
$ du -h --apparent-size sparse.bin
10M sparse.bin
Ten megabytes of content, zero blocks allocated, because nothing has been
written into it yet. --apparent-size prints the length of the
content, the same number ls -l shows, which is the right figure
when you're comparing against a transferred byte count. It's the wrong
figure for predicting what a copy will occupy, because the holes come back
the moment the data is written out.
du -sh gives you one number
Without flags, du prints a line for every directory it walks.
-s is the summary, so you get one line per argument.
$ du -sh .
2.6M .
-h makes it readable, -s makes it one line, and
together they're the flag pair I type most often. Two point six megabytes
for that project, and now the question is which part of it.
Break it down one level
--max-depth is what turns a single total into a diagnosis of
where the space went. One level, sorted by size, and the biggest directory
is the last line.
The last line is the same total du -sh would print, so the
rows above it need not add up to it. Files sitting directly in the root
belong to the total and to no row. sort -h compares the printed
strings, so ties are ordered by nothing meaningful, which means the biggest
is reliably last and the middle of the list isn't. Here
build is two megabytes of the two point six, so the build
output is the answer and the source tree isn't.
The glob form does the same job in a directory you're already standing
in, and it skips the dot directories, which is often what you want.
On this box the question came up about a tool's data directory. One total
says nine point six gigabytes, which is more than everything else in the
home directory put together, so the obvious assumption is that the content is
heavy.
$ du -sh ~/.hermes
9.6G /home/deploy/.hermes
$ du -sh ~/.hermes/skills
80M /home/deploy/.hermes/skills
Eighty megabytes. The thing I assumed was the bulk is under one percent of
the directory, and nine gigabytes live somewhere else inside it. That's the
whole value of measuring first. Without the second command I would have
deleted the wrong thing and freed almost nothing.
Then one level up, to see whether the nine gigabytes is even unusual.
Sixty two gigabytes in a home directory, thirty one of it repositories and
most of the rest that data directory. Nothing is broken. The machine is simply full
of work, and the disk still has terabytes free.
du slows down on some paths
du has to stat every file it counts, so it costs what the
tree costs. -x is the only flag in this article that needs a
warning, because it's easy to overuse. It keeps the walk on the filesystem
the starting point lives on, which means it hides a bind mount sitting
inside the tree. A home directory usually has one and you want to see it.
Pass -x when you're auditing one volume and specifically do
not want the mounts inside it counted, and give it a starting point on that
volume, because du -x / skips every mount and the total is then
not how full the disk is. On a network filesystem du is slow
either way, so measure the mount point only when you have to.
$ du -sh -x /var/log 2>/dev/null
1.3G /var/log
One point three gigabytes of readable logs on this box, and the flag
means the number covers only that filesystem. That figure is a partial sum.
Three directories under /var/log are owned by root, this run
couldn't read them, and 2>/dev/null hid the complaints
along with the exit status, which is one. Rerun without the redirect to see
what it skipped, or run it under sudo for a complete number. A
plain du on / for a full picture can take minutes
on a machine with several mounts, and half of that time is spent crossing
into directories you were not asking about.
Which one to use, at a glance
Command
What it does
Reach for it when
du -sh path
One readable total
You want to know how big a directory is
du -h --max-depth=1 dir | sort -h
Sizes one level down, smallest first
You need to know which subdirectory is the problem
du -sh * | sort -h
Sizes everything in the current directory
You are already standing in the folder
du -sh --apparent-size path
Content size instead of block usage
You are copying data, not reclaiming space
du -sh -x /
Stays on one filesystem
The tree has mounts inside it and you want one of them
du -a | sort -n | tail -5
Sizes files as well as directories
A single enormous file is the suspected cause. The last line is always the grand total, so read the line above it
Verify it works
Build a small tree with known sizes, then check that the totals line up
with what you created.
$ mkdir -p proj/build proj/src proj/logs$ head -c 2000000 /dev/zero > proj/build/app.bin$ head -c 500000 /dev/zero > proj/src/main.js$ head -c 100000 /dev/zero > proj/logs/app.log$ du -sh proj
2.6M proj
$ du -h --max-depth=1 proj | sort -h
104K proj/logs
496K proj/src
2.0M proj/build
2.6M proj
Two megabytes of binary, half a megabyte of source, a hundred kilobytes of
log, and a total of two point six megabytes. The numbers reconcile, and the
breakdown points at build immediately. If they don't reconcile
on your box, look for sparse files or a mount point inside the tree.
The honest caveat
du and a filesystem can disagree about the same directory,
and both can be right. Hard links are counted once per inode, whichever
path du reaches first, and -l is the flag that
counts every link separately, which overstates a tree that holds all of
them. Sparse files occupy less
disk than their apparent size suggests, and a deleted file that a process
still holds open has no name for du to walk but its blocks are
still in use. That last one is the classic reason deleting doesn't free
space, and the fix is finding the process rather than deleting more.
The other difference is that du answers a different
question from df. du adds up what the paths
contain, df reports what the filesystem has left. When you need
the second one, that's df.
$ lsof +L1 2>/dev/null | head -3
Every line with a deleted marker is a file whose blocks are still
allocated with no name attached. Restart that process and the space comes
back.
The boring conclusion
One total with du -sh, one level of detail with
--max-depth=1 and a sort, and you know what to delete instead of
guessing. Measure while there's still room to think.
When the volume is already at 100 percent and writes are failing, free
something before you measure, because the walk itself thrashes on a full
disk. Truncating a service's log through its open descriptor works, but
never do it to journald, which keeps its own index. There you use
journalctl --vacuum-size=200M.
What's the directory that turns out to be huge every single time?
Frequently asked questions
How do I check disk usage in Linux?
Run du -sh path for one readable total. For a breakdown, run du -h with a max depth of one and pipe the output through sort -h, then read the last lines because the biggest directory sorts to the bottom.
What does du -sh mean?
du is disk usage, -s is summary so you get one line instead of one per directory, and -h makes the sizes readable. Put together they answer how big is this directory in one command.
Why is du bigger than the file sizes?
Because du counts blocks, not bytes. The filesystem hands out space in four kilobyte blocks, so a six byte file still occupies one. That's why a directory of tiny files can be far larger than the sum of their contents.
How do I find the biggest directory?
Run du -h with a max depth of one, pipe it through sort -h, and read the last lines. Add more depth to go further down the tree once you know which branch is heavy.
Why does du not free space after I delete files?
A process is probably still holding a deleted file open, so the blocks aren't released until it closes the handle. Run lsof +L1 and look for deleted entries, then restart the process that owns them.
How do I see which subdirectory is using the most space?
Run du with a readable size and a depth of one, pipe the output through sort, and read the last line. The biggest subdirectory sorts to the bottom, and the last line is the total for the directory you named.
Why does du not free space after I delete files?
A process is still holding a deleted file open, so its blocks stay allocated with no name in the tree. Run lsof +L1 and look for an entry ending in (deleted). Truncate through the descriptor with truncate -s 0 /proc/PID/fd/N, or reload the service, then confirm with df.
What is the difference between du and df?
du adds up the blocks used by the paths you give it. df reports used and free space for a whole mounted filesystem. Use du to find what is big and df to find out how much room is left.
Resources
The reference the command follows, including the block size rules behind
that six byte file taking four kilobytes.