Linux du Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the du (disk usage) command in Linux to estimate file space usage and provide information about the disk space occupied by files and directories. You will understand the various options of the du command, measure the disk usage of a directory, and exclude directories from the du measurement. The lab covers the basic file and directory operations in Linux, which are essential for system administration and file management tasks.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/SystemInformationandMonitoringGroup -.-> linux/df("`Disk Space Reporting`") linux/SystemInformationandMonitoringGroup -.-> linux/du("`File Space Estimating`") subgraph Lab Skills linux/cd -.-> lab-422652{{"`Linux du Command with Practical Examples`"}} linux/ls -.-> lab-422652{{"`Linux du Command with Practical Examples`"}} linux/df -.-> lab-422652{{"`Linux du Command with Practical Examples`"}} linux/du -.-> lab-422652{{"`Linux du Command with Practical Examples`"}} end

Understand the du Command

In this step, you will learn about the du (disk usage) command in Linux. The du command is used to estimate file space usage, providing information about the disk space occupied by files and directories.

To get started, let's first check the du command's help:

man du

The help output will provide you with detailed information about the various options and usage of the du command.

Some of the commonly used du options include:

  • -h: Display the output in human-readable format (e.g., 1.2M instead of 1234567)
  • -s: Display the total size of a directory, instead of the size of each file/directory within it
  • -c: Display the grand total at the end of the output
  • -x: Do not cross file system boundaries
  • -d <depth>: Limit the depth of the directory tree that du will display

Now, let's try some examples to understand how to use the du command:

## Display the disk usage of the current directory
du -h .

## Display the disk usage of the current directory, including subdirectories
du -h -s *

## Display the disk usage of the current directory, limiting the depth to 1 level
du -h -d 1

Example output:

4.0K    .
4.0K    file1.txt
8.0K    file2.txt
12K     .

The output shows the disk usage for the current directory (.) and the files within it. The -h option displays the sizes in human-readable format.

Measure Disk Usage for a Directory

In this step, you will learn how to use the du command to measure the disk usage of a specific directory.

First, let's create a directory and some files to work with:

mkdir ~/project/test_dir
cd ~/project/test_dir
touch file1.txt file2.txt file3.txt

Now, let's use the du command to measure the disk usage of the test_dir directory:

## Display the disk usage of the test_dir directory
du -h ~/project/test_dir

## Display the total disk usage of the test_dir directory
du -hs ~/project/test_dir

Example output:

12K     /home/labex/project/test_dir
12K     /home/labex/project/test_dir

The first command, du -h ~/project/test_dir, displays the disk usage of each file and subdirectory within the test_dir directory. The -h option shows the sizes in human-readable format.

The second command, du -hs ~/project/test_dir, displays the total disk usage of the test_dir directory. The -s option shows the summary (total) size instead of the individual file/directory sizes.

You can also use the du command to measure the disk usage of a directory and its subdirectories recursively:

## Display the disk usage of the test_dir directory and its subdirectories
du -h -d 1 ~/project/test_dir

Example output:

4.0K    /home/labex/project/test_dir/file1.txt
4.0K    /home/labex/project/test_dir/file2.txt
4.0K    /home/labex/project/test_dir/file3.txt
12K     /home/labex/project/test_dir
12K     /home/labex/project/test_dir

The -d 1 option limits the depth of the directory tree to 1 level, showing the disk usage of the test_dir directory and its immediate files/subdirectories.

Exclude Directories from du Measurement

In this step, you will learn how to exclude specific directories from the du command's disk usage measurement.

Let's create a new directory structure to demonstrate this:

mkdir -p ~/project/main_dir/subdir1 ~/project/main_dir/subdir2
touch ~/project/main_dir/subdir1/file1.txt ~/project/main_dir/subdir2/file2.txt

Now, let's use the du command to measure the disk usage of the main_dir directory, including its subdirectories:

du -h -d 1 ~/project/main_dir

Example output:

4.0K    /home/labex/project/main_dir/subdir1
4.0K    /home/labex/project/main_dir/subdir2
8.0K    /home/labex/project/main_dir
8.0K    /home/labex/project/main_dir

As you can see, the du command includes the disk usage of the subdir1 and subdir2 directories.

If you want to exclude specific directories from the du measurement, you can use the --exclude option:

du -h -d 1 --exclude=subdir1 ~/project/main_dir

Example output:

4.0K    /home/labex/project/main_dir/subdir2
4.0K    /home/labex/project/main_dir
4.0K    /home/labex/project/main_dir

In this example, the --exclude=subdir1 option tells du to exclude the subdir1 directory from the disk usage measurement.

You can also exclude multiple directories by using the --exclude option multiple times:

du -h -d 1 --exclude=subdir1 --exclude=subdir2 ~/project/main_dir

Example output:

0   /home/labex/project/main_dir
0   /home/labex/project/main_dir

In this case, since we excluded both subdir1 and subdir2, the total disk usage of the main_dir directory is reported as 0.

Summary

In this lab, you learned about the du (disk usage) command in Linux, which is used to estimate file space usage and provide information about the disk space occupied by files and directories. You explored the commonly used du options, such as -h for human-readable output, -s for displaying the total size of a directory, and -d for limiting the depth of the directory tree. You then practiced using the du command to measure the disk usage of a specific directory, both for the directory itself and for the individual files within it.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like