Lesson 45: du Command
In this lesson, you'll learn how to use the du command to check disk usage of files and directories in Linux.
The Linux du (Disk Usage) is a standard Unix/Linux command used to check the disk usage information of files and directories on a machine.
The du command has many parameter options that can be used to get the results in many formats. It also displays the files and directory sizes in a recursive manner.
Let's explain some useful du commands with examples, which will help you determine the sizes of files and directories in Linux.
du Command Syntax
# du [OPTIONS] [FILE/DIRECTORY]
du Command Options
| Option | Description |
|---|---|
-h |
Display sizes in human-readable format (KB, MB, GB) |
-s |
Display only the total size of a directory |
-a |
Display disk usage of all files and directories |
-k |
Display sizes in 1024-byte (Kilobyte) blocks |
-m |
Count blocks in Megabyte units |
-c |
Display a grand total at the last line |
--exclude=PATTERN |
Exclude files matching a pattern |
--time |
Display the last modification time alongside usage |
1. Find the Size of a Directory
To obtain a summary of disk usage for a directory tree /home/ubuntu and its subdirectories, use the following command.
# du /home/ubuntu
4 /home/ubuntu/Templates
4 /home/ubuntu/Public
12 /home/ubuntu/.gnupg
4 /home/ubuntu/Videos
21504 /home/ubuntu/Documents
4 /home/ubuntu/Desktop
4 /home/ubuntu/Pictures/Screenshots
...
175840 /home/ubuntu
The output of the above command displays the number of disk blocks in the /home/ubuntu directory along with its sub-directories.
2. Find the Directory Size in Human-Readable Format
By employing the -h option with the du command, you can obtain results in a "Human Readable Format", which enables you to view sizes in units such as Bytes, Kilobytes, Megabytes, Gigabytes, and so on.
# du -h /home/ubuntu
4.0K /home/ubuntu/Templates
4.0K /home/ubuntu/Public
12K /home/ubuntu/.gnupg
4.0K /home/ubuntu/Videos
21M /home/ubuntu/Documents
4.0K /home/ubuntu/Desktop
184K /home/ubuntu/Pictures/Screenshots
188K /home/ubuntu/Pictures
...
173M /home/ubuntu