Ncdu Command in Linux: Disk Usage Analysis with Examples
This article is a follow-up to the previous 90 Linux Commands frequently used by Linux Sysadmins post. As time allows, I will continue to publish articles on each of these 90 commands, geared toward Linux sysadmins and Linux power users.
Efficient disk space management is a key aspect of Linux system administration. The ncdu
(NCurses Disk Usage) command offers a significant improvement over the traditional du
command, providing a more interactive and user-friendly approach to analyzing disk space.
Table of Contents
What is Ncdu?
ncdu
is a command-line utility in Linux that delivers an interactive and detailed view of the space occupied by files and directories. It stands out for its ease of use and interactive interface, making it a popular choice for system administrators and users who need a straightforward method to manage disk space.
Basic Command Structure
ncdu [options] [directory]
Initiate ncdu
by navigating to the desired directory and executing:
ncdu
This command scans the directory and displays a list of files and subdirectories, along with their respective sizes.
You can also use it this way:
ncdu /home
This command initiates a scan of the /home
directory, presenting an interactive interface to view and navigate through the disk usage of each file and folder within it.
ncdu examples
Analyzing Specific Directories: Direct ncdu
to analyze a particular directory by specifying its path:
ncdu /var/www/html
This command is useful for monitoring disk usage in web server directories.
Sorting Output: Sort the output by size, name, or modification time using the -o
option:
ncdu -o
Exporting and Importing Scan Results: Save scan results for later review:
ncdu -o results.ncdu /path/to/directory
To read these results at a later time:
ncdu -f results.ncdu
Excluding Files or Directories: Exclude certain files or directories from the scan, useful for omitting non-essential items:
ncdu --exclude '/path/to/exclude'
Using ncdu
offers several advantages:
- Time Efficiency: Facilitates quick identification of large files or directories.
- Resource Optimization: Aids in server storage management.
- Ease of Use: More user-friendly than the traditional
du
command.
You can interact with the ncdu interface using the supported keys:
↑,↓ or k,j to Move →,l to enter ←,h to return g toggle graph c toggle counts a toggle average size in directory m toggle modified time u toggle human-readable format n,s,C,A,M sort by name,size,count,asize,mtime d delete file/directory v select file/directory V enter visual select mode D delete selected files/directories y copy current path to clipboard Y display current path ^L refresh screen (fix screen corruption) r recalculate file sizes ? to toggle help on and off q/ESC/^c to quit
Conclusion
The ncdu
tool is an invaluable resource for disk space management on Linux systems. Its interactive approach, makes it a great utility for Linux storage administration tasks. It simplifies disk usage analysis, catering to the needs of both novice and experienced Linux users.
ncdu
is a great tool but it sadly lacks the feature to compare reports, which for example is very useful for image size comparison if you run your own image pipeline. If you need this feature then you can use gdu + gdu-diff.Welcome to the Linux community @misc Thanks for contributing this useful alternative! I’ve not heard of gdu, but will be checking it out.
Hiya. That looks handy. I didn’t notice mention of how to navigate folder though. Would it just be the usual ENTER & Backspace keys?
This is great. Imma try out the GDU for sure.
I’ve been using Ncdu for such a long time but that GDU looks promising.
The NCDU command is almost like life to any Linux user. It is practically impossible to not want to know the files that are hogging up space on your Linux PC. That’s where NCDU comes to play. It can even be used to search for specific kinds of files in a particular directory with a typical command like this
ncdu --exclude ‘*.jpg’.
Adding a file extension to a command like that would exclude the file extension added from the directory search results, showing you only files without that extension that you want to see.