Speed Up Ubuntu Disk Cleanup with ncdu and duf: Replacing Outdated Commands

Ubuntu tutorial - IT technology blog
Ubuntu tutorial - IT technology blog

When the Traditional df -h Command Is No Longer Enough

Sleeping soundly only to receive a “Disk space critical” alert at 3 AM is a nightmare for any Sysadmin. When I first started, I would immediately jump in and type df -h. This command reports full partitions quickly but remains “silent” when you want to know exactly which files are hogging tens of gigabytes.

Next, I’d struggle with du -sh * | sort -h. This works but is very slow. Every time you want to dig deeper into a subdirectory, you have to retype the command from scratch—an absolute waste of time. After six months of managing production VPS clusters, I ditched that habit. Instead, I use the duo ncdu and duf. These two tools are extremely lightweight yet solve the problem intuitively.

Install ncdu and duf in 30 Seconds

Both are available in the default Ubuntu repositories since version 20.04. I always pre-install this pair along with htop as soon as I initialize a new server.

sudo apt update
sudo apt install ncdu duf -y

If you’re using an older Ubuntu version or want to try the latest duf written in Go, you can download the .deb file from GitHub. However, the version in the official repo is usually more than enough for daily cleanup tasks.

duf – The “Evolved” Version of the df Command

The df -h command provides very raw output that can be hard to read if your server has many mounted partitions. duf (Disk Usage/Free Utility) fixes this with a color-coded table interface, bar charts, and smart automatic classification.

Basic Usage

Type just one concise command:

duf

The results are clearly divided into: local devices, special file systems, and network partitions. Just glance at the Usage column; anything in red indicates a critical level.

Data Filtering Tips

My favorite feature in duf is the ability to hide “junk” partitions (like snap loops) to focus on real hard drives:

duf --only local

Or if you encounter a situation where the disk still has free space but you can’t create files (Inodes error), check it immediately with:

duf --inodes

ncdu – A “Magnifying Glass” for Large Files

If duf helps you know which house is full, then ncdu (NCurses Disk Usage) allows you to step inside and see which closet is full of junk. This is the most powerful tool for cleaning a server without needing to remember complex commands.

Effective Scanning

To scan the current directory, use the ncdu command. However, when scanning from the root /, use the -x flag. This prevents ncdu from scanning network drives or external mounts, making it 2-3 times faster.

sudo ncdu -x /

Real-world example: I once scanned 100GB of data in less than 10 seconds to find an Nginx log file that had bloated to 45GB.

Essential Shortcuts

The ncdu interface is extremely easy to navigate using arrow keys:

  • Up/Down: Move between files/directories.
  • Right or Enter: Go deeper into a directory.
  • Left: Return to the parent directory.
  • s key: Sort by size (default).
  • d key: Delete the selected file directly. Don’t worry, it will ask for confirmation.

Real-World Experience: Common Storage Hogs

Through many troubleshooting sessions, I’ve identified 3 locations you should check first with ncdu:

1. System Logs (Journald)

The /var/log directory is always the prime suspect. Specifically, journald can occupy many gigabytes if left unmanaged. Try cleaning logs older than 7 days with this command:

sudo journalctl --vacuum-time=7d

2. Docker Resources

If the server runs Docker, the /var/lib/docker directory will bloat rapidly due to old images or junk containers. Note: Do not manually delete files here. Instead, run this command:

docker system prune -a --volumes

3. Apt Cache

The /var/cache/apt/archives directory contains old .deb installation files. You can free up hundreds of MBs to several GBs with:

sudo apt-get clean

Weekly Maintenance Routine

Don’t wait until the system crashes to start cleaning. I usually spend 2 minutes every weekend for a quick check:

  1. Type duf for an overview to see if any partition exceeds 80%.
  2. Use ncdu to hunt down old log files or backups.
  3. Delete items that are definitely junk using the d key.

Managing Ubuntu disk space becomes effortless when you have the right tools. Try installing ncdu and duf today; I bet you’ll never want to go back to typing manual du -sh commands again!

Share: