Posted by

Find What Is Filling Your Linux Server Disk: df, du, ncdu, Deleted Open Files and Inodes

Sooner or later every server shows the same error: No space left on device. Databases stop writing, uploads fail, services crash on start, and sometimes you cannot even log in properly. The fix is rarely hard, but you need to find the real cause first. This guide walks through a calm, repeatable process on Ubuntu and Debian servers using standard tools: df, du, ncdu, lsof and find.

Step 1: See which filesystem is full with df

Start with df, which reports space per mounted filesystem:

df -hT -x tmpfs -x devtmpfs
  • -h prints human-readable sizes (powers of 1024, such as 20G).
  • -T adds the filesystem type (ext4, xfs and so on).
  • -x tmpfs -x devtmpfs hides memory-backed filesystems that only add noise.

Look at the Use% and Mounted on columns. A full / is a different problem from a full /var or /boot, so note the exact mount point. All the searches below should stay on that one filesystem.

Why Size is not Used + Avail

On ext4, Used plus Avail is often smaller than Size. That is normal: ext4 reserves a share of blocks (5% by default when the filesystem is created) for the root user, so the system and root processes can keep working when ordinary users fill the disk. You can check the reserve with:

sudo tune2fs -l /dev/sda1 | grep -i "reserved block count"

Replace /dev/sda1 with the device shown by df. Do not lower this reserve as a "fix" for a full root filesystem; find what is using the space instead.

Step 2: Check inodes with df -i

If an application says the disk is full but df -h shows free space, you may have run out of inodes. Every file and directory uses one inode, and on ext4 the number of inodes is fixed when the filesystem is created. Millions of tiny files can use them all up while gigabytes stay free.

df -i

If IUse% is at or near 100%, skip to Step 6.

Step 3: Find the largest directories with du

du adds up how much space files and directories use. Walk down one level at a time, largest last:

sudo du -xh --max-depth=1 / 2>/dev/null | sort -h
  • -x (--one-file-system) skips directories on other filesystems, so /proc, other disks and network mounts are not counted.
  • -h gives readable sizes, and sort -h sorts those human-readable numbers correctly.
  • --max-depth=1 (short form -d 1) shows only the first level.
  • 2>/dev/null hides "Permission denied" and vanished-file messages.

Then repeat on the biggest result, for example /var, then /var/log or /var/lib, until you reach the real culprit:

sudo du -xh --max-depth=1 /var 2>/dev/null | sort -h | tail -n 15

Find single large files

Sometimes the problem is one huge file, such as a forgotten backup archive or a runaway log:

sudo find / -xdev -type f -size +500M -exec ls -lh {} + 2>/dev/null

-xdev keeps find on the same filesystem, just like du -x.

Use ncdu for an interactive view

If you prefer to browse, ncdu is an interactive disk usage viewer in the Ubuntu and Debian repositories:

sudo apt install ncdu
sudo ncdu -x /

Use the arrow keys and Enter to move through folders, sorted by size. ncdu can also delete files, so read the path twice before you press anything, and press q to quit. On a completely full disk, apt install may fail; use du first to free a little space.

Step 4: Catch deleted files that are still open

A classic trap: you delete a large log file, but df still shows the disk as full. On Linux, deleting a file only removes its name. The space is not released while any process still has the file open, and du cannot see it because there is no longer a path to count. This is the usual reason why df and du disagree.

List open files that have been unlinked (deleted):

sudo lsof +L1

The output shows the process name, PID and size of each deleted-but-open file. To release the space, restart the process that holds it, for example:

sudo systemctl restart nginx

If you cannot restart the service right now, you can empty the file through the process's file descriptor. Take the PID and FD number from the lsof output (the FD column shows something like 4w; use the number only):

sudo truncate -s 0 /proc/PID/fd/FD

Only do this for files you are sure are disposable, such as old logs. To avoid the problem in future, rotate logs with logrotate instead of deleting them by hand (copytruncate or a postrotate reload tells the program to stop writing to the old file).

Step 5: Look for files hidden under a mount point

If du on / still adds up to much less than df reports, and lsof +L1 shows nothing large, check for files hidden under a mount point. If something wrote to /data before a disk was mounted there, those files still use space on the root filesystem but are hidden behind the mount. A bind mount shows the root filesystem without anything mounted on top of it:

sudo mkdir -p /mnt/rootfs
sudo mount --bind / /mnt/rootfs
sudo du -xh --max-depth=1 /mnt/rootfs 2>/dev/null | sort -h
sudo umount /mnt/rootfs

Compare the size of directories like /mnt/rootfs/data with what you expect. Anything large there is hidden data on the root disk.

Step 6: Fix inode exhaustion

When df -i shows inodes at 100%, look for the directory with the most files rather than the most bytes. GNU du can count inodes directly:

sudo du --inodes -x --max-depth=1 / 2>/dev/null | sort -n | tail -n 15

Drill down the same way as in Step 3. Common causes are PHP session files that are never cleaned up, a stuck mail queue, cache directories, and applications that write one small file per request. Delete old files with a filter rather than a wildcard, because a huge directory can make rm * fail with "Argument list too long":

sudo find /path/to/cache -type f -mtime +7 -delete

Then fix the cause, for example by making the application clean up after itself or by scheduling the cleanup with a systemd timer.

Common space users and safe ways to clean them

WhereWhat it usually isSafe way to check or clean
/var/log/journalsystemd journaljournalctl --disk-usage, then set SystemMaxUse= in journald.conf or run sudo journalctl --vacuum-size=500M
/var/logApplication and system logsSet up logrotate; do not delete logs that services still write to
/var/cache/apt/archivesDownloaded .deb packagessudo apt clean
/boot, /usr/lib/modulesOld kernelssudo apt autoremove (review the list before you confirm)
/var/lib/dockerImages, containers, build cache, volumesdocker system df, then docker system prune; volumes are only removed with --volumes
/var/lib/mysql, /var/lib/postgresqlDatabase data and binary or WAL logsNever delete files here by hand; use the database's own settings and commands
/home, /root, /tmpOld backups, dumps, archivesfind with -size; move to backup storage before deleting

What not to do

  • Do not delete files under /var/lib for databases or Docker by hand. Use the tool that owns them, or you may corrupt data.
  • Do not run rm on a log that a service is writing to and expect space back. Truncate it or restart the service, as in Step 4.
  • Do not clean blindly with wildcards as root. List first, delete second.
  • Do not stop at freeing space. Find out why it filled up, or it will happen again.

Prevent the next full disk

  • Limit the systemd journal size and rotate application logs.
  • Keep backups on separate storage, not on the same disk as the data.
  • Schedule cleanup jobs for caches and temporary files.
  • Alert before the disk is full, for example at 80% space and 80% inodes, using your monitoring system or a simple script that checks df.
  • For Docker hosts, review docker system df regularly.

Quick checklist

  1. df -hT: which filesystem is full?
  2. df -i: is it space or inodes?
  3. du -xh --max-depth=1 | sort -h: which folder is big?
  4. lsof +L1: are deleted files still held open?
  5. Bind mount /: are files hidden under a mount point?
  6. Clean with the right tool, then fix the cause.

Sources