Full lesson
Explore the full explanation, examples, and visuals at your own pace.
Same disk, two answers
df says 48 gigabytes are used, but du can only find 30. That's an 18 gigabyte gap on the same disk. These tools measure different things, so they can disagree, and that disagreement is the clue. Somewhere, space is held by something du can't see.
$ df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 48G 2.0G 96% /
$ sudo du -xsh /
30G /du follows names, df counts blocks
A filename is just a directory entry that links to an inode, and here that's inode 4711, which owns the 18G of blocks. du starts at names and follows them down. df skips the names and asks the filesystem how many blocks are allocated.
rm removed the name, not the blocks
Once someone runs rm, the app.log entry is deleted, so du has no name to follow and its walk dead-ends. But java PID 812 still holds inode 4711 open. The inode keeps owning its 18G of blocks, and df keeps counting them as used.
Two strings hold the balloon
Think of inode 4711 as a balloon held by two strings. One string is the name, app.log, and rm just snipped it. The other is java, PID 812, still gripping an open handle. The disk space is released only when the last string lets go.
Find the holder, then release it
Run lsof with plus L one on the root filesystem. It lists open files with no remaining name, along with the PID and file descriptor. Then restart the service to release the handle, or truncate through slash proc to free the blocks without downtime. Service name and fd number are illustrative.
# list open files with link count < 1
sudo lsof +L1 /
# fix A: restart the process holding it
sudo systemctl restart myapp
# fix B: empty it via the open descriptor
sudo truncate -s 0 /proc/812/fd/7Last handle closes, 18G returns
du walks names, and the app.log entry is gone, so it finds nothing. Java PID 812 still holds inode 4711 open, and that inode owns the 18G of blocks, which df counts. Restart the service and the inode has no links and no handles, so the blocks free. Any gap means space held without a name.
If lsof shows nothing
If lsof shows nothing, files may be hidden under a mount point, written before something was mounted over that directory. Bind-mount / elsewhere and rerun du to see them. Reserved blocks, often 5% on ext4, only lower Avail, not Used.
- Files hidden under a mount point
- Check: bind-mount / and rerun du
- Reserved blocks lower Avail, not Used
Space frees only when the last name and open handle let go.
Space frees only when the last name and the last open handle let go. So when df and du disagree, run lsof with plus L one before you delete anything else. Something may be holding blocks without a visible name.
