This post was originally published on go2linux.org on May 1, 2007. The domain is no longer mine, but I am the original author. I am republishing it here on garron.me with corrections and improvements.

When you need to know how much space a file or directory is using, du (Disk Usage) is the right tool. While df tells you how much space is free on a filesystem, du tells you what is using that space.

Basic usage

du [options] [file or directory]

With no arguments, du recursively lists the size of every file and directory in the current working directory. That is usually too much output. In practice you almost always combine it with options.

Most useful options

Human-readable sizes (-h):

$ du -h /var/log
4.0K    /var/log/apt/history.log
1.2M    /var/log/syslog
8.0K    /var/log/auth.log

Summarize a directory (-s):

$ du -sh /var/log
48M     /var/log

The -s flag suppresses the recursive listing and shows only the total. Combined with -h this is the quickest way to check the size of a directory.

Show totals for each item in a directory:

$ du -sh /var/*
4.0K    /var/backups
48M     /var/log
1.2G    /var/lib
120M    /var/cache

This is probably the command I use most when a disk is getting full and I need to find what is eating space.

Limit depth (--max-depth):

$ du -h --max-depth=1 /var
48M     /var/log
1.2G    /var/lib
120M    /var/cache
1.4G    /var

Useful when you want to drill down level by level without drowning in output.

Show grand total (-c):

$ du -sh -c /var/log /var/cache
48M     /var/log
120M    /var/cache
168M    total

Find the largest directories

This one-liner sorts the output of du to show the biggest directories first:

du -h --max-depth=1 /var | sort -rh | head -10

The sort -rh flag sorts human-readable sizes in reverse order (largest first). This is my go-to command when I need to find what is filling up a disk.

Other options

  • -a — show counts for all files, not just directories
  • -L — follow symbolic links and measure the target, not the link itself
  • -x — stay on one filesystem (useful to avoid crossing mount points)

du and df are a natural pair — use df -h to find which filesystem is full, then du -sh /path/* | sort -rh to find what is filling it.