How to Use ncdu to Find Disk-Hogging Directories in Linux
Linux & macOS Terminal
Summary
- ncdu is a Linux utility to find large files and directories.
- To scan your root directory with ncdu, for example, simply use the command “ncdu /” (without quotes) and you’ll see your largest files listed.
- Use ncdu with command-line arguments to customize scans, such as the -x argument to ignore links to other filesystems.
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?
ncdu is a utility for finding large directories on your Linux system. The name stands for “Ncurses Disk Usage.” It’s similar to the existing du utility except 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
sudo apt install ncdu
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 can 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.
Related
How to View Free Disk Space and Disk Usage From the Linux Terminal
Everything you need to know about Linux’s df and du commands
Finding the Space Hogs
To find the biggest directories on your system, you can run the ncdu command. Running ncdu without any arguments will start the scan in your current working directory:
ncdu
To start the scan in another directory, add the path on the command line. For example, to start the scan at the root directory (/), type
ncdu /
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 is mounted in /mnt/c.
Here’s an example that puts these options together
ncdu -x /
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 can 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 can move up and down in ncdu with the arrow keys. If you’re a Vim fan, you can use the j and k keys to 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+D or 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 follow symbolic 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 can 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
ncdu / -o file
The scan will still run, but at the command line. After it finishes, you’ll be back at the shell. To view the results:
ncdu -f file
To dig even deeper, use the -h option for help, use the help menu by pressing “?” (question mark) in ncdu, or read the manual page for ncdu, or check the website.
Related
The Linux Directory Structure, Explained
If you’re coming from Windows, the Linux file system structure can seem particularly alien.