Save 10%Pay yearly on any plan. 6% half-yearly, 3% quarterly. Help centreWhatsAppTelegram TeamsOpen a ticket

Monitoring Disk Space Usage in Linux

Check how full a Linux disk is with df, find what fills it with du and ncdu, free space safely, and get a warning before a server runs out.

3 min readBy Daniel

Reviewed by the AmazingRDP technical team

Monitoring disk space usage in Linux

Disk too small? A Linux VPS can be moved to a bigger plan by ticket. See Linux VPS from $6.99

A full disk is one of the commonest reasons a Linux server stops working: databases refuse to write, logins fail, updates break halfway. Two commands answer the two questions that matter. df says how full each disk is. du says what is using the space.

How full? df

df -h

-h prints sizes in units people read. One line per filesystem: size, used, available, percentage, and where it is mounted. Look at the line whose last column is /, and at any separate lines for /home, /var or data disks. Lines starting with tmpfs are memory, not disk; hide them with:

df -h -x tmpfs -x devtmpfs
df -h /var/log

The second form answers for one path: which filesystem holds it and how full that is.

What is using it? du

sudo du -sh /var
sudo du -h --max-depth=1 /var | sort -h
sudo du -xh --max-depth=1 / | sort -h | tail
  • The first gives one total for a directory.
  • The second lists its sub-directories, smallest to largest.
  • The third does the same for the whole system; -x stays on one filesystem, so other disks and network shares are not counted in.

Repeat the second line one level deeper each time, following the largest entry, and within a minute you are standing in front of the culprit.

The comfortable way: ncdu

sudo apt install ncdu
sudo ncdu -x /

ncdu scans once and then lets you walk through the result with the arrow keys, largest first. q quits. On the Red Hat family: sudo dnf install ncdu.

Find single large files

sudo find / -xdev -type f -size +500M -exec ls -lh {} \;

Every file above 500 MB on the root filesystem.

The usual suspects, and how to clear them safely

WhereWhatClear with
/var/log/journalThe system journalsudo journalctl --vacuum-size=200M
/var/logLog files of applicationsCheck that logrotate covers them. Empty a live log by cutting it to zero length, not by erasing the file, or the program keeps writing into a file you can no longer see. See reading logs with tail
/var/cache/aptDownloaded packagessudo apt clean
Old kernels and unused packagessudo apt autoremove
/var/lib/dockerImages, stopped containers, build cachedocker system df shows what takes the room; Docker's own clean-up commands clear it
/var/lib/snapdOld versions of snap packagessudo snap set system refresh.retain=2
/tmp, /var/tmpLeftoversA restart clears /tmp
Home folders and backup foldersForgotten archives and database dumpsMove them off the server

When df and du disagree

df says the disk is full, and du cannot find the data. Two causes account for nearly every case.

Erased files that are still open

A file erased while a program has it open disappears from the directory, but its space is freed only when the program closes it. Typical: someone erases a huge log file and the service keeps writing into it.

sudo lsof +L1

This lists such files with the program that holds them. Restart that service and the space returns.

Out of inodes

Each file needs an index entry, an inode, and their number is fixed when the filesystem is made. Millions of tiny files, often session or cache files, can use them all while gigabytes are still free. The error is the same "No space left on device".

df -i

If IUse% is at 100, some directory holds an enormous number of small files. Count files per directory under the likely places (/var, /tmp, application cache folders) with sudo du --inodes -x --max-depth=2 /var | sort -n | tail and clear the one that stands out.

Get a warning in time

A small script, run daily by cron, that speaks up above 85 percent:

#!/bin/sh
USE=$(df --output=pcent / | tail -1 | tr -dc 0-9)
if [ "$USE" -ge 85 ]; then
  echo "Disk on $(hostname) is at $USE percent" | mail -s "Disk warning" you@example.com
fi

It needs a working mail command; any other way of sending a message will do in its place. Setting it up: running a shell script and scheduling with cron.

When cleaning is not enough

  • With LVM, a volume can be enlarged without downtime: see setting up LVM.
  • Which disk is mounted where: findmnt.
  • On a VPS, the disk grows with the plan. Ours can be moved to a bigger one by ticket; see the Linux VPS plans.

The Windows counterparts: Storage Sense and extending drive C:. For processor and memory in the same spirit: monitoring with htop.

Questions

How do I check disk space in Linux?

Run df -h. It shows the size, use and free space of every filesystem.

How do I find what is using disk space?

Run sudo du -h --max-depth=1 /var | sort -h and follow the largest entry down, or use ncdu.

Why is the disk full when du shows free space?

Either erased files are still held open by a program (sudo lsof +L1), or the filesystem has run out of inodes (df -i).

How do I free disk space on Ubuntu?

Shrink the journal with journalctl --vacuum-size, run sudo apt clean and sudo apt autoremove, and clear old logs, caches and unused Docker data.