- Posted July 30, 2018
- ByMatthew F. Fox
Understanding disk space usage on a server is a routine, and important, task for many admins. While the du command is a common item in the Linux admin’s toolkit, its output can leave a lot to be desired:
While this provides information on the amount of data stored in each directory shown in the screenshot, it isn’t easy to read when many directories must be listed and there is no visual indicator of disk usage of a directory, relative to its neighbors.
Ncdu is located in the main repositories of most Linux distributions, so you can install it using your distribution’s package manager of choice within seconds. Debian Linux is the distro I work with the most and installing Ncdu via APT is a snap:
sudo apt-get install ncdu
Running Ncdu to report back disk space usage of your current location is as simple as running the ncdu command without any options:
In the example above, Ncdu was run in a directory that contains the document roots for several websites hosted on the server. We can see, in a single screen as opposed to scrolling, that there are 17 directories, and the largest contains 1.9GB of data – much more than the others.
From here, you can easily navigate to subdirectories for additional detail on disk space usage. Ncdu also outputs the total amount of disk space used and number of items at the bottom of the screen and provides us with a wealth of information that is easily navigated – all in return for typing an additional 2 characters at the command line.
Much like the du command, Ncdu includes several options that can help you get the information you are looking for, quickly.
Analyzing a specific directory
Running the ncdu command without any options will report back disk usage for the directory you are currently viewing. To view disk usage for a particular directory, simply append the directory to be scanned to the default command:
Analyzing an entire file system
To analyze an entire file system, run the following command:
ncdu -x /
Including the -x flag instructs Ncdu to only analyze directories on the same file system being scanned and will exclude any mounted directories.
Ncdu refreshes its interface 10 times per second, and doing so can consume more resources than necessary, particularly if you are scanning an entire file system. Enabling quiet mode reduces the refresh rate to twice a second. Quiet mode may be enabled by adding the -q flag to the ncdu command:
ncdu -q /
Output analysis to a file
Think you’ll need to refer to the analysis of a directory at a later date? No problem – Ncdu will save all if its output to a file:
ncdu -1xo- / | gzip >ncdu-results.gz
In this example, we and using Ncdu to scan an entire partition on a server which Ncdu saves to a file that is piped to an archive for viewing later.
Analyzing directories on a remote machine
Ncdu can even analyze directories on remote machines, as long as SSH is available to establish a remote connection:
ssh -C email@example.com ncdu -o- / | ncdu -f-
Ncdu contains several other options, but the ones above are the ones we most commonly use. For more information, please visit Ncdu’s man page. Have a small, but powerful, utility that you’d like to share? Let us know in the comments below!