Fast tools for checking disk utilization

Disk Usage

© Lead Image © arcoss, 123RF.com

© Lead Image © arcoss, 123RF.com

Article from Issue 251/2021
Author(s):

Three modern tools, gdu, godu, and duf, make the task of checking the utilization level of hard disks easier thanks to fast execution speed and a good graphical implementation.

Hard disks have three states: empty, dead, and full. You can reach a full disk faster than expected due to carelessly hoarded data, much like in a kitchen junk drawer. Error messages logged once a millisecond, such as .xsession-errors, can also quickly fill up a logfile causing even large hard disks to overflow overnight.

If your disk is filling up, you first need to identify the cause, specifically which directories or files are hogging the most space. Plasma and Gnome have graphical tools such as Filelight, GdMap, or Baobab, which display the utilization level. However, these tools are very slow, making them unsuitable for network servers or full hard disks, because a graphical user interface will often fail to launch if the disk utilization level exceeds 95 percent.

Instead, you need a terminal-based solution. Linux on-board tools, such as du or df can show disk utilization, but they are not necessarily renowned for their clarity. For example, you can use the du command in Listing 1 to display the 20 largest directories in home but in a pretty awkward way. Another option is ncdu [1], an Ncurses-based tool available in the archives of all major distributions, which I covered in an earlier issue [2]. While ncdu does a fantastic job, it lacks speed during indexing.

Listing 1

Checking with Du

$ du -a /home/ | sort -n -r | head -n 20

If you are looking for speed, three lesser-known tools can do the job: gdu [3], godu [4], and duf [5]. These three tools replace the ncdu tool completely or partially and also do their job at lightning speed. All three owe their speed to the Go programming language in which they are written (ncdu is written in C). All three tools are optimized for SSD use, which also makes full use of parallel processing. According to the gdu developer, the performance gain is lower with HDDs but still noticeable.

gdu

Currently the most popular, gdu can be found in the archives of Debian, Devuan, Manjaro, PureOS, and Raspberry Pi OS. You will also find packages for 32- and 64-bit Linux, ARM, and many BSD variants on gdu's website. Arch Linux users will find gdu in the Arch User Repository (AUR).

If you use Snap, you can install gdu with the command-line call from the first line of Listing 2; the project's GitHub page reveals the further steps. If your distribution does not have gdu, the tool can also be installed quickly at the command line (lines 2 to 4).

Listing 2

Installing gdu

01 $ sudo snap install gdu-disk-usage-analyzer
02 $ curl -L https://github.com/dundee/gdu/releases/latest/download/gdu_linux_amd64.tgz | tar xz
03 $ chmod +x gdu_linux_amd64
04 $ sudo mv gdu_linux_amd64 /usr/bin/gdu

First launching with gdu -h lists the startup parameters. gdu -d shows you all the mounted partitions with their sizes and the used and free space (Figure 1). The -i parameter excludes directories, and you can view the history log with gdu -l.

Figure 1: The gdu -d command gives you an overview of the mounted disks and the partition usage.

Normally, gdu runs in interactive mode, which can be switched off for use in scripts with -n. If you prefer monochrome, -c disables the colored display. To navigate, you use the arrow keys in gdu, just like in ncdu. Pressing Enter or the right arrow key lets you change to the selected directory, while the left arrow key moves you up one directory level again. Pressing Q returns to the terminal.

If you press ?, gdu lists the available options (Figure 2). Table 1 shows more keyboard shortcuts. If you call gdu without parameters, it shows the contents of the current working directory. By specifying a path, you can steer gdu precisely to a location in the filesystem without being in that directory (Figure 3).

Table 1

gdu Keyboard Shortcuts

Key

Function

Up arrow

Move up in the output

Down arrow

Move down in the output

Right arrow

Change to selected directory

Left arrow

Move one directory level up

Enter

Change to selected directory

A

Change actual size/occupied space

C

Sort by number of contents

D

Delete selected file

J

Move down in the output

K

Move up in the output

L

Change to selected directory

N

Sort by file name

Q

Quit gdu

R

Recompute selected element

S

Sort by file size

Figure 3: After specifying the path, gdu shows you the contents for a specific directory.
Figure 2: Pressing ? displays a help window.

The only destructive parameter, D, deletes the selected file or directory. As an alternative to the arrow keys, use K to move up and J to move down. L moves you to the selected directory.

Overall, gdu leans heavily on ncdu in both appearance and operation, though ncdu has more options. The biggest difference, however, is in speed. ncdu took 7.4 seconds to scan a home directory with around 280GB of content on an NVMe disk in the test, while gdu completed the same task within milliseconds – so quickly that the duration could not be measured with a stopwatch. It would be interesting to compare the two tools on an HDD, but there was no HDD available for the test.

