Disk Usage Analysis with `du` and `ncdu`: A Complete Guide
Use du -hs * for quick, human-readable disk usage summaries and ncdu for interactive exploration to identify large files and directories efficiently.
The jlevy/the-art-of-command-line repository curates battle-tested command-line techniques for system administration and development. For disk usage analysis, the guide emphasizes two complementary approaches: the built-in du utility for rapid scripting and ncdu for visual navigation. Both tools appear in the main README as essential utilities for reclaiming storage space and understanding filesystem consumption.
Quick Disk Inspection with du
According to the source code in [README.md at line 68](https://github.com/jlevy/the-art-of-command-line/blob/master/README.md#L68), du serves as the standard tool for generating a "quick summary of disk usage." The recommended invocation uses two critical flags:
-h(human-readable): Displays sizes in K, M, G units instead of raw block counts.-s(summarize): Reports totals per argument without listing every subdirectory recursively.
Basic Usage Examples
For a fast overview of top-level directory consumption:
du -hs *
Typical output shows relative weights at a glance:
1.4G bin
56M docs
3.2G src
12K README.md
To check the total size of the current directory tree (including all nested folders):
du -sh .
Interactive Analysis with ncdu
When you need to drill down into directory hierarchies, the repository recommends ncdu at [README.md line 328](https://github.com/jlevy/the-art-of-command-line/blob/master/README.md#L328), noting it "saves time over the usual commands like du -sh *."
ncdu builds an ncurses-based interface that allows real-time navigation, sorting, and immediate deletion. Unlike du, which streams text output, ncdu calculates the entire tree once, then lets you explore interactively.
Installation and Launch
Install the tool using your package manager:
# Debian/Ubuntu
sudo apt-get install ncdu
# macOS (Homebrew)
brew install ncdu
Launch an interactive session in the current directory:
ncdu .
Navigation uses arrow keys to traverse directories, d to delete selected files, and q to quit.
Advanced du Patterns
For scripted analysis without interactivity, combine du with standard Unix utilities to surface the largest consumers.
Sort by Size
To identify the top 20 largest files and directories:
du -ah . | sort -hr | head -n 20
Output lists paths descending by size:
1.2G ./node_modules
540M ./build
210M ./venv
...
Excluding Specific Paths
Skip irrelevant filesystem branches like network mounts or Docker volumes:
du -h --max-depth=1 --exclude='./proc' --exclude='./sys' .
Summary
du -hs *provides immediate, human-readable summaries of current directory entries according to the project README.du -ah . | sort -hrsurfaces the largest files programmatically for automated reporting.ncdudelivers an interactive curses interface for visual exploration and on-the-spot cleanup, recommended for complex investigations.- Both tools are documented across language-specific READMEs (e.g.,
README-es.md) for international users.
Frequently Asked Questions
What is the difference between du and ncdu?
du is a standard Unix utility that outputs text-based disk usage reports suitable for scripting and piping to other commands. ncdu is a third-party tool that provides an interactive, navigable interface built on ncurses, allowing you to browse directories, sort by size dynamically, and delete files immediately without leaving the terminal.
How do I sort du output by size?
Pipe the output to sort with the -h flag for human-readable numeric sorting. The complete command is du -ah . | sort -hr | head -n 20, which lists all files and directories sorted from largest to smallest, limiting results to the top 20 entries.
Is ncdu available on macOS?
Yes. Install it via Homebrew using brew install ncdu. It functions identically to the Linux version, providing the same keyboard-driven interface for analyzing APFS or HFS+ volumes.
How can I see the total size of the current directory only?
Run du -sh .. The -s flag summarizes the entire directory tree into a single figure, while -h presents the result in human-readable units (e.g., 4.9G).
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →