Check what's taking your disk space in Linux with ncdu

Updated: November 30, 2018

Space. The final frontier. But what if you run out of space? Sometimes, you may end up hogging your hard disk partitions with data, and you might not even be fully aware you're doing that until the moment you actually need some free storage. Well, not to worry, we shall clean up.

That's not quite as simple as it sounds. First, most if not all distros distinctively lack a self-cleanup mechanism that will remove - in a safe way - unnecessary stuff. You can cleanup your software packages and browser temp files, but there may be other stuff you want to get rid of, but you don't know how. Experienced users may point out the old, familiar  df and du tools, but there's an even better way. Ncdu.

Teaser

Da-da-da-duuuuu

So. If you are comfortable working on the command line, you can indeed use the df utility to examine file systems, and then du to check the usage. You do not a few fancy flags to get this going. For example, you can just run something like du -h / | sort -nr to get the listing based on the highest hitter. But in this case, there's the question of filesystem traversal (if you have other disks/partitions mounted), and there might be a problem with filesystem size and directory tree hierarchy depth. It's solvable, but it's not fun.

To wit, the tool called ncdu was born. I believe the name stands for Ncurses du, and that first word is the text-based user interface so you get nice, blocky UI in your terminal window. In other words, a friendly frontend for du. It's available in most distro repos, e.g.:

sudo <package manager> install ncdu

Let's use ncdu

Like all tools, it has flags and options. The simplest way to run ncdu (as sudo or root) is:

ncdu -x /

Or you can also add the -r (read-only) flag to make sure nothing gets messed up. Let the program run. It can take a while. Once the tool completes, it will list the disk usage top-down, showing you how much space each directory takes. Since we used the -x flag, we know these are not separate partitions or filesystems, like say NFS or Samba or something. If you keep your home separate, then you may want to run this for /home, too.

Scanning

Finished scan

Hit the right arrow key to go one level down or left arrow key to go back. On one of my machines, a specific partitions was fairly heavily used, and I wasn't quite sure why. So after running ncdu, I had the information. That does not mean you should delete anything - but at least you know what the top offenders are, if you want to clean up or free some disk space.

Results 1

Results 2

Conclusion

Ncdu is definitely not the only program of its kind. Far from it. But it has a certain charm, and it's quite useful. The same way I've always loved tkdiff (a frontend for diff), as it resonates well with how my brain sees things, ncdu also works well in this regard. Most importantly, it's simple, effective, does not rely on any big or heavy desktop environment, it's maintained, and it gave us the answers we sought.

If you find yourself short of megabytes for your pr0 ... I mean useful and important work stuff, then you can quickly explore the system tree with ncdu. Lots of Linux programs do not have a uniform approach to where they keep their files, so the disk space quest can take a little bit of digging. Frustration-free digging, with ncdu. Heigh-ho. We're done.

Cheers.

You may also like: