Fast Way To Analyze Filesystems And Calculate Directory Sizes With ncdu

ncdu 1.15.1 ~ Use the arrow keys to navigate, press ? for help 16.4 GiB [##########] /var 8.2 GiB [##### ] /var/log 5.7 GiB [### ] /var/lib Fast Way To Analyze Filesystems And Calculate Directory Sizes With ncdu 81% 56% ncdu -x /var

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.

Once launched, ncdu provides an interactive interface where you can:

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:

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

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.