Are you having a hard time trying to audit disk utilization on your Linux system?. I know most Linux sysadmins are accustomed to using du
command to check files consuming more disk space or to find actual file size. I always run the command below to find files in specified directory and sort them by size – Largest to smallest:
$ find /dir -type f -exec du -Sh {} + | sort -rh
You can further limit output results by piping it to head
$ find /dir -type f -exec du -Sh {} + | sort -rh | head -n 10
Which will only print top 10 files
Or by using du
command only:
$ du -sh /dir/* | sort -hr
In this guide, I’ll introduce you to Ncdu
– A disk usage analyzer with an ncurses
interface. Ncdu was designed to ease finding space hogs on a remote server where you don’t have an entire graphical setup available, but it is a useful tool even on regular desktop systems.
Ncdu aims to be fast, simple and easy to use, and should be able to run in any minimal POSIX-like environment with ncurses installed. By default, it will show files/directories disk usage sorted from large to small:
Installing Ncdu on Linux
Ncdu package is available on most Linux distributions from upstream repositories. Let’s see how to install ncdu
package on CentOS, Debian family distributions, and on Arch Linux.
Install Ncdu on Arch Linux
Use the commands:
$ sudo pacman -S ncdu
resolving dependencies...
looking for conflicting packages...
Packages (1) ncdu-2.1-1
Total Installed Size: 0.11 MiB
:: Proceed with installation? [Y/n] y
(1/1) checking keys in keyring [################] 100%
(1/1) checking package integrity [################] 100%
(1/1) loading package files [################] 100%
(1/1) checking for file conflicts [################] 100%
(1/1) checking available disk space [################] 100%
:: Processing package changes...
(1/1) installing ncdu [################] 100%
:: Running post-transaction hooks...
(1/1) Arming ConditionNeedsUpdate...
Install Ncdu on Ubuntu / Debian
On Ubuntu and all other Debian family distributions, install ncdu using the apt
package manager.
sudo apt update
sudo apt install ncdu
Install Ncdu on CentOS / Fedora
On CentOS and Fedora, the package is available from EPEL repository. Add it first then install ncdu using yum.
sudo yum -y install epel-release
sudo yum install ncdu
Using Ncdu to Audit disk usage
Once the package is installed on your OS, simply run:
$ ncdu /dir/
Ncdu also provide a number of options to manipulate files and folders – Navigation, sorting, and even deleting:
- up, k – used to move the cursor up
- down, j – used to move the cursor down
- right, enter, l > – Open selected directory
- left, <,h – This opens parent directory
- n – order by name (press again for descending order)
- s – Order by file size (press again for descending order)
- d – Delete selected file or directory
- g – Show percentage and/or graph
- t – Toggle dirs before files when sorting.
- c – Toggle display of child item counts.
- b – Spawn shell in the current directory.
- i – Show information about the selected item
- r – Refresh/recalculate the current directory.
- q – Quit ncdu
Example 1: Navigate using >
Example 2: Show percentage and/or graph using g
Example 2: Delete file using d
Example 3: Scan a full filesystem
# ncdu -x /
Since scanning a large directory may take a while, you can scan a directory and export the results for later viewing:
# ncdu -1xo- / | gzip >export.gz
# ...some time later:
# zcat export.gz | ncdu -f-
You can also export a directory and browse it once scanning is done:
# ncdu -o- | tee export.file | ./ncdu -f-
The same is possible with gzip compression, but is a bit kludgey:
# ncdu -o- | gzip | tee export.gz | gunzip | ./ncdu -f-
To scan a system remotely, but browse through the files locally:
# ssh -C user@system ncdu -o- / | ./ncdu -f-
Ncdu Scan Options
These options affect the scanning progress and have no effect when importing directory information from a file.
-x
Do not cross filesystem boundaries, i.e. only count files and directories on the same filesystem as the directory being scanned.
–exclude PATTERN
Exclude files that match PATTERN. The files will still be displayed by default, but are not counted towards the disk usage statistics. This argument can be added multiple times to add more patterns.
-X FILE, –exclude-from FILE
Exclude files that match any pattern in FILE. Patterns should be separated by a newline.
–exclude-caches
Exclude directories containing CACHEDIR.TAG. The directories will still be displayed, but not their content, and they are not counted towards the disk usage statistics. See http://www.brynosaurus.com/cachedir/
Conclusion
This tool will save you lots of time when working on disk usage audit for both remote servers and local Desktop systems. Enjoy using Ncdu!