How to Analyze Disk Usage with 'ncdu' (with examples)
Ncdu is a powerful disk usage analyzer that provides an intuitive interface using the ncurses library. It’s particularly useful for administrators and users needing to efficiently identify and manage disk space usage across directories. Ncdu presents an interactive view of disk usage, allowing you to navigate through directories and see the space each occupies, making it easier to pinpoint bloat and optimize storage.
Use case 1: Analyze the current working directory
Code:
ncdu
Motivation:
This command is perfect for users who need to quickly assess how space is being utilized within their current working directory. When you’re unsure about where disk space is being consumed, running ncdu
without any arguments provides a straightforward readout of the current directory’s disk usage.
Explanation:
ncdu
: Executes the ncdu program to analyze the disk usage of the current working directory. When invoked without additional parameters, it defaults to showing the directory you’re presently navigating.
Example Output:
--- /home/user ----------------------------------
10.0GiB [##########] /Documents
8.0GiB [####### ] /Pictures
2.0GiB [## ] /Downloads
500.0MiB [ ] /Music
200.0MiB [ ] /Desktop
Total disk usage: 20.7GiB
Use case 2: Colorize output
Code:
ncdu --color dark
Motivation: Colorizing the output can make it easier to distinguish between different levels of disk usage by indicating them with colors. Users who prefer a visual enhancement for better readability can benefit from this feature, especially when working with many directories and files.
Explanation:
--color dark
: This argument specifies that the output should be colorized using a dark theme, enhancing readability and helping users immediately pick out the largest directories and files based on color intensity. Alternatively,off
can be used to disable color output altogether.
Example Output:
--- /home/user ----------------------------------
10.0GiB [##########] /Documents
8.0GiB [####### ] /Pictures
2.0GiB [## ] /Downloads
Where directories may appear in different colors based on their size.
Use case 3: Analyze a given directory
Code:
ncdu path/to/directory
Motivation: Analyzing a specific directory is critical when you need more focused insight into particular folder hierarchies. This is particularly useful for system administrators or users aiming to debug space issues within distinct segments of their file systems rather than the whole filesystem.
Explanation:
path/to/directory
: Replace this placeholder with the path of the directory you want to analyze. Ncdu will then specifically target this directory to provide a breakdown of its disk usage.
Example Output:
--- /var/log ------------------------------------
5.0GiB [##########] 2023_logs
3.2GiB [####### ] 2022_logs
700.0MiB [# ] application_logs
80.0MiB [ ] system_logs
Use case 4: Save results to a file
Code:
ncdu -o path/to/file
Motivation: Saving results to a file is invaluable for record-keeping, reporting, and analyzing disk usage trends over time. Users can revisit these saved files to compare with future ncdu outputs, helping track improvements or regressions in storage usage.
Explanation:
-o
: This flag indicates that the output should be saved to a specified file.path/to/file
: This is the destination file where ncdu will save the disk usage report. The content is stored in a format thatncdu
can later read for review.
Example Output:
The file specified will contain a serialized report of the disk usage analysis, not directly human-readable but intended for use with ncdu
.
Use case 5: Exclude files that match a pattern
Code:
ncdu --exclude '*.txt'
Motivation: Excluding files based on a pattern is useful for focusing the disk usage report on only the most relevant files. This allows users to ignore files that they know are not contributing significantly to space issues, streamlining the analysis process.
Explanation:
--exclude '*.txt'
: Tells ncdu to skip over files matching the given pattern. The argument can be specified multiple times to exclude multiple patterns, providing flexibility in tailoring the analysis to specific needs.
Example Output:
--- /home/user ----------------------------------
9.5GiB [##########] /Documents
8.0GiB [####### ] /Pictures
Excluded 5.2MiB in 10 files
Conclusion:
Ncdu is an invaluable tool for efficiently managing disk space through detailed analysis and an intuitive interface. Whether you’re inspecting the current working directory, a specific path, or tailoring the report by excluding certain files, ncdu provides a versatile toolkit for every scenario. It can enhance productivity and efficiency by giving users the power to visualize and navigate their storage use in a manner that aligns with their specific needs, ensuring optimized management of disk resources.