Cloud VPS from $1.98/mo · pay with Bitcoin, Ethereum, Monero, USDT or Solana · paid in crypto
Linux

Find what is filling your Linux disk with df, du and ncdu

A top-down method to find what eats disk space on a Linux server with df, du and ncdu, plus the usual suspects: logs, caches, Docker and deleted files.

CloudSat teamNovember 18, 20267 min read
A disk drawn as a ring of colored segments, with one swollen segment glowing like a warning light.
On this pageStep 1: find the full filesystem

A full disk rarely fails politely. Databases refuse writes, package upgrades stop halfway, logs go silent and any service that needs a temporary file crashes with No space left on device. The cure is always the same sequence: find the full filesystem, then the heavy directory, then the file, then the reason it grew. Each step below narrows the search, and the second half covers the culprits that turn up again and again.

Step 1: find the full filesystem

df -hT -x tmpfs -x devtmpfs

-h prints human-readable sizes, -T adds the filesystem type and the two -x options hide RAM-backed filesystems that cannot fill your disk. A typical result on a struggling server:

Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/vda1      ext4   59G   56G  271M 100% /
/dev/vdb       xfs   200G   41G  160G  21% /mnt/data

Note that used plus available is less than the size. ext4 keeps about 5% of its blocks for root, so ordinary users hit "full" a little before root does. Write down which mount point is full: everything after this step should stay on that filesystem.

When df shows space but writes still fail

Every file also consumes an inode, and a filesystem holding millions of tiny files (PHP sessions, mail queues, cache shards) can run out of inodes with gigabytes free. Check with df -i, then find the directory with the most files:

sudo du --inodes -x -d 3 / 2>/dev/null | sort -n | tail -n 10

Step 2: find the heavy directory

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

-x keeps du on one filesystem, so it skips /proc, /sys and other mounts. --max-depth=1 totals each top-level directory and sort -h understands sizes such as 4.2G. Run it again on the biggest entry (/var, then /var/lib, and so on); three or four rounds usually land on the culprit. Running as root matters, because du silently skips directories it cannot read and under-reports.

If you already suspect the usual places, check them in one line:

