Oct 23, 2023 4 min read

Du Command in Linux

Use about du command in Linux with our step-by-step tutorial. The du command shows disk space used by specified files.

Du Command in Linux
Table of Contents

Introduction

Before we discuss Du command in Linux, let's first understand-What is Du Command ?

The du command, which stands for "disc usage," displays the estimated amount of disc space consumed by specific files or directories. It can be used to locate files and folders that are taking up a lot of disc space.

In this tutorial, you will use the du command and learn about it. We will also address a few FAQs on du command in Linux.

How to Use the du command

The du command has the following general syntax:

du [OPTIONS]... FILE...

The du will summarise disc usage of each file and subdirectory in the specified FILE if it is a directory. If no FILE is given, du will report the current working directory's disc use.

du displays the disc use of the specified file or directory and each of its subdirectories in bytes when run without any options.

du ~/Documents 

You may also use the du command to pass several files and directories as arguments:

du ~/Documents ~/Pictures ~/.zshrc

You'll get a message like "du: cannot read directory" if you run du on a file or directory for which you don't have permissions. You'll need to use sudo to prefix the command in this case.

We'll go over just a few of the most commonly utilized settings in du.

The -a option instructs du to report the total amount of disc space used by each file in the directory.

du -a ~/Documents 

In most cases, you'll want to show only the space used up by the specified directory in a human-readable way. Use the -h option to accomplish this.

For instance, to determine the entire size of /var/lib and all of its subdirectories, use the following command:

sudo du -h /var

We're using sudo because the root user owns most of the files and directories in the /var/lib directory, making them inaccessible to regular users. This is what the final output will look like:

Output
...
4.0K	/var/lib/apt/mirrors/partial
8.0K	/var/lib/apt/mirrors
205M	/var/lib/apt
2.9G	/var/lib/

Use the -s option to report only the entire size of the selected directory, not subdirectories:

sudo du -sh /var
Output

2.9G	/var

The -c flag instructs du to display a grand total. When you wish to determine the combined size of two or more directories, this is beneficial.

sudo du -csh /var/log /var/lib
Output

1.2G	/var/log
2.9G	/var/lib
4.1G	total

Use the --max-depth option and specify the subdirectories level to display the disc use of n-level subdirectories. To get a report on the first-level directories, for example, type:

sudo du -h --max-depth=1 /var/lib
Output

...
544K	/var/lib/usbutils
4.0K	/var/lib/acpi-support
205M	/var/lib/apt
2.9G	/var/lib

The du utility's default behavior is to resize the disc space used by the directory or file. Use the --apparent-size switch to determine a file's apparent size. The "apparent size" of a file refers to the amount of data contained within it.

sudo du -sh --apparent-size /var/lib
Output

2.9G	/var/lib

You may also use the shell pattern with du. For example, to get the size of all directories in your home directory that begin with the letter "Do," run:

sudo du -csh ~/Do*
Output

102M	/home/vegastack/Documents
358M	/home/vegastack/Downloads
460M	total

Using du with Other Commands

With pipes, the du command can be coupled with other commands.

To print the top 5 directories inside the /var directory, for example, pipe the output of du to the sort command, which will sort the directories by size, and then pipe the result to the head command, which will only print the top 5 directories:

sudo du -h /var/ | sort -rh | head -5
Output

4.6G	/var/
2.9G	/var/lib
2.6G	/var/lib/snapd
1.7G	/var/lib/snapd/snaps
1.2G	/var/log/journal/af8ce1d394b844fea8c19ea5c6a9bd09

FAQs on Du Command in Linux

How does the du command work?

du recursively traverses the specified directories, calculating and summarizing the disk usage of each file and directory encountered.

How do I use the du command to check disk usage?

To use du, simply provide the directory or file path as an argument. For example, du /path/to/directory will display the total disk usage of the specified directory.

Can I display disk usage in a more human-readable format?

Yes, you can use the -h or --human-readable option with du to display sizes in a more understandable format, using units like K, M, G, etc. For example, du -h /path/to/directory will display disk usage in human-readable format.

Can du calculate the size of a single file?

Yes, du can calculate the size of a single file by providing the file path as an argument. However, it will display the same output as ls -l, providing information about the file size and not diving into subdirectories.

Can I sort the output of du by size?

Yes, you can use the -s or --summarize option with du to display a summary of disk usage and sort it in decreasing order. For example, du -sh * | sort -rh will display disk usage in a sorted, human-readable format.

Can du exclude certain directories or files from its calculations?

Yes, you can use the --exclude option followed by a pattern to exclude specific directories or files from du calculations. For example, du --exclude=dir_to_exclude /path/to/directory will exclude the specified directory from the disk usage calculation.

Can du display disk usage recursively for subdirectories?

By default, du displays disk usage for each subdirectory encountered. However, you can use the -d or --max-depth option followed by a number to limit the depth of recursion. For example, du -d 2 /path/to/directory will display disk usage up to a depth of 2 subdirectories.

Conclusion

You should have a fair understanding of how to use the du command by now.

Unlike df, which outputs information about the mounted file systems' disc utilization, the du command estimates the amount of disc space consumed by specific files or directories.

By running man du on your terminal, you may see all the various du command options.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.