Linux ncdu Command: Interactive Disk Usage Analyzer
The ncdu (NCurses Disk Usage) command is a lightweight,
terminal-based utility designed to analyze disk space utilization on
Linux systems. Serving as an interactive alternative to the standard
du command, it combines fast scanning capabilities with an
intuitive ncurses text-based user interface. This article explores how
ncdu functions, how to install and navigate it, and how to
use its core features to identify and clean up large files directly from
the command line.
What is the ncdu Command?
On Linux, locating directories consuming excessive storage
traditionally requires chaining commands like
du -sh * | sort -h. While effective, this process is static
and slow for deep directory hierarchies.
The ncdu command solves this problem by performing a
recursive scan of a specified directory and displaying the results in an
interactive, scrollable list. It presents directory sizes visually using
progress bars and absolute values, enabling system administrators to
quickly pinpoint disk hogs and manage files without leaving the
terminal.
Installing ncdu
Because ncdu is not typically installed by default, you
must install it using your distribution's package manager:
- Debian/Ubuntu:
sudo apt install ncdu - RHEL/CentOS/Rocky Linux:
sudo dnf install ncdu - Arch Linux:
sudo pacman -S ncdu
Basic Usage
To analyze the current working directory, run:
ncduTo scan a specific path, provide the target directory as an argument:
ncdu /var/logTo scan the entire root filesystem without crossing into other
mounted filesystems (such as network shares or external drives), use the
-x flag:
sudo ncdu -x /Running the command with sudo ensures that
ncdu can read system directories protected by root
permissions.
Navigating the Interactive Interface
Once the scan completes, ncdu provides an interactive
browser. You can navigate through your filesystem using standard key
bindings:
- Up/Down Arrows (or k/j): Move the cursor through files and directories.
- Enter (or Right Arrow): Open the selected directory.
- Left Arrow: Move back to the parent directory.
- d: Delete the currently selected file or directory (prompts for confirmation).
- i: Display detailed information about the selected item, including path, disk usage, apparent size, and inode count.
- n: Sort items alphabetically by name.
- s: Sort items by size (default view).
- c: Sort items by item count.
- q: Exit the interface.
Advanced Features
- Exporting Scan Results: For large remote servers,
running repeated interactive sessions can waste resources. You can scan
a directory once and export the output to a file:
ncdu -1xo- / | gzip > scan_results.json.gz - Importing Results: You can later open and inspect
the saved report locally without re-scanning:
zcat scan_results.json.gz | ncdu -f - - Read-Only Mode: To prevent accidental file deletion
within the interface, launch the utility with the
-rflag:ncdu -r /
The ncdu command is a vital tool for Linux disk
management, eliminating the complexity of manual pipeline scripts and
offering immediate visual clarity over storage consumption.