Check free disk space in Linux with ncdu | Opensource.com

Check free disk space in Linux with ncdu

Get an interactive report about disk usage with the ncdu Linux command.

Check disk usage
Image credits : 

CC BY-SA Seth Kenlon

x

Subscribe now

Get the highlights in your inbox every week.

Computer users tend to amass a lot of data over the years, whether it's important personal projects, digital photos, videos, music, or code repositories. While hard drives tend to be pretty big these days, sometimes you have to step back and take stock of what you're actually storing on your drives. The classic Linux commands df and du are quick ways to gain insight about what's on your drive, and they provide a reliable report that's easy to parse and process. That's great for scripting and processing, but the human brain doesn't always respond well to hundreds of lines of raw data. In recognition of this, the ncdu command aims to provide an interactive report about the space you're using on your hard drive.

Installing ncdu on Linux

On Linux, you can install ncdu from your software repository. For instance, on Fedora or CentOS:

$ sudo dnf install ncdu

On BSD, you can use pkgsrc.

On macOS, you can install from MacPorts or HomeBrew.

Alternately, you can compile ncdu from source code.

Using ncdu

The interface of ncdu uses the ncurses library, which turns your terminal window into a rudimentary graphical application so you can use the Arrow keys to navigate visual menus.

ncdu.jpg

ncdu interface

CC BY-SA Seth Kenlon

That's one of the main appeals of ncdu, and what sets it apart from the original du command.

To get a complete listing of a directory, launch ncdu. It defaults to the current directory.

$ ncdu
ncdu 1.16 ~ Use the arrow keys to navigate, press ? for help                                                                  
--- /home/tux -----------------------------------------------
   22.1 GiB [##################] /.var                                                                                        
   19.0 GiB [###############   ] /Iso
   10.0 GiB [########          ] /.local
    7.9 GiB [######            ] /.cache
    3.8 GiB [###               ] /Downloads
    3.6 GiB [##                ] /.mail
    2.9 GiB [##                ] /Code
    2.8 GiB [##                ] /Documents
    2.3 GiB [#                 ] /Videos
[...]

The listing shows the largest directory first (in this example, that's the ~/.var directory, full of many many flatpaks).

Using the Arrow keys on your keyboard, you can navigate through the listing to move deeper into a directory so you can gain better insight into what's taking up the most space.

Get the size of a specific directory

You can run ncdu on an arbitrary directory by providing the path of a folder when launching it:

$ ncdu ~/chromiumos

Excluding directories

By default, ncdu includes everything it can, including symbolic links and pseudo-filesystems such as procfs and sysfs. You can exclude these with the --exclude-kernfs.

You can exclude arbitrary files and directories using the --exclude option, followed by a pattern to match.

$ ncdu --exclude ".var"
   19.0 GiB [##################] /Iso                                                                                          
   10.0 GiB [#########         ] /.local
    7.9 GiB [#######           ] /.cache
    3.8 GiB [###               ] /Downloads
[...]

Alternately, you can list files and directories to exclude in a file, and cite the file using the --exclude-from option:

$ ncdu --exclude-from myexcludes.txt /home/tux                                                                                    
   10.0 GiB [#########         ] /.local
    7.9 GiB [#######           ] /.cache
    3.8 GiB [###               ] /Downloads
[...]

Color scheme

You can add some color to ncdu with the --color dark option.

ncdu-dark.jpg

ncdu color scheme

CC BY-SA Seth Kenlon

Including symlinks

The ncdu output treats symlinks literally, meaning that a symlink pointing to a 9 GB file takes up just 40 bytes.

$ ncdu ~/Iso
    9.3 GiB [##################]  CentOS-Stream-8-x86_64-20210427-dvd1.iso                                                    
@   0.0   B [                  ]  fake.iso

You can force ncdu to follow symlinks with the --follow-symlinks option:

$ ncdu --follow-symlinks ~/Iso
    9.3 GiB [##################]  fake.iso                                                                                    
    9.3 GiB [##################]  CentOS-Stream-8-x86_64-20210427-dvd1.iso

Disk usage

It's not fun to run out of disk space, so monitoring your disk usage is important. The ncdu command makes it easy and interactive. Try ncdu the next time you're curious about what you've got stored on your PC, or just to explore your filesystem in a new way.

Free disk space

Find out how much Linux disk space you have left with the df command.
Magnifying glass on code

Keep track of disk utilization with this handy list of commands.
Check disk usage

Find out how much disk space you're using with the Linux du command.
Sand dunes

The dust command is a more intuitive implementation of the du command written in Rust.

About the author

Seth Kenlon
Seth Kenlon - Seth Kenlon is a UNIX geek, free culture advocate, independent multimedia artist, and D&D nerd. He has worked in the film and computing industry, often at the same time. He is one of the maintainers of the Slackware-based multimedia production project Slackermedia.