Introduction
In today’s fast-paced IT environments, keeping track of disk usage is essential. ncdu (NCurses Disk Usage) is a powerful, interactive tool designed to help system administrators quickly analyze and manage disk space. This guide covers everything from what ncdu is and how to install it, to real-world examples and advanced options.
1. What is NCDU?
ncdu stands for NCurses Disk Usage. It is a lightweight, terminal-based disk usage analyzer that provides a user-friendly, interactive interface to help you in several ways. It quickly scans directories to determine what is consuming disk space. It allows you to navigate directories using intuitive key commands. It also enables you to delete unwanted files or directories directly from the interface. This tool is especially useful on servers and systems where graphical applications are not available or practical.
2. Installation
Installation is straightforward and depends on your operating system or Linux distribution.
For Debian/Ubuntu, run the following commands:
sudo apt-get update
sudo apt-get install ncdu
For Red Hat/CentOS (ensure the EPEL repository is enabled), run:
sudo yum install epel-release
sudo yum install ncdu
For Fedora, run:
sudo dnf install ncdu
For Arch Linux, run:
sudo pacman -S ncdu
For macOS (using Homebrew), run:
brew install ncdu
3. Basic Usage
Running ncdu is as simple as typing ncdu
in your terminal. For example, if you want to scan the current directory, type:
ncdu
If you want to scan a specific directory, such as /var
, type:
ncdu /var
To scan the entire filesystem (which requires root privileges), type:
sudo ncdu /
When launched, ncdu scans the directory structure and displays an interactive list of files and directories sorted by size.
4. Navigating the Interface
Within ncdu, you navigate the interface using simple key commands. Use the Up and Down arrow keys to move through the list. Press Enter to drill down into a selected directory. To return to the previous directory, use the Left arrow key or Backspace. If you wish to delete a file or directory, press d (a confirmation prompt will appear). To quit ncdu, press q. If you need a reminder of all available commands, press ? or h to display the help screen.
5. Advanced Options and Commands
ncdu offers several command-line options to tailor its functionality. For example, if you want to restrict the scan to a single filesystem, use the -x
option as shown below:
ncdu -x /
To enable quiet mode, which reduces the amount of output (a useful feature for scripting), use the -q
option:
ncdu -q /var
If you prefer to run ncdu in read-only mode, which disables the deletion command to prevent accidental file removals, use the -r
option:
ncdu -r /home
You can output the scan results to a file for later analysis by using the -o
option:
ncdu -o scan.ncdu /var
Later, if you want to open a previously saved scan file, use the -f
option:
ncdu -f scan.ncdu
If you need ncdu to follow symbolic links (use this option with caution, as it may result in scanning large or recursive directories), use the -L
option:
ncdu -L /path/to/dir
6. Practical Use Cases for System Administrators
ncdu is very useful in real-world scenarios. For disk cleanup, it helps you identify unusually large directories or files so you can remove temporary or unnecessary data. For scheduled monitoring, you can save periodic scan reports using the -o
option and review trends over time. When troubleshooting slow system performance or disk space warnings, ncdu quickly pinpoints the location of large files or directories.
7. Tips and Best Practices
To use ncdu effectively, run it as root when necessary to obtain a complete disk usage overview, especially in directories with restricted permissions. Always review files carefully before deleting them; using read-only mode (-r
) during initial scans can help prevent accidental deletions. It is a good idea to incorporate ncdu into your regular maintenance routines to ensure efficient disk usage management. Lastly, spend some time with the help screen (press “?”) to familiarize yourself with all available commands and options.
Conclusion
ncdu is a versatile and powerful tool for system administrators looking to manage disk space efficiently. Its ease of use, interactive interface, and robust command-line options make it an indispensable part of your toolkit. Whether you are performing routine maintenance, troubleshooting disk space issues, or monitoring usage trends, ncdu provides the insights needed to keep your systems running smoothly.
Happy disk cleaning!
By following this guide, system administrators can leverage ncdu to optimize disk usage, reduce clutter, and maintain efficient system performance.