Home UbuntuNCDU and Disk Usage Alerts: Keep Ubuntu 24.04 Disks Healthy

NCDU and Disk Usage Alerts: Keep Ubuntu 24.04 Disks Healthy

by Knowledge Base Linux
NCDU and Disk Usage Alerts — NCDU and Disk Usage Alerts: Keep Ubuntu 24.04 Disks Healthy

A full disk is one of the most disruptive failures an Ubuntu 24.04 server can experience: databases refuse writes, package upgrades abort halfway, web applications start returning 500 errors, and in extreme cases users cannot even log in. The classic way to investigate is du -sh *, but that approach is slow and produces a flat list that is hard to explore. This is where the ncdu disk usage workflow shines. ncdu (NCurses Disk Usage) renders your filesystem as an interactive, sortable tree, so you can drill down into the exact directories eating your gigabytes and reclaim space in minutes instead of hours. In this guide you will install ncdu, scan your disks interactively, clean up safely, and then automate the whole thing with cron-based disk usage alerts. This article covers NCDU and Disk Usage Alerts in detail.

ncdu disk usage alert workflow: scheduled cron job monitoring a disk gauge and sending an email alert

Prerequisites – NCDU and Disk Usage Alerts

  • An Ubuntu 24.04 server or desktop with a user that has sudo privileges.
  • SSH access if you are working on a remote machine.
  • About 20 minutes and roughly 50 MB of free space for the scan database in /tmp.

Step 1: Install ncdu on Ubuntu 24.04 – NCDU and Disk Usage Alerts

ncdu ships in the Ubuntu universe repository, so installation is a single apt command:

sudo apt update
sudo apt install -y ncdu
ncdu --version

On a fresh Ubuntu 24.04 install this pulls in a tiny binary (under 200 KB) with no extra dependencies. The package is maintained and receives the same security updates as the rest of the archive, so prefer it over downloading a random static binary from the internet. Once installed, the ncdu disk usage tool is ready to scan any directory you point it at.

Step 2: Run an interactive ncdu disk usage scan

The basic ncdu disk usage scan starts at the root filesystem:

sudo ncdu /

ncdu first walks the directory tree (this takes a few seconds to a few minutes on large disks) and then shows an interactive list of directories sorted by size, with a bar graph next to each entry. Navigate with the arrow keys, press Enter to descend into a directory, and press q to quit. Other handy keys:

  • n — sort by name instead of size.
  • s — sort by size (the default).
  • d — delete the selected file or directory (asks for confirmation).
  • e — toggle hidden files.
  • ? — show the full help screen.

Unlike flat du output, an ncdu disk usage session lets you interactively drill into folders instead of scrolling through endless lines of text. To scan a specific directory instead of the whole root, pass it as an argument. These two are the most useful starting points on a server:

sudo ncdu /var/log
ncdu /home

Note that ncdu stays within a single filesystem by default, which keeps the scan fast and avoids surprises with mounted network shares. If you do want to cross mount points, add the -x flag. For a deeper primer on reading disk statistics, see our guide on how to check disk space on Linux.

Step 3: Delete files safely with ncdu’s delete command

When you find an obvious space hog — an ancient backup tarball, a forgotten download directory, a rotated log that never got cleaned — highlight it and press d. ncdu asks for confirmation before removing anything, which makes accidental deletion much less likely than typing rm -rf by hand.

Still, follow these safety rules during every ncdu disk usage cleanup session:

  • Only delete things you can identify: caches (/var/cache/apt), old logs, your own backups and downloads.
  • Never delete libraries, binaries, or anything under /usr, /lib, /etc, or /boot unless you are absolutely sure what it is.
  • If the system is running production services, prefer archiving to another disk before deleting: tar -czf /mnt/backup/var-log-old.tar.gz /var/log/old-logs.
  • After deleting, quit ncdu and confirm the space is really free with df -h.

A cautious ncdu disk usage cleanup beats a panicked rm -rf every time — take the extra minute to confirm what you are deleting.

Step 4: Find top space hogs without a terminal UI

Sometimes you are on a server where an interactive session is inconvenient, or you want to record a scan for later comparison. The ncdu disk usage report can be exported and re-opened offline:

