Summary
If you ever find yourself running low on disk space in Linux, you probably want to see which directories take up the most space. ncdu is a Linux terminal program that will help you do that.
What Is ncdu?
ncduis a utility for finding large directories on your Linux system. The name stands for “Ncurses Disk Usage.” It’s similar to the existingdu utilityexcept that it works in a full-screen terminal interface created in ncurses, hence the name. ncdu was created by Yoren Heling.
Installing ncdu
Installing ncdu is simple. You can just use your distro’s package manager. To install ncdu in Debian or Ubuntu, type this command
It’s available on almost all of the other major Linux distros. Heling maintains a list of distros that package ncdu. He also lists a few BSD systems.
you’re able to also download source code or binary versions from his website, but it’s better to use your distro’s packages when they’re available. It’ll be easier to upgrade when newer versions come out.
Finding the Space Hogs
To find the biggest directories on your system, you’re able to run the ncdu command. Running ncdu without any arguments will start the scan in your current working directory:
To start the scan in another directory, add the path on the command line. For example, to start the scan at theroot directory(/), type
ncdu also allows for command-line arguments, more of which are covered below. A useful argument is -x, which will tell ncdu to stay on the same filesystem and not follow any links to other filesystems. I use this in my Windows Subsystem for Linux installation to make sure it will only scan directories in my Linux distro and not the Windows directory, which ismounted in /mnt/c.
Here’s an example that puts these options together
When you start ncdu, it will run its scan. It’ll take a while, though the time depends on which directories you run the scan on. A scan of an entire drive with many subdirectories on it will take longer than a smaller directory.
When the scan finishes you’ll see a sorted list of directories, with the largest directories on the top. You can then drill down through subdirectories to find the largest directories.
When you enter a directory, you can see the largest subdirectory in that directory.
You can change how ncdu sorts directories with some key presses. The “n” key sorts by name, s sorts by size (the default), and C sorts by the number of items. These keys will also toggle sorting by ascending or descending order.
you’re able to sort by modification time, you can use the “M” (capital M) key.
If there’s a file or directory that you want to delete, you can delete it with the “d” key.
The “c” key will toggle the display of the number of items in the subdirectories on and off.
You can toggle the percentage and graph the directories are taking up with the “g” key to show the graph, the percentage, or both.
Moving Around in ncdu
you’re able to move up and down in ncdu with the arrow keys. If you’re a Vim fan, you canuse the j and k keysto move up and down in the screen.
To move down in the directory tree, you can highlight the subdirectory and press enter or the right arrow. To go up a directory, you can press the right arrow, the < key, or the h key.
You can launch a shell from the current directory if you want to perform some operations. When you’ve finished,press Ctrl+Dor type “exit” to return to ncdu.
More ncdu Options
ncdu has even more options than those mentioned earlier. You can tell ncdu to explicitly cross filesystems with the –cross-file-system option. You can exclude directories with the –exclude option, followed by a pattern to exclude.
The -L option will explicitly tell ncdu to followsymbolic links, as well as the –follow-symlinks option. The –no-follow-symlinks will do the opposite, telling ncdu not to follow symbolic links.
Because scanning can take a while, you may save the results in a file with the -o option and view the results later with the -f command.
For example, to save the results from scanning the entire filesystem
The scan will still run, but at the command line. After it finishes, you’ll be back at the shell. To view the results:
To dig even deeper, use the -h option for help, use the help menu by pressing “?” (question mark) in ncdu, or read themanual pagefor ncdu, or check the website.