du: show disk usage of files and directories
du adds up how much disk space files and directories use. When a disk is full, df tells you which file system, and du tells you which directories are responsible.
du [options] [file or directory ...]
Common options
| Option | Meaning |
|---|---|
-h | Human-readable sizes (K, M, G) |
-s | Only a total for each argument |
-d N, --max-depth=N | Show directories only N levels deep |
-x | Stay on one file system |
-a | Show files as well as directories |
-c | Print a grand total |
--exclude=PATTERN | Skip matching files (GNU) |
Examples
du -sh /var/log # total size of one directory
du -sh * # size of everything in the current directory
du -h -d 1 /var | sort -h # each directory in /var, smallest to largest
sudo du -xh -d 1 / | sort -h | tail -15 # the biggest top-level directories on /
du -ah /srv/uploads | sort -h | tail -20 # the 20 biggest files and directories
du -sh --exclude='*.iso' ~/Downloads
sort -h understands sizes such as 1.5G and 300M; see sort.
Hunting down a full disk
df -hto see which file system is full.sudo du -xh -d 1 /mountpoint | sort -hto find the biggest directory.- Repeat one level deeper until you find the culprit: usually logs, caches, old backups, core dumps or container images.
- For big single files:
sudo find /mountpoint -xdev -type f -size +1G -exec ls -lh {} +.
For interactive browsing, install ncdu: sudo ncdu -x / lets you walk through directories sorted by size and delete from inside the tool.
When du and df disagree
- Deleted files still open: a program keeps writing to a log you removed.
dfcounts the space,ducannot see the file. Find them withsudo lsof +L1and restart the program. - Files hidden under a mount point: data written to
/databefore a disk was mounted there is invisible todubut uses space on the parent file system. - Reserved blocks: ext4 reserves 5% for root by default, which
dfdoes not show as available. - Snapshots and compression on btrfs and ZFS: use
btrfs filesystem duorzfs list -o spacefor accurate numbers.
macOS and BSD du support -h, -s, -d and -x but not the GNU long options.