Sub topic 8
When `df` and `du` Disagree: Deleted Files Still Held Open
What `df` Measures and What `du` Measures
Assume you are comfortable with a Linux shell, sudo, file permissions, pipes, and basic shell scripts.
df reports filesystem-level usage. It asks the filesystem how many data blocks are allocated on each mounted filesystem.
du walks through directory entries and adds the sizes of files it can find. It reports usage from a directory tree.
These measurements can differ for several reasons, including separate mount points, permission errors, sparse files, and files that were deleted while a process still had them open. This chapter focuses on the last case.
How a Deleted File Can Still Use Space
On Linux, deleting a file usually removes its directory entry. The file's data is not immediately released if a running process still has the file open.
The kernel keeps the file's inode and data blocks until both conditions are true:
- No directory entry points to the file.
- No process has the file open.
A common example is a service that writes to a log file. An administrator removes the log file to recover space, but the service continues writing through its already-open file descriptor. du no longer sees the file, but df still counts its blocks.
Diagnose the Difference
First compare the filesystem and directory totals. Run du on the same filesystem that df reports, and avoid crossing into other mounts when necessary.
The lsof command lists open files. The +L1 filter selects files whose link count is below one. These files have no remaining directory link but are still open.
df -hT /var
sudo du -xsh /var
sudo lsof -nP +L1Look for entries with (deleted) in the NAME column. Important fields include:
COMMAND: the process name.PID: the process identifier.USER: the account running the process.FD: the file descriptor, such as7w.SIZE/OFF: the file size or current offset.NAME: the original path, often ending in(deleted).
If lsof is unavailable, inspect the process descriptors directly:
pid=1234
sudo ls -l /proc/"$pid"/fd | grep deletedThe size shown by df can remain high until the process closes that descriptor. The process might need a restart, a log-reopen signal, or a configuration change.
Recover the Space Safely
Identify the owning service before taking action. Check the process command line and its systemd unit:
sudo ps -fp 1234
sudo systemctl status example.servicePrefer the least disruptive supported action:
- Use the service's documented log-reopen signal if it supports one.
- Ask systemd to restart the service during an approved maintenance window.
- Change log rotation so the service closes or reopens rotated files correctly.
- Verify recovery with
df -hTandlsof -nP +L1.
Do not remove files from /proc/PID/fd or truncate an unknown descriptor. Those actions can corrupt application output or interrupt a service. Restarting a service also releases its memory and file descriptors, but it may interrupt active requests.
Exercise
du -sh /var reports 12 GB, while df -h /var reports 20 GB used. lsof -nP +L1 shows a deleted 8 GB log held by worker with PID 1234. What is the safest next step?
Show answer
Identify the owning service, then use its supported log-reopen action or perform a planned restart. Verify afterward that df decreases and the deleted-open entry disappears.