Sep 20, 2023 4 min read

How to Get the Size of a Directory on Linux

Get the Size of a Directory on Linux with our step-by-step tutorial. Directories in ls are usually 4096 bytes in size for storing metadata.

Get the Size of a Directory on Linux
Table of Contents

Introduction

When using the lscommand to list the contents of a directory, you may have noted that the directories are usually always 4096 bytes in size (4 KB). That is the amount of disc space utilized to store the directory's meta-information, not the amount of data it contains.

du, which stands for "disc use," is the command to use to determine the true size of a directory.

In this tutorial, you will get the size of a directory on Linux. We will also address a few FAQs on how to get the size of a directory on Linux.

Getting the Size of a Directory

The du command shows how much file space the supplied files or directories are taking up. du summarizes the disc usage of each subdirectory in the supplied path if it is a directory. du gives the disc use of the current working directory if no path is given.

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

In most cases, you'll want to present the directory's space in a human-readable way. For example, to determine the entire size of the /var directory, use the command:

sudo du -sh /var

You will get an output like below:

Output

85G	/var

Let's go over the command and its arguments in more detail:

  • Because most of the files and folders in the /var directory are owned by the root user and inaccessible to regular users, the command begins with sudo. The du command will print "du: cannot read directory" if you don't use sudo.
  • s - Only show the entire size of the chosen directory; file size totals for subdirectories are not shown.
  • h - Print sizes in a legible format for humans (h).
  • /var - The path to the directory whose size you want to know.

What if you want to see the first-level subdirectories' disc usage? There are two options available for you. The first is to use the asterisk symbol (*), which means "match everything that does not begin with a period (.)" as shown below. du will print a grand total of all sizes if you provide the -c option:

sudo du -shc /var/*
Output

24K	/var/db
4.0K	/var/empty
4.0K	/var/games
77G	/var/lib
4.0K	/var/local
0	/var/lock
3.3G	/var/log
0	/var/mail
4.0K	/var/opt
0	/var/run
196K	/var/spool
28K	/var/tmp
85G	total

The --max-depth option can also be used to generate a report on the disc use of first-level subdirectories:

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

77G	  /var/lib
24K	  /var/db
4.0K	/var/empty
4.0K	/var/local
4.0K	/var/opt
196K	/var/spool
4.0K	/var/games
3.3G	/var/log
5.0G	/var/cache
28K	/var/tmp
85G	/var
85G	total

The du command, by default, displays the amount of disc space consumed by the directory or file. Use the --apparent-size option to determine the apparent size of a directory. The "apparent size" of a file refers to the amount of data contained within it.

sudo du -sh --apparent-size /var

The quantity of data transported over the network while transferring a directory through SCP, Rsync, or SFTP is the apparent size of the files. This explains why the size of the disc space consumed on the source when displayed with du (without --apparent-size) differs from the size on the target.

The du command can also be piped together with other commands.

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

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

85G	/var/
77G	/var/lib
75G	/var/lib/libvirt/images
75G	/var/lib/libvirt
5.0G	/var/cache/pacman/pkg

FAQs to Get the Size of a Directory on Linux

Can I get the size of nested directories within a directory? 

Yes, the du -sh command recursively calculates the size of all directories and files within the specified directory.

How can I sort the output to display the largest directories first? 

You can use the du -sh directory_path | sort -h command to sort the directories based on their size in a human-readable format.

What if I want to display the size in different units, like in kilobytes? 

You can use the -k option with du command. For example, du -sk directory_path will display the size in kilobytes.

Is it possible to get the sizes of individual files within a directory? 

Yes, using the -a option with du command will display the sizes of both directories and individual files.

Is there a way to exclude certain subdirectories or files from the size calculation? 

Yes, you can use the --exclude option followed by the names or patterns of directories or files you want to exclude.

How can I get the total size of all directories in the current directory? 

You can use the du -sh * command to get the size of all directories in the current directory and display the total size at the end.

Can du display the sizes in a machine-readable, tabular format?

Yes, you can use the --bytes or -b option with du, which will display the sizes in bytes without any unit.

Conclusion

The du command on Linux can be used to determine the size of a directory.

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.