cheat-sheets

Disk Usage

Find out what is filling a disk with df, du, ncdu and lsof. Start with df to see which filesystem is full, then du or ncdu to find the directory, and check for deleted files that are still held open.

Documentation: df(1) · du(1) · ncdu

Verification: options were checked against the GNU coreutils man pages and ncdu 1.19 in Ubuntu 24.04. Examples were not run against a full disk.

Filesystem Space (df) #

COMMANDDESCRIPTION
df -hUsed and free space per filesystem, human-readable
df -hTAdd the filesystem type
df -iInode usage (a full inode table looks like a full disk)
df -h /varThe filesystem holding a path
df -h -x tmpfs -x devtmpfsHide virtual filesystems
findmntMount tree with sources and options

Directory Size (du) #

COMMANDDESCRIPTION
du -sh <PATH>Total size of one path
du -h --max-depth=1 <PATH>Size of each immediate child
du -h --max-depth=1 <PATH> | sort -rh | head -20Largest children first
sudo du -xh --max-depth=1 /Stay on one filesystem (-x), skipping other mounts
du -ah <PATH> | sort -rh | head -20Largest files and directories
du -sh --apparent-size <FILE>Logical size rather than blocks used (sparse files)
du -sh --exclude='*.log' <PATH>Skip a pattern

Find Large Files #

COMMANDDESCRIPTION
find <PATH> -xdev -type f -size +500MFiles over 500 MiB on one filesystem
find <PATH> -xdev -type f -size +500M -exec ls -lh {} +Same, with sizes
find <PATH> -xdev -type f -mtime +365Files not modified for a year
sudo ls -lSh /var/log | headLargest files in a directory, biggest first

ncdu #

Interactive browser. Press d to delete (it asks first), q to quit.

COMMANDDESCRIPTION
ncdu <PATH>Scan and browse
ncdu -x <PATH>Stay on one filesystem
sudo ncdu -x /Whole root filesystem
ncdu -o <FILE> <PATH>Save a scan to a file
ncdu -f <FILE>Browse a saved scan (scan a server, browse elsewhere)

Space Not Freed After Deleting #

COMMANDDESCRIPTION
sudo lsof +L1Open files with a link count below 1: deleted but still held by a process
sudo lsof -nP +L1 | sort -k7 -rn | headBiggest of those first (size is column 7)
sudo systemctl restart <SERVICE>Releasing the file usually means restarting its owner
: > <FILE>Truncate a log in place instead of deleting it (if running as the right user)

Common Culprits #

LOCATIONCLEAN WITH
/var/log, journaljournalctl --disk-usage, then sudo journalctl --vacuum-size=500M
Dockerdocker system df, see Docker Cleanup
APT cachesudo apt clean
Old kernelssudo apt autoremove
Snapshots (ZFS, LVM)zfs list -t snapshot, see ZFS; lvs for LVM, see LVM

Troubleshooting #

SYMPTOMFIX
No space left on device but df -h shows free spaceOut of inodes: df -i; look for directories with millions of small files
df and du disagreeDeleted-but-open files (lsof +L1), or data hidden under a mount point
du slow on a network shareRun it on the file server, or use ncdu -o there

See Also #

Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.