godu

Pursuing the same goal, godu takes a slightly different approach. The difference lies in the representation: godu uses Miller columns [6] to allow for more flexible visualization (Figure 4). The columns let you open multiple hierarchy levels at the same time and provide a better overview of where you are in the directory tree.

Figure 4: To accommodate as much content as possible in the terminal, godu uses Miller columns to display the content.

The easiest way to install godu on your machine is to download the appropriate archive containing the binary [7], which you then unzip and move to ~/.local/bin/. If this directory does not already exist, create it, and add it to the path by adding the command in Listing 3 to ~/.bashrc. A subsequent source ~/.bashrc activates the change. If you want all users to use godu, store it in /usr/local/bin/. Other installation methods include Homebrew or a Golang environment. However, the latter can be tricky for nonexpert users.

Listing 3

Supplementing the Path

export PATH="/home/$USER/.local/bin:$PATH"

Typing godu -h reveals that godu provides only one parameter apart from the help and the version information: godu -l lets you exclude files if they are below a specified size. For example,

godu -l 100 /

scans the entire file tree but only considers files larger than 100MB. gdu lacks this convenient option. If you do not specify anything else, godu uses 10MB as the limit for -l.

If you want to exclude certain directories from the display, enter them in the .goduignore file in the home directory, which you need to create for this purpose. This can also speed up content scanning, especially if you exclude directories with a large number of small files.

Navigation in godu also relies on the arrow keys. Up to three levels can be opened side by side. If you move further down the directory tree, the level shown on the left disappears and makes room for a new one on the right (Figure 5). Pressing the space bar lets you select several directories or files for deletion or moving.

Figure 5: If you click on a directory in the right column above, it opens to the right, while godu hides the left column to make the necessary space.

To delete or move the selected entries, exit godu by pressing Q and use

godu -print0 | xargs -0 COMMAND

where COMMAND is the appropriate command for the desired action. For example, to move previously selected entries, use the command shown in line 1 of Listing 4. To simplify the process, you can create an appropriate alias in .bashrc (line 2).

Listing 4

Moving Selected Entries

01 $ godu -print0 | xargs -0 -I _ mv _ <I>Ziel<I>
02 $ alias gmv="godu -print0 | xargs -0 -I _ mv _ "

godu provides a better overview by outputting all previously selected objects for checking in the standard output after exiting. It is worth looking through this list carefully before you let xargs loose on it.

duf

The third tool, duf, stands for Disk Usage/Free. It differs from gdu and godu both in terms of operation and how it displays the results. duf is available for Alpine, NixOS, and Void Linux in their respective repositories; on Arch Linux, you will find duf in the AUR. The developer also offers a variety of binaries on GitHub [8] for various Linux and BSD distributions, macOS, Windows, and ARM. duf also runs on Android. To try this, enter the command

pkg install duf

in Android's Termux app.

At first glance, entering duf without any further parameters results in impressive output. Of the three tools, duf offers the clearest and most detailed view of the mounted devices, distinguishing between local and network devices. As a third category, duf displays directories mounted in memory via tmpfs. The -all parameter expands the display again to include mounted FUSE devices, as well as the pseudo filesystems under /proc and /sys (Figure 6).

Figure 6: duf provides the best overview of all mounted filesystems and shows virtual filesystems in addition to local partitions and network devices.

Entering duf -h shows more than a dozen other ways to shape the output. duf -hide AREA lets you hide individual areas, while duf -only AREA only shows certain areas. The default dark display can be changed to a light theme with duf -theme light.

You do not navigate with duf; you just define the mount point view via the options at startup. The output is sorted using the --sort and --output parameters, to which you can assign keywords that you will find in the help if needed. For example, duf --sort size sorts the mount points by size, while duf --output mountpoint simply lists the mount points without any other information.

duf -json gives you output that can be further processed with other applications (Figure 7). However, data manipulation is left to gdu and godu; duf does not offer an interactive mode.

Figure 7: The duf -json command prepares the requested data in JSON format allowing for downstream processing with other tools.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Ncdu

    Ncdu adds some GUI-like features to the classic du command.

  • Charly's Column

    I’ve never heard of an admin having to remove disks from a server because of a chronic lack of data, but full disks are part of the daily grind. The du clone ncdu will help slim down your data.

  • broot

    The broot file manager guarantees clearer, quicker navigation of the directory tree at the command line.

  • 3D File Browsers

    Moving a flat filesystem hierarchy to the third dimension makes navigating a directory tree child's play.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News