How To Check Disk Space Usage In Linux Using Ncdu
Today, we will be discussing how to check disk space usage in Unix-like systems using Ncdu utility. Ncdu, acronym of NCurses Disk Usage, is a curses-based version of the well-known ‘du’ command. It provides a fast way to see what directories are using the disk space. Even though there are plenty of tools and ways available to analyze the disk usage in Linux, the developer of this utility is not satisfied with all of them. So, he developed this utility using C programming language with an ncurses interface. It is simple and fast disk usage analyzer which is used to find which directories or files are taking up more space either on a local or remote systems. In this brief tutorial, I will teach you how to check disk space usage with the help of Ncdu utility in Linux operating systems.
Check Disk Space Usage In Linux Using Ncdu
Ncdu is available in the default repositories of most Linux distributions. So, you can install it using the distribution’s default package manager.
On Arch Linux, Antergos, Manjaro Linux:
$ sudo pacman -S ncdu
On RHEL, CentOS, Scientific Linux:
$ sudo yum install ncdu
$ sudo dnf install ncdu
On SUSE, openSUSE:
$ sudo zypper in ncdu
On Debian, Ubuntu, Linux Mint:
$ sudo apt-get install ncdu
Once installed, run the following command to analyze the disk space usage on your Linux box.
Sample output would be:
This command will analyze your HOME directory. After analyzing, it will show you disk usage report, sorted in descending order. Large items will be displayed on top.
Use up/down arrows (Or k and j in the keyboard) to move between items.
Press “i” to view the details of the selected item.
Press “i” again to close this window.
To view the items inside the selected directory, press ‘right’ arrow or ENTER key. It will display the list of files or directories inside the selected directory.
To go back to the parent directory, press “left” arrow.
We can also display size of a particular directory by specifying its actual path like below.
$ ncdu Downloads/
To analyze entire root (/) file system, run:
$ sudo ncdu -x /
Here, -x indicates that only count files and directories on the same filesystem as the directory being scanned. It will avoid scanning the mounted devices.
By default, ncdu will update the output screen 10 times a second while scanning the directory. This might consume more bandwidth if you are analyzing the disk usage of a remote system. Fortunately, this will be decreased to once every 2 seconds in quiet mode. We can use this feature to save bandwidth over remote connections.
To run the ncdu in quiet mode, use -q flag as shown below.
$ ncdu -q
Some times, you might want to view the scanning report later. In such cases, scan a directory and export the results in any archive format for later viewing like below.
$ ncdu -1xo- / | gzip >export.gz
This command will scan the HOME directory and save the scanning report in a file called export.gz.
/usr/lib32/dbus-1.0/include/dbus/dbus-arch-deps.h 509932 files
You can view it later by running the following command:
$ zcat export.gz | ncdu -f-
It is also possible to export a directory and browse it once scanning is done:
$ ncdu -o- | tee export.file | ncdu -f-
To scan a remote system, but browse through the files locally, run:
$ ssh -C [email protected] ncdu -o- / | ncdu -f-
Here, ostechnix is the user name of my remote system, and 192.168.43.2 is remote system’s IP address. -C switch enables compression.
To quit ncdu, press q.
Here is the list of available key options in ncdu utility.
- up, k – Move cursor up.
- down, j – Move cursor down.
- Right arrow, ENTER key – Open selected directory.
- Left arrow, <, h – Open parent directory.
- n – Sort by name (ascending/descending).
- s – Sort by size (ascending/descending).
- C – Sort by items (ascending/descending).
- d – Delete selected file or directory.
- t – Toggle dirs before files when sorting.
- g – Show percentage and/or graph.
- a – Toggle between apparent size and disk usage.
- c – Toggle display of child item counts.
- e – Show/hide hidden or excluded files.
- i – Show information about selected item.
- r – Recalculate the current directory.
- b – Spawn shell in current directory.
- q – Quit ncdu.
For more details, read the man pages.
$ man ncdu
Now, you know how to analyze the disk space usage in Linux using ncdu. If you came to know that certain directory or file is consuming more space on your hard drive, you can delete or move them safely to another drive to free up the disk space.
And, that’s all. I will be soon here with another useful guide. If you find our guides useful, please share them on your social, professional networks and support OSTechNix.