Linux Commands

How To Use ncdu in Linux To Analyze and Track Disk Usage

The ncdu command is a convenient way of checking disk usage and detecting space hogs on servers. The tool summarizes all the sizes of your directory trees and individual file sizes, and it can be said to use a ncurses interface.

The results of ncdu are displayed on a Text User Interface (TUI), and you can easily track the space usage using the tool, as discussed in the article.

How To Install ncdu

The ncdu doesn’t come preinstalled, but its installation is quite simple. You only need to type the following command:

$ sudo apt-get install -y ncdu

How To Use ncdu

The ncdu command calculates the disk usage of your current directory unless you specify the path. For instance, running the command while on the Desktop/ will display the disk usage, as shown below:

We note that it lists the files in the directory and the space they use. Furthermore, the total disk usage for the given directory gets displayed at the interface’s bottom. To quit the window, press q.

The Up and Down keyboard keys help navigate the files. If you need to move to a given directory, then you can move to it. When it’s selected, click the enter key.

For instance, the following output selects the Pictures/:

To return to the initial directory, press the left arrow key.

Moreover, if you intend to scan large directories, such as your home directory, you can achieve that using ncdu. However, this will take time. Besides, you will get the following window:

Viewing Directory Information

You can get the information of a selected directory, such as the disk usage and the full path, by pressing the “i” keyboard key. You can press it again to exit the window.

Open Help Page

While using ncdu to display the tree structure of the disk usage, you can press SHIFT + ? to open a Help window and move up and down the window using the arrow keys.

Deleting a Directory Using ncdu

Using ncdu, you can delete a selected directory. Use the -d options, and click Yes or No on the confirmation prompt.

For instance, let’s try to delete a directory named dir1:

Specifying a Specific Path

It’s possible to use ncdu to check the disk usage outside the current directory by specifying the path. For instance, to check disk usage of the Downloads/ directory from a different working directory, the following command will be:

$ ncdu ~/Downloads

Scanning the Full System

The ncdu also allows you to check the disk usage of your root (/) system, as shown below:

$ sudo ncdu -x /

Scanning the root system is time-consuming. However, you can export the results for the scan to a file and review them later. To scan a given directory and store results as an archive file, use the following command:

$ sudo ncdu -1xo- ~/Downloads| gzip > scan.gz

The -x specifies ncdu to stick to files and directories within the same file system as the specified one. In the previous example, we are checking the disk usage of the Downloads directory and storing results in our working directory, as shown below:

Afterward, you can read the archive file and pipe the results to ncdu to display the usage using the -f- flag.

$ zcat scan.gz | ncdu -f-

The output will be similar to scanning the disk usage of the Downloads/ directly. The only difference, in this case, is that you scanned earlier and reviewed the results later from a file.

The ncdu Colors

It’s possible to add the color option on ncdu, and you can do that using the following command:

$ ncdu –color dark

The colored output screen will be:

Furthermore, some keyboard shortcuts are helpful to navigate, and you can access them by pressing the “?” Keyboard key to view the complete list.

Conclusion

The ncdu is a utility that you can’t sideline. Yes, you can use the du -h command to check the disk usage of the given directory. However, using ncdu is the better option. With ncdu, you can get a nice window for the output, and the options and commands available are easy to use. Hopefully, you found this article helpful and will get used to ncdu along the way.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.