Computer Science Atlas
Step-By-Step

Ubuntu: Check Directory Disk Space Usage

January 31, 2021
 
Table of Contents

Overview

This tutorial explains how to show the total amount of disk space a specific directory is using (including all subdirectories).

To find out how big each individual file is, see this tutorial.

To find out the total disk space usage by an entire disk volume, see this tutorial.

Step 1. Open a Terminal Session

If you're using an Ubuntu laptop or desktop, you can press Ctrl + Alt + T on your keyboard to open a new terminal window. If you're using a remote Ubuntu server, you can connect using SSH to open a new terminal session.

Step 2. Use the du Command

Total Usage for Current Directory

To check disk space usage for the current directory:

du -h -d 0
$ du -h -d 0

You should see output like:

74M     .

The -h option tells the command to show human-friendly units such as M for megabytes, G for gigabytes, etc.

The -d 0 option tells the command to show output for "depth level 0" - i.e., only the total amount used by the directory, not individual listings for subdirectories.

As usual, you can also combine the options and get the same output:

du -hd 0
$ du -hd 0

All Depth Levels

If you also want to see disk usage by all subdirectories in the current directory, you can run:

du -h
$ du -h

Specific Depth Levels

If you want to limit the subdirectories listed to just the immediate subdirectories, you can use depth level 1:

du -hd 1
$ du -hd 1

You can keep increasing the depth levels to show subdirectories of subdirectories, and so on.

Different Directories

If you want to see information for directories other than your current directory, you can just list the directory paths at the end of the command. For example, to see disk usage by the three directories /home/, /etc/, and /usr/local/bin you can run:

du -hd 0 /home /etc /usr/local/bin
$ du -hd 0 /home /etc /usr/local/bin

If you see a "permission denied" message for some directories like:

du: cannot read directory '/etc/sudoers.d': Permission denied

you can add sudo to the beginning of the command:

sudo du -hd 0 /home /etc /usr/local/bin
$ sudo du -hd 0 /home /etc /usr/local/bin

File Sizes vs. Disk Space Usage

Because of how Linux file systems store data, a file whose size is x bytes may take up more or less than x bytes of disk space. For example, a 6-byte file storing the word "hello" may take an entire 4 KB block of disk space.

To get the total sum of all actual file sizes in the current directory (as opposed to disk usage), you can call du with the --apparent-size option:

du -hd 0 --apparent-size
$ du -hd 0 --apparent-size