31 C
Tuesday, April 13, 2021

How to install ncdu on Linux / Unix to see disk usage

Must read

The du (disk usage) command summarizes directory trees’ sizes, including all of their contents and individual files’ sizes on Linux and Unix-like systems such as macOS. It helps track down space hogs. In other words, we can list directories and files that consume large amounts of space on a hard disk drive. Let us see the ncdu command, a curses-based version of the well-known du command.

Over the years, ncdu recommend to me by many nixCraft readers. Ncdu is a disk usage analyzer with an ncurses interface. Still, I never tried an alternative for finding the sizes of files and directory trees when using a text-based graphical user interface (TUI). However, in this post, I will explain how to install ncdu on Linux or Unix and see if it is worth installing it on production systems.

Installing ncdu on Linux

Open the Terminal application and then type commands as per your distro. For instances, Debian/Ubuntu Linux users try the apt command/apt-get command as follows:
sudo apt install ncdu

Alpine Linux install ncdu

Try the apk command to install ncdu including man pages on Alpine Linux:
# apk add ncdu ncdu-doc

Arch Linux install ncdu

Use the pacman command:
sudo pacman -S ncdu


We use the zypper command:
sudo zypper in ncdu

CentOS/RHEL/Fedora Linux

Turn on EPLE repo for RHEL/CentOS and then run the yum command:
sudo yum install ncdu
Fedora Linux user simply run the dnf command:
sudo dnf install ncdu

macOS install ncdu

First, install Homebrew on macOS to use the brew package manager and then type:
brew install ncdu
Or use the following when using macports:
sudo port install ncdu

FreeBSD Unix install ncdu

Type the following pkg command:
sudo pkg install ncdu

OpenBSD installing ncdu

Execute the pkg_add command:
doas pkg_add ncdu

How to use the ncdu command

The basic syntax for ncdu is:
ncdu [options] [directories]

The items in the square brackets are optional. When used with no options or arguments, ncdu shows the names and space consumption of each of the directories that begin with the current directory:

Press q to exit to the shell. The ncdu can give information about any directory trees. For example:
ncdu /etc/
ncdu /tmp/
ncdu /nfs
ncdu $HOME


We can enable extended information mode by passing the -e:
ncdu -e
When ou want to scan a full filesystem, your root filesystem, for example, then you’ll want to pass the -x:
sudo ncdu -x /
On large Unix and Linux file servers, scanning a whole directory may take a while. So what you can do is review a directory and export the results for later viewing:
sudo ncdu -1xo- / | gzip >my_root_export.gz
Later after some time, we can use zcommands to read gzip compressed text files on a fly and pipe it out to the ncdu as follows:
ls -l my_root_export.gz
zcat my_root_export.gz | ncdu -f-

The -f option load the given file, which has earlier been created with the -o option. If FILE is equivalent to -, the file is read from standard input (pipe).
It is also possible to scan a system remotely using the ssh command. Then browse through the files locally:
ssh -C [email protected] ncdu -o- / | ncdu -f-
ssh -C [email protected] ncdu -o- / | ncdu -f-

Turn on color option:
ncdu --color dark
ncdu --color dark -x /

We can exclude files that match PATTERN:
ncdu --exclude '*.c'
ncdu -x --exclude '/dir1' --exclude '/dir2' /

Follow symlinks and count the size of the file they point to:
ncdu -L
ncdu -L dir1

Keyboard shortcuts

Always press ? to get help about keys
Key Description
up, k Move cursor up
down, j Move cursor down
right/enter Open selected directory
left, <, h Open parent directory
n Sort by name (ascending/descending)
s Sort by size (ascending/descending)
C Sort by items (ascending/descending
M Sort by mtime (-e flag)
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
m Toggle display of latest mtime (-e flag)
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

Summing up

I found ncdu as a convenient alternative for finding the sizes of files and directory trees using a TUI method. However, this tool is not installed by default and is not required on the production Linux or Unix server. However, it does provide reporting that du provides in neat format. Hence, we can use it on workstations or even on servers to find space hogs. Personally, I will only use it on my desktop, but your mileage may vary. Make sure you checkout project home page too.

Source link

- Advertisement -

More articles


Please enter your comment!
Please enter your name here

Latest article