Find the largest files on disk
Problem: Disk space is running low and you need to find out what's using it.
Solution:
du -ah /var | sort -rh | head -20
2.5M /var
2.0M /var/cache/big.dat
2.0M /var/cache
504K /var/log
500K /var/log/syslog
8.0K /var/small.txt
How it works:
du -ah /varprints the disk usage of every file and directory under/var, in human-readable sizes (-h), including individual files, not just directory totals (-a).sort -rhsorts that output by size, largest first (-rreverse,-hunderstands human-readable sizes like "1.2G").head -20keeps just the top 20 results.
Variations:
du -ah . | sort -rh | head -20 # current directory instead of the whole filesystem
du -sh */ # top-level directories only, not every file
find /var -size +500k -exec ls -lh {} \; # only files over a size threshold
Restricting to top-level directories first (du -sh */) is often the faster starting point on
a large filesystem: it tells you which subtree to dig into with the full du -ah command,
rather than sorting through every individual file up front.
If the culprit turns out to be an old backup archive rather than a single runaway file, checking
what's actually inside it before deleting is worth the extra step: tar -tzvf backup.tar.gz
lists contents with sizes without extracting anything (see tar). For repeated
disk investigations, the interactive tool ncdu (not installed by default; apt install ncdu)
gives the same information as the du command above but lets you navigate the tree interactively
instead of re-running the command with a different path each time.