How to Master the “du” Command with Examples

du-command

The “du” command (Disk Usage) reports the disk space utilized by directories or files. As a Linux user, one task is to analyze what is taking up space to help efficiently manage the disk space.

Using the du command is easy. Besides, it offers various options to help get a specific result when analyzing disk usage. This post discusses everything you need to know about the “du” command. Moreover, we will give various examples of utilizing the du command. Let’s begin!

du” Command Syntax and Options

The Linux du command follows the below syntax.

Here are some options for running the du command in Linux.

OptionDescription
-hIt is added to make the output human-readable.
-cIt is added to show the total disk usage for combined directories.
-aIt is included to give the disk usage for all files and directories, including those hidden.
-sIt is added to summarize the disk usage instead of individual information.
-timeIt displays the last modification for the directory or file.
-excludeIt is added to exclude some directories or files when generating the disk usage information.

Having presented the du command syntax and commonly used options, the next step is to cover examples of using the command.

du” Command in Linux with Examples

How you use the “du” command will depend on your goal. For instance, do you want to query the disk usage of files or directories? Again, do you want to see the summary report or know the total disk usage? These are some of the questions to guide your goal.

Here are different examples of using the “du” command in Linux.

1. “du” Command with No Option

When you run the du command with no option and without specifying the target file or directory, it will return the disk usage for the current working directory.

For instance, we are in the Desktop/ directory, and running the du command gives human-unfriendly outputs on space usage.

du-command

2. Check Disk Usage of a Specific Directory

Instead of checking the disk usage of your current working directory, you can add the path of the target directory or file you wish to analyze.

For this example, we analyze the Downloads/ directory without passing any option.

du-command-specify-path

3. “du Command Check Multiple Directories

The previous example demonstrates checking the disk usage of one directory by passing its path. How about checking multiple directories simultaneously?

In that case, pass each path in your du command like in the example below.

You will get disk usage information for all directories and sub-directories in the specified paths.

du-command-with-multiple-directories

4. Get Disk Usage for All Files and Directories

So far, we have yet to have a case where files have been included in the du command output. However, adding the -a option will display the disk usage information for all files and directories in the specified path.

Here’s an example.

du-command-all-files-and-directories

5. Display Human-Readable Output

Although we’ve run the “du” command in Linux using different options, it’s hard to tell what the numbers on the left represent.

Luckily, the –h option gives more light by making the output more human-friendly.

It will give the size metrics for each display output. That way, you can easily distinguish the disk usage by understanding the sizes.

du-command-human-readable

6. “du” Command: Show Only the Total Size of the Directory

Sometimes, you only want to know the total size occupied by a directory, excluding its sub-directories. To get this total size, use the -s option.

We’ve added the -h for better readability. Notice how we now get one output representing the total size occupied by our current working directory.

du-command-show-total-size

7. Display Total Size for Combined Directories

If you want to know the size two or more directories occupy, you can use the -c flag and specify the directories. For instance, we would run the command below to show the total size occupied by the two directories, excluding their sub-directories.

The output gives the total size for each directory, thanks to the -s option, then the total size, thanks to the -c option.

du-command-show-total-combined-size

8. Set Level for Sub-Directories When Checking Disk Usage

The -d or -max-depth=N allows specifying the N number of levels for the sub-directories to include in your disk usage report.

For instance, we would run the below command to get the disk usage for level 1 sub-directories in our current working directory.

'du'-command-set-level

9. Get the Last Modification Time

The touch command lets you check and change the last access and modification time for a directory or file. However, you can also get disk usage information showing the last time a file was modified.

This option is handy, especially when you want to know which recently modified files in a directory or sub-directory are taking up space.

Run it as demonstrated below.

"du"-command-get-last-modified

10. “du” Command Exclude Specific Files

Assuming you have numerous files in your target directory and want to add the -a option to get all their disk usage. Using the –exclude option, you can omit specific files based on their extension.

For instance, to exclude text files, we would run our command as follows.

du-command-exclude-files
Read Also: How to use the 'df' command to check disk filesystem usage in Linux.

Conclusion

This post has explained the “du” command and what tasks it helps you perform. Additionally, we’ve covered various examples of utilizing the du command in Linux to fetch different disk usage metrics on a Linux system. With that, you now understand how to analyze disk usage with the du command.

Found this helpful? - Share it!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top