sudo du -sh /var/log /var/lib/docker /var/cache /home/* 2>/dev/null

du reports blocks actually allocated, while ls -l shows the apparent size. Sparse files, such as virtual machine images and some database files, can look enormous in ls and small in du. Compare both with du --apparent-size before you panic about a 100 GB file on a 60 GB disk.

Step 3: browse it with ncdu

ncdu turns the same scan into a browser you can drill into with the arrow keys.

sudo apt install ncdu     # Debian and Ubuntu
sudo dnf install ncdu     # Fedora; on AlmaLinux and Rocky, enable EPEL first
sudo ncdu -x /

Press Enter to open a directory, s to sort by size, C to sort by file count, i for details and q to quit. d deletes the selected item after one confirmation, with no undo, so read the path twice. On a very large filesystem, save a scan with ncdu -x -o scan.json / and browse it later with ncdu -f scan.json, which avoids rescanning. gdu is a similar tool that scans in parallel and is quick on NVMe.

Step 4: list the biggest files

Sometimes one file is the whole story: a forgotten backup, a core dump, a database export.

sudo find / -xdev -type f -size +500M -printf '%s\t%p\n' 2>/dev/null \
  | sort -rn | head -n 20 | numfmt --to=iec --field=1

numfmt converts the byte counts into readable units. For stale files, list before you delete: find /var/backups -type f -mtime +30 -name '*.tar.gz' shows archives older than 30 days, and adding -delete to the same command removes them once you are sure.

The usual suspects

The systemd journal

journalctl --disk-usage
sudo journalctl --vacuum-size=500M     # or --vacuum-time=14d

Vacuuming frees space once. To cap the journal for good, add a drop-in and restart journald:

sudo mkdir -p /etc/systemd/journald.conf.d
printf '[Journal]\nSystemMaxUse=500M\n' | sudo tee /etc/systemd/journald.conf.d/size.conf
sudo systemctl restart systemd-journald

Application logs that never rotate

An application that writes its own log file without a rotation rule will eventually take the disk. Do not rm a log that a process still writes to: the space is not released until the process closes the file. Truncate it instead with sudo truncate -s 0 /var/log/myapp/app.log, then add a rule in /etc/logrotate.d/myapp:

/var/log/myapp/*.log {
    weekly
    rotate 8
    compress
    missingok
    notifempty
    copytruncate
}

copytruncate suits applications that keep the file open. Test the rule with sudo logrotate -d /etc/logrotate.d/myapp, which only prints what it would do.

Package caches and old kernels

sudo apt clean                 # cached .deb downloads
sudo apt autoremove --purge    # orphaned dependencies and old kernels
sudo dnf clean all             # RHEL family cache

Ubuntu snaps keep earlier revisions of each snap. snap list --all shows them as disabled, and sudo snap set system refresh.retain=2 keeps the minimum of two.

Docker images, build cache and logs

docker system df          # space used by images, containers, volumes, cache
docker image prune -a     # images that no container uses
docker builder prune      # build cache

Be careful with docker system prune --volumes. It also deletes unused anonymous volumes, and a database container started without a named volume keeps its data in exactly that kind of volume.

Container logs are the quieter problem. The default json-file driver never rotates, so a chatty container grows a log under /var/lib/docker/containers/ until the disk fills. Cap it in /etc/docker/daemon.json, restart Docker, and recreate the containers, because the setting only applies to containers created after the change:

{
  "log-driver": "json-file",
  "log-opts": { "max-size": "10m", "max-file": "3" }
}

Core dumps and crash reports

A service stuck in a crash loop can leave a core dump on every restart. Where systemd-coredump is installed (the default on Fedora and the RHEL family) the dumps land in /var/lib/systemd/coredump/ and coredumpctl list shows them; Ubuntu's crash reporter writes to /var/crash/ instead. Delete the old dumps once you have what you need for debugging, then fix the crash loop itself, or the space disappears again overnight.

Files deleted while still open

When df says the disk is full but du cannot find the space, a process is probably holding a deleted file open. This is common after someone removes a huge log by hand.

sudo lsof -nP +L1

The command lists open files whose link count dropped to zero. Restart the process that owns them (often the service writing the log) and the space comes back.

Data hidden under a mount point

If something wrote to /mnt/data before the second disk was mounted there, those files sit on the root filesystem, invisible under the mount. A bind mount of / shows what is underneath:

sudo mkdir -p /mnt/rootview
sudo mount --bind / /mnt/rootview
sudo du -sh /mnt/rootview/mnt/data
sudo umount /mnt/rootview

Stop it happening again

Cap the journal and container logs as shown above, give every application log a logrotate rule, and get warned before the disk is full. On a single server, a small script run hourly from cron is enough:

#!/usr/bin/env bash
LIMIT=85
USED=$(df --output=pcent / | tail -n 1 | tr -dc '0-9')
if (( USED >= LIMIT )); then
  logger -t disk-alert "Root filesystem at $USED% on $(hostname)"
fi

logger writes to the journal; swap it for an email or a webhook call. The (( )) test is explained in Bash conditionals.

If the data is legitimate, add space instead of deleting. On CloudSat, resizing to a bigger plan gives the server a larger NVMe disk. If df -h still shows the old size afterwards, grow the partition and then the filesystem:

sudo growpart /dev/vda 1     # package cloud-guest-utils or cloud-utils-growpart
sudo resize2fs /dev/vda1     # ext4; for XFS run: sudo xfs_growfs /

FAQ

Why do df and du show different numbers?

df asks the filesystem how many blocks are in use; du adds up the files it can see. The gap comes from deleted files still held open, files hidden under a mount point, the ext4 reserve for root, filesystem metadata, and directories du could not read because it ran without root.

Is it safe to delete files in /var/log?

Rotated files ending in .1 or .gz are safe to remove. Truncate active logs instead of deleting them, shrink the journal with journalctl --vacuum-size, and keep the directories themselves: some services, Nginx among them, refuse to start when their log directory is missing.

What does "No space left on device" mean when df shows free space?

Usually the filesystem ran out of inodes, which df -i confirms. Less often, a user quota is exhausted, or a non-root process hit the blocks that ext4 reserves for root.

Run it on CloudSat. Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. See plans or deploy a server.