Back to lessons

Web Server Rescue

Find Large Directories with du

You need to identify which top-level directory is using the most space.

Command

du -xh --max-depth=1 /var 2>/dev/null | sort -h

What changed

Nothing changes. The command measures directory sizes and sorts them.

Danger

safe

When to use it

Use after df -h shows a full filesystem and you need to locate the largest directory.

When not to use it

Do not start at / on a busy production system without narrowing the path first.

Undo or recovery

No undo needed because this command is read-only.

Expected output

A size-sorted list of directories under /var, with the largest entries at the bottom.

demo script

Disposable terminal steps

  1. df -h
  2. du -xh --max-depth=1 /var 2>/dev/null | sort -h

simulated output

What it looks like

disposable vessel
::fixture-ready::
$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        25G   23G  1.1G  96% /
tmpfs           982M   12M  970M   2% /run
::exit-code::0
$ du -xh --max-depth=1 /var 2>/dev/null | sort -h
24M	/var/tmp
180M	/var/cache
1.4G	/var/log
3.8G	/var/lib
::exit-code::0

YouTube Short

df says full. du says where.

After df identifies the full filesystem, use a targeted du scan to find the large directory.

LinkedIn hook

Once you know a filesystem is full, the next question is where.

Question: Do you start disk hunts at /, or narrow the path first?

experiments

A/B tests to run

Metric: completion_rate

A: Start narrow.

B: df then du.