Computer Science Atlas
Tutorial

How to Check Directory Disk Space Usage on Ubuntu

January 31, 2021
 

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:

Bash
Copy
du -h -d 0
$
du -h -d 0

You should see output like:

Out
Copy
74M     .
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:

Bash
Copy
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:

Bash
Copy
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:

Bash
Copy
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:

Bash
Copy
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:

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

you can add sudo to the beginning of the command:

Bash
Copy
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:

Bash
Copy
du -hd 0 --apparent-size
$
du -hd 0 --apparent-size