[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedApril 10, 2023

How to use du command.

How To Use Du Command.

The du command, short for “disk usage” reports the estimated amount of disk space used by given files or directories. It is practically useful for finding files and directories taking up large amounts of disk space.

 

How to Use the du command

The general syntax for the du command is as follows:

du [OPTIONS]... FILE...

If the given FILE is a directory, du will summarize disk usage of each file and subdirectory in that directory. If no FILE is specified, du will report the disk usage of the current working directory .

When executed without any option du displays the disk usage of the given file or directory and each of its subdirectories in bytes.

du ~/Documents 

You can also pass multiple files and directories to the du command as arguments:

du ~/Documents ~/Pictures ~/.zshrc

If you run du on a file or directory for which you don’t have permissions, you will get something like “du: cannot read directory”. In this situation, you’ll need to prepend the command with sudo .

du has lots of options, we’ll outline just the most frequently used ones.

The -a option tells du to report the disk space usage of each file within the directory.

du -a ~/Documents 

Usually, you would want to display only the space occupied by the given directory in a human-readable format. To do that, use the -h option.

For example, to get the total size of the /var/lib and all of its subdirectories, you would run the following command:

sudo du -h /var

We are using sudo because most of the files and directories inside the /var/lib directory are owned by the root user and are not readable by the regular users. The output will look something like this:

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

To report only the total size of the specified directory, and not for subdirectories use the -s option:

sudo du -sh /var
2.9G	/var

The -c option tells du to report a grand total. This is useful when you want to get the combined size of two or more directories.

sudo du -csh /var/log /var/lib
1.2G	/var/log
2.9G	/var/lib
4.1G	total

If you want to display the disk usage of the n-level subdirectories use the –max-depth option and specify the subdirectories level. For example, to get a report about the first-level directories you would use:

sudo du -h --max-depth=1 /var/lib
...
544K	/var/lib/usbutils
4.0K	/var/lib/acpi-support
205M	/var/lib/apt
2.9G	/var/lib

The default behavior of the du utility is to re the disk space used by the directory or file. To find the apparent size of a file, use the –apparent-size switch. The “apparent size” of a file is how much data is actually in the file.

sudo du -sh --apparent-size /var/lib
2.9G	/var/lib

du also allows you to use shell pattern. For example, to get the size of all directories starting with “Do” in your home directory you would run:

sudo du -csh ~/Do*
102M	/home/linuxize/Documents
358M	/home/linuxize/Downloads
460M	total

 

Using du with Other Commands

The du command can be combined with other commands with pipes.

For example, to print the 5 largest directories inside the /var directory you would pass the output of du to the sort command to sort the directories by their size and then pipe the output to the head command which will print only the top 5 directories:

sudo du -h /var/ | sort -rh | head -5
4.6G	/var/
2.9G	/var/lib
2.6G	/var/lib/snapd
1.7G	/var/lib/snapd/snaps
1.2G	/var/log/journal/af8ce1d394b844fea8c19ea5c6a9bd09

 

Conclusion

By now you should have a good understanding of how to use the du command.

unlike df which prints information about the disk usage of the mounted file systems , the du command gives you estimate of disk space used by given files or directories.

Keep reading
How to Remove (Delete) Symbolic Links.
March 16, 2023

How to Remove (Delete) Symbolic Links.

How To Remove (Delete) Symbolic Links. In this guide, we will show you how to remove (delete) symbolic links in Linux/UNIX systems using the rm, unlink, and find commands. A symbolic link, also known as a symlink, is a special type of file that points to another file or directory. It is something…

Read article
How to understand symbolic link and their use.
March 16, 2023

How to understand symbolic link and their use.

How To Understand Symbolic Link And Their Use. A symlink or a Symbolic Link is simply enough a shortcut to another file. It is a file that points to another file. They may sound a little complex, but they’re really not, you can think of symlinks as shortcuts. To create a symbolic link you use…

Read article
How to Show Line Numbers in Vim / Vi.
March 16, 2023

How to Show Line Numbers in Vim / Vi.

How To Show Line Numbers In Vim / Vi. This article will show you how to display or hide line numbers in Vim / Vi text editor. Vim/Vi is the text editor of choice for many software developers and Linux system administrators. By default, Vim doesn’t show line numbers, but they can be easily turned…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00