Command-line Basics: Analyzing Disk Usage


If you’ve ever ran out of disk space, you know how terribly painful it can be. Applications will stop opening, indexing services crawl. If you have a swap file or partition configured, it will start to shred. Working with a remote server can be even more painful, especially if you don’t have any GUI monitoring services running. Fortunately, we can get everything we need from the command-line using commands like df and du.

Getting Started

This article will use the commands df and du to analyze disk and partitiona space as well as storage usage by files and directories. These commands are pretty standard on Unix-like operating systems like macOS and Linux.

Neither command requires elevated privileges, unless you are attempting to use du on a directory that you don’t have permission to. We’ll be working with files and directories that your user account should have access to and none of the commands are destructive in nature.

Disk Free

The df command allows you to get information about the physical disks and partitions of the system you are on. Running the command without any arguments will present you with a list of the disks, their total size, storage used and available, and the mount point on your system:

$ df

Will output something like this:

Filesystem     1K-blocks      Used Available Use% Mounted on
dev             16399220         0  16399220   0% /dev
run             16407880      1524  16406356   1% /run
/dev/nvme0n1p2 982940416 231329248 701610820  25% /
tmpfs           16407880     32584  16375296   1% /dev/shm
tmpfs           16407880         0  16407880   0% /sys/fs/cgroup
tmpfs           16407880   1698632  14709248  11% /tmp
/dev/nvme0n1p1    523248    107668    415580  21% /boot
tmpfs            3281576        84   3281492   1% /run/user/1000

The output on your system may look a bit different as there are different implementations of the df command. The output above is from the GNU version of df.

The numbers can be a bit intimidating as they can be pretty large and aren’t prettified up with commas or periods. To improve the output, we can add the -h or --human-readable flag to display the sizes in the power of 1024:

$ df -h

Which changes the output to:

Filesystem      Size  Used Avail Use% Mounted on
dev              16G     0   16G   0% /dev
run              16G  1.5M   16G   1% /run
/dev/nvme0n1p2  938G  221G  670G  25% /
tmpfs            16G   32M   16G   1% /dev/shm
tmpfs            16G     0   16G   0% /sys/fs/cgroup
tmpfs            16G  1.7G   15G  11% /tmp
/dev/nvme0n1p1  511M  106M  406M  21% /boot
tmpfs           3.2G   84K  3.2G   1% /run/user/1000

There is also the -H or --si that will use the powers of 1000 instead of 1024.

Because I’m a human, and prefer to save a few keystrokes whenever possible, I alias df to df -h in my shell configuration:

# Disk utility aliases
$ alias df='df -h'
$ alias du='du -h' # More on du below

If you’re working with a system that has a ton of directories or files, the inode (or index node) usage may be more important than the block size. If you would prefer to see the inode usage information, you can pass the -i or --inodes argument:

$ df -i

Which presents a similar set of information, but with inode information instead of block storage information:

Filesystem       Inodes   IUsed    IFree IUse% Mounted on
dev             4099805     418  4099387    1% /dev
run             4101970     819  4101151    1% /run
/dev/nvme0n1p2 62480384 2139558 60340826    4% /
tmpfs           4101970     180  4101790    1% /dev/shm
tmpfs           4101970      16  4101954    1% /sys/fs/cgroup
tmpfs           4101970  220083  3881887    6% /tmp
/dev/nvme0n1p1        0       0        0     - /boot
tmpfs           4101970      51  4101919    1% /run/user/1000

And assuming you’re a human, you can also include the -h argument to display the inode information in a more human-readable format.

Disk Usage

The du command allows you to see the disk usage information at a more micro level than that of the df command. Invoking the du command without any arguments will display the disk usage for the current directory.

If you would prefer to show the disk usage for a directory that isn’t the directory you’re current in, you can simply pass in the path:

$ du
$ du . # Same thing, just more explicit
$ du ~ # Explicitly analyzing your home directory
$ du /home/user # Even more verbose

If you were to run that command from your home directory, you’ll probably be greeted with a ton of output that quite frankly, is pretty intimidating and hard to sift though.

Some truncated results from my own system:

# ... ton of previous output
4712    ./.node-gyp/10.10.0/include
4720    ./.node-gyp/10.10.0
13584   ./.node-gyp
96132   ./.electron
88      ./.ssh
20      ./.parallel
36880032        ./vmware
20      ./Desktop
152     ./Pictures/Webcam
904     ./Pictures
4       ./Music
12      ./.steam
166449800       .

The command lists out every single directory, subdirectory and file and reports the amount of disk space that it’s occupying. The final line of the output is the total disk usage for the directory, at upper most level.

Similar to df, the du output defaults to the number of bytes. Both set of arguments, -h and --human-readable as as well as -H and --si are available, to help make the output more digestible:

$ du -h

Which outputs the information in the base of 1024:

# ... ton of previous output
4.7M    ./.node-gyp/10.10.0/include
4.7M    ./.node-gyp/10.10.0
14M     ./.node-gyp
94M     ./.electron
88K     ./.ssh
20K     ./.parallel
36G     ./vmware
20K     ./Desktop
152K    ./Pictures/Webcam
904K    ./Pictures
4.0K    ./Music
12K     ./.steam
159G    .

And if you’re more interested in the inode values than the block storage values, you can also use the -i or --inodes argument.

Even with the human-readable output, a large directory is going to be pretty noisy since every single directory, subdirectory and file is going to be reported on.

To help clean up the output a bit, we can limit the depth that is output by passing in the -d or --max-depth argument:

$ du -d 0 ~ # Grand total for the directory
$ du -d 1 ~ # Totals for files in the current directory
$ du -s ~   # Shorter version of -d 0

Depending on the size of the directory, limiting the max depth to traverse may cause things to appear like they are locked up since there will be a lack of visual output.

The du command isn’t limited to a single directory either, if you wanted to find out the disk usage for your home directory as well as /tmp you can do so quite easily:

$ du -s ~ /tmp


If you’re a macOS user and have tried to get the total size of a handful of files and/or directories from Finder, there’s a good chance you’ve wound up with a bunch of open Information dialogs.

Graphical tools can be quite convenient, but tend to fall short for power user scenarios. Generally speaking, they also tend to not be available on remote systems.

Commands like df and du don’t do much, but they do it well. Getting the size of files and disk usage tends to be a task that comes up when you’re low on space and need to get something resolved quickly.

These commands make it easy to identify problem areas in your system quick and efficiently and are generally available on Linux systems out of the box.

  Tweet It

🕵 Search Results

🔎 Searching...