Home FAQ How To Find The Size Of A Directory In Linux
How To Find The Size Of A Directory In Linux

How To Find The Size Of A Directory In Linux

By sk
15277 Views

This brief tutorial explains how to find the size of a directory in Linux operating systems. Finding size of files and directories in GUI mode is easy! All we have to do is just right click on the file or directory, and choose the properties option from the context menu. However, it is equally important to know how to find the size of a directory from CLI mode as well. Let me explain how to find directory size from command line.

Find the size of a directory in Linux

To find out the size of a directory, we will use 'du' command. du stands for disk usage.

The typical syntax of du command is given below:

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

du [OPTION]... --files0-from=F

Let us type the 'du' command in the Terminal and see what it displays.

$ du

sample output:

Find the size of current directory in Linux

As you see above, du command displays the disk usage of the directories along with its sub-directories in the current directory.

To display a particular directory's size, for example ostechnix, run:

$ du ostechnix/

Sample output:

36252 ostechnix/Swami Vivekananda (Chicago Speech)
452 ostechnix/MultiCD/plugins
44 ostechnix/MultiCD/.git/hooks
4 ostechnix/MultiCD/.git/branches
1012 ostechnix/MultiCD/.git/objects/pack
4 ostechnix/MultiCD/.git/objects/info
1020 ostechnix/MultiCD/.git/objects
8 ostechnix/MultiCD/.git/logs/refs/heads
8 ostechnix/MultiCD/.git/logs/refs/remotes/origin
12 ostechnix/MultiCD/.git/logs/refs/remotes
24 ostechnix/MultiCD/.git/logs/refs
32 ostechnix/MultiCD/.git/logs
8 ostechnix/MultiCD/.git/refs/heads
4 ostechnix/MultiCD/.git/refs/tags
8 ostechnix/MultiCD/.git/refs/remotes/origin
12 ostechnix/MultiCD/.git/refs/remotes
28 ostechnix/MultiCD/.git/refs
8 ostechnix/MultiCD/.git/info
1168 ostechnix/MultiCD/.git
140 ostechnix/MultiCD/maps
2706504 ostechnix/MultiCD
2832056 ostechnix/

We can also display the size in "human readable format" (i.e. auto-selecting the appropriate unit for each size), rather than the standard block size.

To do so, add -h tag with du command as shown below.

$ du -h ostechnix/

Sample output:

36M ostechnix/Swami Vivekananda (Chicago Speech)
452K ostechnix/MultiCD/plugins
44K ostechnix/MultiCD/.git/hooks
4.0K ostechnix/MultiCD/.git/branches
1012K ostechnix/MultiCD/.git/objects/pack
4.0K ostechnix/MultiCD/.git/objects/info
1020K ostechnix/MultiCD/.git/objects
8.0K ostechnix/MultiCD/.git/logs/refs/heads
8.0K ostechnix/MultiCD/.git/logs/refs/remotes/origin
12K ostechnix/MultiCD/.git/logs/refs/remotes
24K ostechnix/MultiCD/.git/logs/refs
32K ostechnix/MultiCD/.git/logs
8.0K ostechnix/MultiCD/.git/refs/heads
4.0K ostechnix/MultiCD/.git/refs/tags
8.0K ostechnix/MultiCD/.git/refs/remotes/origin
12K ostechnix/MultiCD/.git/refs/remotes
28K ostechnix/MultiCD/.git/refs
8.0K ostechnix/MultiCD/.git/info
1.2M ostechnix/MultiCD/.git
140K ostechnix/MultiCD/maps
2.6G ostechnix/MultiCD
2.8G ostechnix/

Now you see the size of the directories in Kilobytes, Megabytes and Gigabytes, which is very clear and easy to understand.

We can also display the disk usage size only in KB, or MB, or GB.

To do so, use -k for kilobytes, -m for megabytes

$ du -k ostechnix/
$ du -m ostechnix/

To find out which sub-directories consume how much disk size, use this command:

$ du -h --max-depth=1 | sort -hr

The largest sub-directories will be displayed on the top. You can increase the directory depth level by increasing the value of --max-depth parameter.

As you may noticed in the all above outputs, du command only displayed the disk usage of directories. But, what about the files? To display the disk usage of all items including files and directories, use -a flag.

$ du -ah ostechnix/

Now, you will see the disk usage of all files and folders in human readable form.

Sample output:

Display disk usage of files and folders in human readable format in Linux

We can also display the size of multiple directories at once as shown below.

$ du -h directory1 directory2

If you want to check the total disk space used by a particular directory, use the -s flag.

$ du -sh ostechnix

Here, -s flag indicates summary.

Sample output:

2.8G ostechnix

Similarly, to display the total disk space used by multiple directories, for example ostechnix and /etc, run:

$ du -sh ostechnix /home/sk/

Sample output:

2.8G ostechnix
279G /home/sk/

To display the grand total of directories, add -c flag with du -sh command.

$ du -csh ostechnix /home/sk/

Sample output:

2.8G ostechnix
279G /home/sk/
281G total

To display only the grand total of the given directory including all  the sub-directories, use 'grep' command with 'du' command like below.

$ du -ch Downloads/ | grep total
12G total

You might want to exclude certain type of files. The following command will display the size of the current directory including its sub-directories, but it will exclude the size of all .mp4 files.

$ du -ch --exclude='*.mp4' | grep total 
6.4G total

Can we find the biggest or smallest directories/files? Of course, yes! Check the following guide.

For more details about 'du' command, check the man pages.

$ man du

Suggested Read:


Hope this helps.

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

13 comments

Chinonso Chukwuogor February 1, 2018 - 3:54 pm

Thanks a lot. This was very helpful

Reply
AMit April 17, 2018 - 4:18 pm

Thanks for du command. It really help me to find the folder size easily.

Reply
Jenna June 22, 2018 - 10:59 pm

Thank you. You were very helpful to put this together.

Reply
Sourabh Srivastava September 6, 2018 - 3:12 am

Thanks for this tutorial. Very helpful

Reply
James September 7, 2018 - 11:39 pm

Thank you! Very Helpful

Reply
himanshu October 12, 2018 - 12:29 am

helpful

Reply
Alex November 7, 2018 - 3:58 am

thanks for your help, it worked. I don’t use this command often but i forget sometimes the abbreviation behind it, disk usage.

Reply
Mallika Bachan May 22, 2019 - 11:38 pm

>the size of the directories is displayed in bits.

?? No, it’s in K
KiloBytes, I think, not Kilobits – but you’re definitely not clarifying any of that here

Reply
sk December 10, 2019 - 3:37 pm

Nice catch. Clarified now. Thanks for pointing it out.

Reply
stang June 15, 2019 - 2:58 am

The du utility displays the file system block usage for each file argument and for each directory in the file hierarchy rooted in each directory argument. The block size depends on the file system you are using (typically 4k in most modern FS) .

Reply
Oliv August 7, 2019 - 8:26 pm

Thanks for this tutorial.!!

Reply
Andrea Florio January 3, 2020 - 2:47 pm

for people looking at the short answer..

du -h -d 1

Reply
sk January 3, 2020 - 6:41 pm

Brilliant. Thanks.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More