sudo ncdu --exclude /proc --exclude /sys -o /tmp/scan.dat /
ncdu -f /tmp/scan.dat

The first command performs the scan and writes a compact database file; the second opens that database in the normal interactive interface. This is also great for comparing growth over time: take a scan today, take another next month, and diff the biggest directories. Scheduling a weekly ncdu disk usage export makes disk growth visible before it becomes an emergency. For a strictly read-only browse (no delete key), add the -r flag: ncdu -r /.

Step 5: Set up cron-based disk usage alerts

Interactive cleanup fixes today’s problem, but disks fill up again. A small monitoring script that runs from cron and warns you at 80% usage turns a 3 a.m. outage into a routine ticket. Create /usr/local/bin/disk-alert.sh:

#!/usr/bin/env bash
set -euo pipefail

THRESHOLD=80
MOUNTS="/ /home /var"
RECIPIENT="[email protected]"

for mount in $MOUNTS; do
  usage=$(df --output=pcent "$mount" | tail -1 | tr -dc '0-9')
  if [ "$usage" -ge "$THRESHOLD" ]; then
    message="$(hostname): disk usage on $mount is ${usage}% (threshold ${THRESHOLD}%)"
    logger -t disk-alert "$message"
    if command -v mail >/dev/null 2>&1; then
      echo "$message" | mail -s "[ALERT] Disk usage on $(hostname)" "$RECIPIENT"
    fi
  fi
done

The script uses df --output=pcent to get a machine-readable percentage, logger to always leave a trace in the system log, and the mail command when it is available. Make it executable and schedule it every six hours:

sudo chmod +x /usr/local/bin/disk-alert.sh
sudo crontab -e

Add this line:

0 */6 * * * /usr/local/bin/disk-alert.sh

Two notes: email delivery requires a mail setup (install mailutils and configure a relay or a local MTA such as Postfix), and cron runs with a minimal PATH, so the script above deliberately uses absolute command paths only where needed and avoids shell aliases. Even without email, the logger line means every breach is recorded in syslog where your log monitoring can pick it up.

Step 6: Clean up journald and keep logs rotated

On modern Ubuntu, the systemd journal is one of the most common silent space consumers. Check how much it holds and vacuum it down:

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

To make the limit permanent, edit /etc/systemd/journald.conf and set:

SystemMaxUse=500M
RuntimeMaxUse=200M

Then restart the service with sudo systemctl restart systemd-journald. For the full walkthrough, see our article on how to clear journal logs.

Beyond the journal, make sure logrotate is actually doing its job. Application logs in /var/log are managed by configuration files in /etc/logrotate.d/. Verify a config in debug mode (which changes nothing) before relying on it:

sudo logrotate -d /etc/logrotate.d/nginx

Look for daily or weekly, a sane rotate count, and compress in the output. A missing or broken logrotate config is the number-one reason log directories grow without bound.

Troubleshooting common disk space problems

df and du disagree about free space

If df says the disk is full but du and your ncdu disk usage scan cannot find the files, a process is probably holding open a deleted file. List them with:

sudo lsof +L1 | grep deleted

Restarting the owning service (or truncating the file descriptor) releases the space immediately.

ncdu reports permission errors

Run the scan with sudo so it can read every directory. Without root privileges, unreadable directories are skipped and their sizes are underreported.

The disk fills up again within days

Re-run sudo ncdu /var/log and check whether logrotate is rotating. Also look for runaway application logs, unrotated Docker container logs, and database binary logs.

The alert script sends no email

Confirm mailutils is installed and that the MTA can deliver (check /var/log/mail.log). The script still logs to syslog via logger, so a centralized log monitor remains a reliable fallback.

The cron job never runs

Check the script is executable, uses no aliases, and look for cron errors with grep CRON /var/log/syslog.

Conclusion

A healthy ncdu disk usage routine has two halves: the interactive scan that finds and removes today’s space hogs, and the automation — cron-based alerts, journald limits, and working logrotate configs — that stops the disk from silently filling again. Make the ncdu disk usage scan part of your monthly maintenance checklist, keep the alert threshold at 80% so you always have headroom, and consult the official Ubuntu Server documentation whenever you need the authoritative reference for storage tooling. Your future self, the one not paged at 3 a.m., will thank you.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.