Americas

  • United States
sandra_henrystocker
Unix Dweeb

Using the Linux ncdu command to view your disk usage

How-To
Apr 11, 20235 mins
Linux

The ncdu command provides a convenient way to review files and the disk space being used on Linux systems, but the file sizes may appear a little strange at first.

data files
Credit: Dell

The ncdu command provides a fast and very easy-to-use way to see how you are using disk space on your Linux system. It allows you to navigate through your directories and files and review what file content is using up the most disk space. If you’ve never used this command, you’ll likely have to install it before you can take advantage of the insights it can provide with a command like one of these:

$ sudo dnf install ncdu
$ sudo apt install ncdu

The name “ncdu” stands for “NCurses disk usage. .It uses an ncurses interface to provide the disk usage information. “Curses”, as you probably know, has no connection to foul language. Instead, when related to Linux, “curses” is a term related to “cursor” – that little marker on your screen that indicates where you are currently working. Ncurses is a terminal control library that lends itself to constructing text user interfaces.

Using ncdu

To start ncdu on the command line, simply type “ncdu”. You’ll see a display that looks something like what is shown below once it has run a series of commands to quickly gather the needed file size data to generate the listing for you.

ncdu 1.17 ~ Use the arrow keys to navigate, press ? for help
--- /home/shs ----------------------------------------------------------------
   61.3 GiB [###########] /fampix
    5.1 GiB [           ] /pix
    1.5 GiB [           ] /.cache
  483.1 MiB [           ] /.local
  311.3 MiB [           ] /videos
  152.9 MiB [           ] /nextjs
   85.3 MiB [           ] /.mozilla
   78.2 MiB [           ] /homebrew
   59.4 MiB [           ] /Desktop

You can navigate up and down the listing using your down arrow and up arrow keys. If you press the enter key when you’re sitting on a line that represents a directory, you will move into that directory. Later, you can press the left-pointing arrow key to move back into the former directory.

The ncdu command is one that will list the files and directories in your current file system location in size order. However, it displays file sizes based on a binary-based numbering system with file sizes like 5.1 GiB, 10.2 MiB and 23.9 KiB. The second section below compares these measurements to numbers based on powers of 10 (our normal numbering system). These tebibyte (TiB), gibibyte (GiB), mebibyte (MiB) and kibibyte (KiB) numbers are all based on powers of two. They are close in size to terabytes, gigabytes, megabytes and kilobytes, but are all significantly larger. One kibibyte equals 1,024 bytes – larger than kilobyte at 1,000 bytes. One gibibyte equals 1,073,741,824 bytes – quite a bit larger than a gigabyte at 1,000,000,000.

Other options

The ncdu tool provides a number of additional options. For example, you can switch to listing files by name by pressing “n”, and you can delete a file by pressing “d” and responding to the request for confirmation. You can pull up more information on a file or directory by moving to its name in the listing and pressing “i”. You can toggle between viewing file sizes and disk usage by pressing “a”. The display will begin with the potentially larger disk usage sizes. The sizes shown will often differ because disk usage counts the full size of the last block even if the file isn’t fully using it.

If you start ncdu with the -e option, you can type “m” to toggle between the normal view and adding file update dates and times to the display.

Check the command’s man page for additional options.

Understanding the file sizes

So, what is the difference between the two sets of file sizes – those you see when using ls -l on the command line and those you see when using ncdu? The numbers used by ncdu are all based on powers of two (actually all powers of 1024). Where one GB is defined as 1000³ (1,000 x 1,000 x 1,000), one GiB is 1024³ (1,024 x 1,024 x 1,024).

The list below shows how all of these numbers compare.

  • 1 TB is 10004 where 1 TiB equals 10244 or (1,000,000,000,000 vs 1,099,511,627,776)
  • 1 GB is 10003 where GiB is 10243 (1,000,000,000 vs 1,073,741,824)
  • 1 MB is 10002 where MiB is 10242 (1,000,000 vs 1,048,576)
  • 1 KB is 1000 where KiB is 1024 (1,000 vs 1,024)

Here’s a calculation to make the size differences clear. It shows that one GB is only 93% or so of one GiB.

$ echo "scale=2; 1000000000 / 1073741824" | bc
.93

In the calculation above, we divide one GB (1,000,000,000 bytes) by one GiB  (1,073,741,824 bytes). That shows that one GB equals 0.93 GiB. However, it limits the number of decimal places to 2 with the “scale” parameter. Adding some additional decimal places shows the difference is actually somewhat larger:

$ echo "scale=5; 1000000000 / 1073741824" | bc
.93132

Doing the math

The for command below runs the calculations for one KiB, one MiB, one GiB and one TiB.

$ for n in 1 2 3 4
> do
>   echo "1024 ^ $n" | bc
> done
1024
1048576
1073741824
1099511627776

To generate the list of values with commas so that they’re easier to read, use a script like this one that makes use of the printf command:

#!/bin/bash

# generate list of powers of 1024
for num in 1 2 3 4
do
  echo "1024 ^ $num" | bc >> nums$$
done

# add commas to the results
n=0	
for num in `cat nums$$`
do
  ((n++))
  echo -n "1024^$n = "
  printf "%'d" $num
  echo
done

Running the script will generate output like this:

$ powers_of_1024
1024^1 = 1,024
1024^2 = 1,048,576
1024^3 = 1,073,741,824
1024^4 = 1,099,511,627,776

Wrap-up

The ncdu command provides a convenient way to review your files and disk usage. Understanding the differences between a Mebibyte (MiB) and a Megabyte (MB) along with the other file sizes based on powers of 1024 shouldn’t be a problem as long as you grasp the significance of these numbers when looking at your files with the ncdu command.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author