What is Ncdu?
Ncdu
(NCurses Disk Usage) is a command-line based disk usage analyzer. The tool provides a fast and efficient way to find out which files and directories consume the most space on your disk. By using ncdu, you can get a clear overview of disk usage, making it easier to determine which files or directories need to be cleaned up to free space.
How to Install Ncdu
Before diving into how to use ncdu, you’ll need to have it installed on your system.
On most Linux distributions, you can use the native package manager:
- Ubuntu/Debian:
sudo apt-get install ncdu
- CentOS/Red Hat:
sudo yum install ncdu
- Fedora:
sudo dnf install ncdu
- Arch Linux:
sudo pacman -S ncdu
How to Use Ncdu
Using ncdu is straightforward. After installation, navigate to the directory you want to scan and type ncdu
in the terminal.
For example, to analyze the home directory:
cd ~
ncdu
Ncdu will scan the directory and display the results in a hierarchical, easy-to-read manner. You’ll see directories listed in descending order based on their size, with the largest directories listed first.
Key commands within ncdu:
↑
and↓
arrows: Navigate the list.Enter
: Dive into a directory.←
: Go back to the parent directory.d
: Delete the selected file or directory (be cautious with this command).n
: Sort by name (default is by size).s
: Toggle between showing size and items.g
: Toggle between showing items and apparent size.q
: Quit ncdu.
Tips and Tricks
- Analyze a Directory Without Entering It: You can specify the directory you want to scan as an argument. For example,
ncdu /var
will scan the /var directory. - Exclude Directories: Using the
-X
or--exclude
option, you can exclude specific directories. For example,ncdu -X Downloads
will exclude the Downloads directory. - Export and Import Results: You can export the scan results for later analysis using the
-o
or--output-file
option. Later, you can read the results with the-f
or--load-from-file
option. This can be helpful when you want to compare disk usage over time. - Use in Remote Sessions: Ncdu is especially useful when connected to a remote server using SSH. Since it’s a terminal-based application, it works seamlessly in this environment, allowing you to analyze and manage disk usage on remote machines easily.
- Limit Depth: If you only want to view directories up to a certain depth, use the
-d
or--max-depth
option. For instance,ncdu -d 1
will only show top-level directories.
Conclusion
Ncdu is a powerful tool for analyzing disk usage on Linux systems. Its intuitive interface, combined with its lightweight nature, makes it a favorite among sysadmins and regular users alike. By familiarizing yourself with its features and shortcuts, you can quickly and effectively manage and reclaim disk space on your system.