Atlantic.Net Blog

How to Find the Total Size of a Directory in Linux

Finding the size of the directory in Linux via the command line is very difficult for a beginner. du, also known as Disk Usage, is a Linux command-line utility that allows users to check the information on disk usage of files and directories on a system. The du command has many options that allow you to get the results in many formats.

In this post, we will show you how to find the size of a directory in Linux.

Basic Syntax of the du Command

The basic syntax of the du command is shown below:

du [OPTION]... [FILE]...

A brief explanation of each option is shown below:

  • -c – Produce a grand total
  • -h – Print sizes in a human-readable format
  • -S – For directories do not include the size of subdirectories
  • -a – Display the disk usage of all items including files and directories
  • -k – Display the disk usage size in kilobytes
  • -m – Display the disk usage size in megabytes
  • -X – Exclude certain file type

Also Read

How to Check Size of Files and Directory on Linux

Find Current Directory Size

Running the du command without any options will display the size of the current directory including all of its sub-directories.

du

You will get the following output:

8	./html/example.com
20	./html
3424	./log/installer
84	./log/apache2
236	./log/apt
du: cannot read directory './log/gdm3': Permission denied
4	./log/gdm3
483588	./log/journal/cfefe3a7c8694e51879fb521a2021b2e
483592	./log/journal
64	./log/cups
500192	./log
509272	.

Find the Size of the Specific Directory

To find the size of the specific directory, run the du command by specifying a directory:

du /var/www/html

This command will display the size of the html directory, including all of its sub-directories:

8	/var/www/html/example.com
20	/var/www/html

If you want to find the size of the multiple directories, run the following command:

du /home/vyom/Music/ /home/vyom/Downloads

You will get the following output:

4996	/home/vyom/Music/
8	/home/vyom/Downloads/html/example.com
20	/home/vyom/Downloads/html
3424	/home/vyom/Downloads/log/installer
84	/home/vyom/Downloads/log/apache2
236	/home/vyom/Downloads/log/apt
4	/home/vyom/Downloads/log/gdm3
483588	/home/vyom/Downloads/log/journal/cfefe3a7c8694e51879fb521a2021b2e
483592	/home/vyom/Downloads/log/journal
64	/home/vyom/Downloads/log/cups
500192	/home/vyom/Downloads/log
509272	/home/vyom/Downloads

Find the Directory Size in Human Readable Format

By default, the du command will display size in bytes. You can use the -h option with the du command to display the size of the directory in a human-readable format.

du -h Downloads/

You will get the following output:

8.0K	Downloads/html/example.com
20K	Downloads/html
3.4M	Downloads/log/installer
84K	Downloads/log/apache2
236K	Downloads/log/apt
4.0K	Downloads/log/gdm3
473M	Downloads/log/journal/cfefe3a7c8694e51879fb521a2021b2e
473M	Downloads/log/journal
64K	Downloads/log/cups
489M	Downloads/log
498M	Downloads/

Find the Directory Size in a Specific Format

You can also display the directory size in KB, MB, or GB. For example, to display the directory size in megabytes, use the -m option:

du -m Downloads/

You will get the following output:

1	Downloads/html/example.com
1	Downloads/html
4	Downloads/log/installer
1	Downloads/log/apache2
1	Downloads/log/apt
1	Downloads/log/gdm3
473	Downloads/log/journal/cfefe3a7c8694e51879fb521a2021b2e
473	Downloads/log/journal
1	Downloads/log/cups
489	Downloads/log
498	Downloads/

To display the directory size in kilobytes, use the -k option:

du -k Downloads/

You will get the following output:

8	Downloads/html/example.com
20	Downloads/html
3424	Downloads/log/installer
84	Downloads/log/apache2
236	Downloads/log/apt
4	Downloads/log/gdm3
483588	Downloads/log/journal/cfefe3a7c8694e51879fb521a2021b2e
483592	Downloads/log/journal
64	Downloads/log/cups
500192	Downloads/log
509272	Downloads/

Find the Total Size Of The Directory

You can use the du command with the -hs option to display the total size of the specified directory in a human-readable format.

du -sh /etc/

You will get the total size of the /etc directory in the following output:

16M	/etc/

If you want to display the grand total of the combined directories in human-readable format, use the -c option:

du -csh Downloads/ Music/ Pictures/

You will get the following output:

498M	Downloads/
4.9M	Music/
2.6M	Pictures/
505M	total

Find the Size of Both Files and Directories

You can use the du command with the -a option to find the size of both files and directories:

du -ah Downloads/

You will get the following output:

8.0K	Downloads/LocalSettings.php
4.0K	Downloads/html/index.html
4.0K	Downloads/html/index.nginx-debian.html
4.0K	Downloads/html/example.com/index.html
8.0K	Downloads/html/example.com
20K	Downloads/html
8.9M	Downloads/apache-zookeeper-3.5.6-bin.tar.gz
4.0K	Downloads/image.png
8.9M	Downloads/

Also Read

How to Use Rsync to Copy/Sync Files Between Servers

Sort Directories By Their Size

If your system is running out of disk space and you want to find out which directories consume the most disk size, use the following command to sort all directories by their size:

du -h --max-depth=1 /var/log | sort -hr

You will get the following output:

1.9G	/var/log/journal
1.9G	/var/log
3.4M	/var/log/installer
476K	/var/log/teamviewer15
240K	/var/log/apt
100K	/var/log/unattended-upgrades
84K	/var/log/apache2
68K	/var/log/nginx
64K	/var/log/postgresql
64K	/var/log/cups
40K	/var/log/mysql
8.0K	/var/log/hp
4.0K	/var/log/sysstat
4.0K	/var/log/speech-dispatcher
4.0K	/var/log/gdm3
4.0K	/var/log/dist-upgrade

Conclusion

In this post, we explained how to find the size of a directory with different examples. You can now use the du command with different options to achieve your desired result. Try it on dedicated hosting from Atlantic.Net!

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year