How to Find the Total Size of a Directory in Linux

At some point, you will be intrigued to know the total size of a current directory, or of any XYZ directory taking space on your disk, and it will be very crucial when you are running out of space, and want to delete a file that is consuming more space.

You may be trying to find the total size of a directory using the ls -shw command, but it won’t work here. So how do I determine the size of a directory in Linux using the command line?

What is du command

du command, aka Disk Usage, is the right tool to find out the size of a directory, which will help you find out the size of any directory and its subdirectories.

By default, it lists out all the directory names along with the size of an individual directory in KB size format, which you can change easily with the available options, which I’ll cover later in this article.

Let’s look at how you can use the du command in Linux to find directory space with several options.

Use the du command to find the size of the current directory

When you want to find the size of the current directory where your terminal is currently located, just run the below command, which will list out all the directories that are the parents of the current directory. 

$ du

For example, I would like to list out the size of the directory which is located in /var

$ cd /var
$ sudo du

From the below output, you can understand the behaviour of the above command. On the left side of the output, it displays the size of a directory, and next to it shows the directory name. As you can see, it lists out all the directories that are held in a specific location. 

It lists out the directory size, which includes the subdirectories by ignoring regular files. 

At the bottom, it shows you the total size of the current directory.

Current directory size find using du command
Current directory size find using du command

Use the du command to find the size of the specific directory

If you want something that should show the size of a particular directory, then you should provide the directory name as an argument with the du command, like in the below code snippet:

$ du [directory-path]

As you see in the below image, I just want to find the size of the “backup” directory.

Use the du command to find the size of the specific directory

Use the du command to find the size of multiple directories

You can line up multiple directories with the du command to find the size of all individual directories along with subdirectories of the specified path.

For that, you have to invoke the du command and specify the multiple directories:

$ du [directory-path-1] [directory-path-2] [directory-path-3]

The output of the above command.

Use the du command to find the size of multiple directories
Use the du command to find the size of multiple directories

Use du to find the total size of a current directory

By using the default command, it lists out all directories and subdirectories, which fills the entire screen with text, which was not convenient for me at all.

I just want to know the size of the current working directory. Then you should use -s or --summarize.

$ du -s
or
$ du --summarize

The output of the above command summarizes the size of the directory and subdirectory and prints out the total size of the current directory.

Use du to find the total size of a current directory
Use du to find the total size of a current directory

Find the total size of a particular directory

Above, you have learned to find the total size of the current working directory, but what if you want to find the total size of a particular directory? Simply invoke the below code.

$ du -s [directory-path]

Find the total size of multiple directories

When you want to find the total size of multiple directories, then you can use the below code to specify multiple directory paths.

$ du -s [directory-path-1] [directory-path-2] [directory-path-3]
Find the total size of multiple directories using the du command
Find the total size of multiple directories using the du command

Add the grand total at the bottom using du command

From the above section, you have found out the way to get the total size of particular or multiple directories, and you can even make the output more sensible by adding the total at the end of the line.

Let me show you how you can use the -s command with -c to get the total size of all the specified directories at once.

$ du -sc [directory path]
or 
$ du -sc [directory-path-1] [directory-path-2] [directory-path-3]
Add total size
Add total size

You can even use the grep command to just find the total value.

$ du -sc [directory-path-1] [directory-path-2] [directory-path-3] | grep total
Use the grep command with du to find total disk usage
Use the grep command with du to find total disk usage.

Find the size of directory in human-readable format in du command

When you invoke the above command, it displays the output in KB format, which is hard to read. When the size of the file is big enough to change the default output to human-readable format, you can use -h, --human-readable.

$ du -h
OR
$ du --human-redable
Find directory size in human-readable format by using du command
Find directory size in human-readable format by using du command

You can find the total size of the disk in human-readable format by using the below syntax.

$ du -sch [directory-path-1] [directory-path-2] [directory-path-3] | grep total

Find the size of directory in multiple format in du command

There are also multiple formats available like -b or –bytes.

$ du -b
or
$ du --bytes

If you want to print out data in megabytes, then use -m.

$ du -m
Find the directory size in multiple formats
Find the directory size in multiple formats.

Use the du command to find the size of a directory and file

Although the du command is used to find the size of a directory, you can also use it to print the file size along with the directory and subdirectory.

If you want to count file and directory sizes, then invoke the below code on your terminal screen:

$ du -ah

Options like -a read everything, and -h you know very well why we have used it.

As you can see, I have used sudo because some of the content you can’t read without using sudo. If you do, then you will get an error on the screen like:Permission denied.

Find the size of directory and file using du command
Use the du command to find the size of a directory and file

Find the actual size of directory using du command

When you execute any of the above commands, we get the disk space used by the directory or file. To get the actual size of the directory, you can use the --apparent-size options.

You may find the --apparent-size is comparatively less than any other option because it shows you how much actual data is loaded into the file.

$ sudo du --apparent-size

The output of the above command

Find actual size of file holding using du command
Find actual size of file holding using du command

Find the largest directory using du command

Every single byte of disk space is crucial when you run out of space. At that point, the du command with some tweaks can be very helpful to list out directories that are consuming more space in a particular location.

Let’s say you want to find the top ten directories in /var that hold the largest size of a file, then you can execute the below code on your terminal screen.

$ sudo du -h /var/ | sort -rh | head -10

The output of the above command

Find largest size of directory using du command
Find largest size of directory using du command

Cheatsheet

CommandUsage
du Show disk usage size of the current directory
du /path/of/directory/Show disk usage size of a particular directory
du /dir1 /dir2 /dir3Show disk usage size of multiple directories
du -s /path/of/directory/Show the total size of a particular directory
du -sc /path/of/directory/Show grand total
du -hShow disk usage size in human-readable format
du -sch /dir1 /dir2Show total using grep command in a human-readable format
du -ahShow directory and file size
du --apparent-sizeShow the actual size of the directory
du -h --max-depth=NShow disk usage size of Nth level
sudo du -h /var/ | sort -rh | head -10Show the largest directory

Wrap up

We tried to cover all the major aspects of du command. If I have missed something, do let us know. You can also share your experience of how much you liked the article.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments