Sub topic 5
Drilling down with du
This chapter assumes you can read df output (covered earlier), use sudo, and pipe commands together in a shell. df tells you which filesystem is filling up. du tells you which directories on that filesystem are responsible.
What du measures
du (disk usage) walks a directory tree and adds up the space used by the files it finds. df asks the filesystem for its totals. du computes its numbers by visiting every file, so it is slower, and it can only count what it can see and read.
By default du reports allocated disk blocks, not the apparent length of each file. The two differ in these cases:
- A small file still occupies at least one whole block (commonly 4 KiB), so a 10-byte file shows as 4 KiB.
- A sparse file, such as a VM disk image, can have a large apparent size but few allocated blocks.
- Compressed or deduplicated storage (Btrfs, ZFS) can make the numbers differ further.
Use --apparent-size to see the byte length instead of the allocated blocks. For finding what fills a disk, the default (allocated blocks) is what you want.
The core options
| Option | Meaning |
|---|---|
| `-h` | Human-readable sizes (K, M, G) |
| `-s` | Print only a total for each argument |
| `-x` | Stay on one filesystem; do not cross mount points |
| `--max-depth=N` (or `-d N`) | Show totals only down to N levels |
| `-a` | Include files, not only directories |
| `-c` | Add a grand total line |
| `--exclude=PATTERN` | Skip paths that match a pattern |
The most important option on a server is -x. Without it, du / descends into /proc, /mnt, network mounts and other filesystems. You get slow runs, misleading totals, and sometimes hangs on a stalled NFS mount.
A first pass: one level at a time
Start at the top of the full filesystem and go one level deep. Then repeat inside the largest directory. This is the drill-down in the chapter title.
sudo du -xh --max-depth=1 /var 2>/dev/null | sort -h | tail -n 10Each part of this pipeline has a job:
sudoletsduread directories your user cannot enter. Without it, you undercount.-xkeeps the walk on the filesystem that holds/var.--max-depth=1prints one line per direct child of/var, plus one line for/varitself.2>/dev/nullhides "Permission denied" and "No such file" messages. Files can vanish whileduruns.sort -hsorts human-readable sizes correctly, so900Mcomes before2.1G. Plainsort -nwould get this wrong.tail -n 10keeps the ten largest entries, becausesortputs the largest last.
A typical result looks like this:
48M /var/cache
310M /var/lib/docker
1.2G /var/lib
6.8G /var/log
8.4G /varThe last line is /var itself, which is the sum of its children. Here /var/log accounts for most of the space, so the next step is to go inside it:
sudo du -xh --max-depth=1 /var/log 2>/dev/null | sort -h | tail -n 10Repeat until you reach a single file or a directory you recognise as the cause.
Summarising with -s
When you only want a single number for a path, use -s:
sudo du -sh /var/log /var/lib /home 2>/dev/nullThis prints one total per argument. It is useful for comparing a few known suspects. For discovery, --max-depth=1 is better because it shows the children, and you do not need to know the names in advance.
-s is equivalent to --max-depth=0.
Seeing individual files with -a
By default du prints only directories. If one huge file hides in a directory of many files, add -a:
sudo du -xah /var/log 2>/dev/null | sort -h | tail -n 15The output mixes files and directories, so a directory total and the file inside it can both appear near the bottom. That duplication is expected. For a files-only search across the whole filesystem, find is usually better. The next chapter covers it.
Comparing du totals with df
After you drill down, add up what you found and compare it with df. Take the -x total for the mount point:
df -h /var
sudo du -xsh /var 2>/dev/nullThe two numbers rarely match exactly. Small gaps are normal because of filesystem metadata, journals, and reserved blocks. A large gap, where df reports far more used space than du can find, has specific causes:
- Deleted files that a process still holds open (covered in a later chapter).
- A mount hiding files. If you mounted a filesystem over a directory that already contained files, those files still use space on the underlying filesystem but are invisible under the mount.
- Files you could not read, because you ran
duwithoutsudoor the entries are inside a restricted path. - Snapshots or subvolumes on Btrfs or LVM that
dudoes not walk.
Finding files hidden under a mount
The hidden-mount case deserves a technique, since it comes up after someone mounts a data disk onto a directory that already had content. A bind mount of the root filesystem exposes the underlying directories without the mounts on top of them:
sudo mkdir -p /mnt/rootview
sudo mount --bind / /mnt/rootview
sudo du -xh --max-depth=1 /mnt/rootview 2>/dev/null | sort -h | tail -n 10
sudo umount /mnt/rootviewA bind mount of / shows only the root filesystem's own contents. Anything mounted on top of a directory, such as /data, is not carried across. If /mnt/rootview/data contains gigabytes while /data shows another filesystem, you have found the hidden files.
Excluding paths
Sometimes you know a directory is expected to be large and want to see everything else. Use --exclude:
sudo du -xh --max-depth=1 --exclude='/var/lib/docker' /var 2>/dev/null | sort -h | tail -n 5The pattern is matched as a shell glob against file names and paths. Test it on a small directory first, since an exclude that does not match fails silently.
Counting hard links
du counts a file with several hard links only once per run, under the first path it meets. Backup tools that use hard links to share unchanged files (for example rsync --link-dest) can make one directory look small and another look large, depending on the order of traversal. If a total looks wrong on such a backup tree, run du on a single snapshot directory on its own.
Speed and load
On a filesystem with millions of files, du can take minutes and generate heavy read I/O, which hurts a busy server. Lower its priority:
sudo ionice -c3 nice -n 19 du -xh --max-depth=1 /var 2>/dev/null | sort -h | tail -n 10ionice -c3 runs du in the idle I/O class, so it reads from the disk only when nothing else needs it. nice -n 19 gives it the lowest CPU priority. Repeating a walk is quicker the second time because the kernel caches directory entries in memory.
Interactive tools such as ncdu do the same walk and let you browse the result. They are convenient, but they are often not installed on a server, and du is always there.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Totals are much lower than `df` | Ran without `sudo`, or files hidden under a mount | Use `sudo`; try the bind-mount check |
| `du` hangs | Walking into a stalled network mount | Add `-x` |
| Sort order looks wrong (`9M` after `1G`) | Used `sort -n` on human-readable sizes | Use `sort -h`, or drop `-h` from `du` and use `sort -n` |
| Same directory listed twice | A bind mount or symlinked path | Use `-x`; avoid `-L` unless you intend to follow links |
| Numbers differ between runs | Files are being written or rotated during the walk | Repeat the run; treat the result as approximate |
Putting it together
A reliable routine for a full filesystem:
- Run
df -hTand identify the mount that is full. - Run
sudo du -xh --max-depth=1 <mount> | sort -h | tailon that mount. - Descend into the largest directory and repeat.
- When you reach a candidate, check what owns it (a service, a user, a package) before touching it.
- Compare your total against
df. If a large gap remains, check for deleted-but-open files and hidden mounts.
Exercise
You run sudo du -h --max-depth=1 / | sort -h | tail -n 5 and it takes ten minutes, and the largest entries include /proc and /mnt/backup, which is an NFS mount. What option is missing, and why does it help?
Show answer
The -x option is missing. It keeps du on the root filesystem, so it does not descend into /proc (a virtual filesystem), /mnt/backup (a network filesystem), or any other mount. The run becomes faster, avoids hangs on stalled mounts, and reports only the space used on the filesystem you are investigating.
Exercise
df -h /data reports 180 GB used, but sudo du -xsh /data reports 4 GB. Name two causes you would check and one command for each.
Show answer
First, a deleted file still held open by a process: run sudo lsof -nP +L1 to list open files with a link count below one. Second, files hidden underneath the mount point: bind-mount / at a temporary directory with sudo mount --bind / /mnt/rootview and run sudo du -xh --max-depth=1 /mnt/rootview/data. Unmount it afterwards.