Fast Way To Analyze Filesystems And Calculate Directory Sizes With ncdu
As a systems administrator or developer, understanding disk usage is crucial for maintaining healthy servers. Let’s explore how to efficiently analyze your filesystem with ncdu
and alternative commands.
What is ncdu?
ncdu
(NCurses Disk Usage) is a powerful, lightweight disk usage analyzer with an interactive interface. It provides a simple way to see which directories are consuming the most space on your system.
Installing ncdu
Getting started with ncdu
is straightforward:
For Debian/Ubuntu-based systems:
sudo apt install ncdu
For Red Hat/CentOS/Fedora systems:
sudo yum install ncdu
Using ncdu to Analyze Filesystem
The basic usage is simple - just provide a directory path:
ncdu /path/to/directory
Restricting to a Single Filesystem
One of the most useful features is the ability to restrict scanning to a single filesystem using the -x
option:
ncdu -x /var/log
This prevents ncdu
from crossing filesystem boundaries, which is particularly helpful when analyzing specific directories without including mounted volumes.
Navigating the ncdu Interface
Once launched, ncdu
provides an interactive interface where you can:
- Navigate with arrow keys
- Press Enter to descend into directories
- Press
d
to delete files (use with caution!) - Press
q
to quit
Alternative Commands for Disk Usage Analysis
While ncdu
offers a user-friendly interface, there are other useful commands for analyzing disk usage:
1. df - Display Free Disk Space
The df
command displays information about total disk space and available space:
df -h
The -h
option provides human-readable output with sizes in KB, MB, or GB.
For specific filesystems:
df -h /var
2. du - Disk Usage
The du
command estimates file space usage:
du -sh /var/log
Where:
-s
provides a summary (total) rather than individual file sizes-h
makes output human-readable
For a breakdown of subdirectories:
du -h --max-depth=1 /var/log | sort -hr
This shows the size of each immediate subdirectory and sorts the results from largest to smallest.
3. Finding Large Files with find
Locate large files directly:
find /var -type f -size +100M -exec ls -lh {} \; | sort -k5 -hr
This finds files over 100MB and displays them sorted by size.
When to Use Each Tool
- ncdu: Great for interactive exploration and when you need to navigate through directories
- df: Quick overview of filesystem usage and available space
- du: Simple command-line usage statistics, good for scripts
- find: Targeted search for specific file sizes
Conclusion
ncdu
provides a fast, interactive way to analyze disk usage on your systems. The -x
option makes it particularly useful for focused analysis without crossing filesystem boundaries. Combined with traditional tools like df
and du
, you now have a comprehensive toolkit for managing disk space on your servers.
Remember that disk space analysis is an essential part of system maintenance and can help prevent unexpected service disruptions caused by full disks.