Weighing in with Ncdu
Ncdu
Ncdu adds some GUI-like features to the classic du command.
The du (for "disk usage") utility is a useful tool for estimating file size and cleaning up a hard drive. du summarizes the disk usage for each file in the directory and even looks down into subdirectories.
du worked well in the old days of printed output, but in today's interactive environments, it has some limitations. Like many old-school command-line tools, du prints the output on the screen by default, and the terminal screen is nowhere near large enough to see all the files in a large directory system. Of course, you could print to a file and examine the file with a text editor, or you could filter and refine the du results using standard command-line piping and sorting techniques. But sometimes what you really want to do is move around in the output as if perusing a printed page, progressing interactively through subdirectories as you would with a GUI-based file manager.
The Ncdu utility [1] is a souped-up version of du that has some GUI-like features. Ncdu uses the ncurses API [2], which is a common tool for building GUI features into command-line programs. (See the box entitled "Why Work in the Terminal?")
Why Work in the Terminal?
Some users prefer to work in a terminal window all the time – if you're good at it, you can work very quickly and efficiently without ever reaching for a mouse or touch screen. But even if you're accustomed to a desktop environment, you might have the occasional need for a command-line disk usage tool.
Desktop environments include several graphical tools for examining file size and disk usage, including QDirStat [3], Filelight [4], and Baobab [5]. But if the disk is completely full due to an error, or if another problem somewhere in the system caused a logfile to expand and lock up the disk with error messages, the graphical interface might not be accessible, and you need to use a text-based command to search out and delete bloated files so you can restart the system.
A tool like Ncdu is also useful for remote terminal sessions with SSH, in cases where @KE:
Ncdu, which has been under development since 2007, is written in C and works on all POSIX-compliant systems. Packages are available for many Linux distributions, as well as for BSD, Solaris, Cygwin, and Mac OS.
Ncdu comes preinstalled on many Linux distributions, and if you can't find it on your system now, you can easily install it using your local package manager. The Ncdu utility is only a few kilobytes in size, and it is released under an MIT license. The easiest way to control Ncdu is to use the arrow keys, the Enter key, and a few letters.
Easy to Use
To start Ncdu, type ncdu
. If you only want to examine hard disk usage, you can simply start the application as a user. If you want to delete data outside of your home directory, you will need root privileges. The ncdu
command supports a number of command-line options (see the box entitled "Command Line Options.")
Command-Line Options
The ncdu
command supports some common command-line options, such as -h
(Figure 1) for help and -v
for displaying the version number. If you start Ncdu in quiet mode with the -q
option, the screen will update less frequently, which will save bandwidth if you are working through an SSH connection.
The -x
option tells Ncdu not to analyze all directories in the path but only those with the same filesystem as the source directory. This means that if the entire system is scanned, other mounted filesystems are not included. -X
lets you exclude files and directories from the scan.
Ncdu sorts directories and files in descending order by default (Figure 2). The letter S
reverses the sort order and shows the smallest folders and files at the start. Pressing G
lets you toggle through the different modes for displaying the relative size. We found figures in percent format the most meaningful (Figure 3). All told, the application offers a handful of options and a good dozen keyboard shortcuts to manipulate the output. The most common operating options are listed in Table 1.
Navigation |
|
---|---|
Key | Function |
Up/K |
Up in the directory tree |
Down/J |
Down in the directory tree |
Right/L/Enter |
Open directory |
Left/</H/Enter |
Close directory |
Manipulation |
|
N |
Sort by file name |
S |
Sort by file size |
A |
Toggle between effective size and occupied space for a file |
D |
Delete the selected file from the directory |
T |
Sort directories before files |
G |
Toggle between size display by graph, percent, both, or none |
E |
Don't show hidden files |
I |
Information about the selected element |
R |
Recalculate selected element |
Q |
Quit Ncdu |
The four arrow keys are all you actually need to navigate Ncdu. Use up and down arrow keys to move through the directory tree. The right arrow key takes you down into the selected directory, and the left arrow key back up again in the directory tree. Instead of the right arrow key, the Enter key also works in both directions. Alternatively, you can move up and down the directory tree with the K
and J
keys. Alternatively, you can open a directory with the L
; the <
key closes it again.
Customizing the Default Output
The default output format for Ncdu shows the size of the largest directory or file in the first row on the left. On the right is the file or directory name, where a forward slash marks a directory. In the middle column, the relative size of the element is shown with a graph of hashtags. Toggle the size display by pressing the G
key. It shows additional information, or only percentages, or no information at all on the relative size.
If you want to scan the whole filesystem, enter ncdu /
. To exclude other filesystems from the search, add -x
.
Delete with Care
Once you have found the element you are looking for, and assuming you want to remove disk hogs that you no longer need, press the D
key to delete the selected element. Be careful, because the process is irreversible; Ncdu uses the rm
command in the background. To protect yourself from accidental deletion, start Ncdu with the -r
option, which disables the delete feature (Figure 4).
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.
-
DebConf24 to be Held in South Korea
Busan will be the location of the latest DebConf running July 28 through August 4
-
Fedora Unleashes Atomic Desktops
Fedora has combined its solid distribution with rpm-ostree system to make it possible to deliver a new family of Fedora spins, called Fedora Atomic Desktops.
-
Bootloader Vulnerability Affects Nearly All Linux Distributions
The developers of shim have released a version to fix numerous security flaws, including one that could enable remote control execution of malicious code under certain circumstances.