Sub topic 5
Drilling Down with `du`
Assume you are comfortable with a Linux shell, files, directories, permissions, sudo, and basic pipes. You should also know that df reports usage for an entire mounted filesystem, while du adds the disk usage of files and directories.
What `du` measures
du means “disk usage.” It walks through a directory tree and totals the space used by the files it can see. This helps you answer questions such as:
- Which directory under
/varis using the most space? - Is
/var/log,/var/lib, or/var/cachegrowing? - Which individual files are unusually large?
The result can differ from df. The df command reads filesystem accounting data. The du command examines directory entries. A deleted file still held open by a process appears in df but not in du.
Start with a shallow summary
Use -h for human-readable units and --max-depth=1 to show each directory directly below the target:
sudo du -xh --max-depth=1 /var 2>/dev/null | sort -h | tail -n 10The command suppresses permission errors, sorts results by size, and displays the ten largest entries. A result might look like this:
| Size | Directory |
|---|---|
| 1.2G | `/var/cache` |
| 4.8G | `/var/log` |
| 38G | `/var/lib` |
The final line for /var is the total reported for the directory tree. If a subdirectory is a separate mount, its contents can make interpretation confusing.
Stay on one filesystem
Use -x when investigating one filesystem:
sudo du -xh -x --max-depth=1 /var 2>/dev/null | sort -h | tail -n 10The -x option prevents du from crossing into other mounted filesystems. Without it, a mounted database volume, network share, or temporary filesystem below /var may be included in the total.
The two options below solve different problems:
| Option | Purpose |
|---|---|
| `--max-depth=1` | Limits how many directory levels are displayed |
| `-x` | Prevents crossing filesystem boundaries |
Narrow the search
After identifying a large directory, run du again at a greater depth:
sudo du -xh -x --max-depth=2 /var/lib 2>/dev/null | sort -h | tail -n 20Repeat this process until you reach the directory or application responsible for the growth. Avoid starting with a deep scan of / on a busy server. It can take time and may produce many permission messages.
A directory’s du total includes the files beneath it. A large total does not mean the directory itself is large. Continue drilling down into its children.
Common errors
du may under-report usage when you lack permission to read a directory. Use sudo for system paths, but do not hide errors until you understand the result.
Hard links can cause a file to appear in more than one directory while consuming blocks only once. Sparse files can also have a large apparent size but use fewer physical blocks. Use du as an investigation tool, then compare suspicious results with df, open-file checks, and service knowledge.
Exercise
Why should you use du -x while investigating /var on a server with several mounted filesystems?
Show answer
It keeps the calculation on the filesystem containing /var, so other mounts do not hide which part of that filesystem is consuming space.