Linux – View size of a directory and its contents

If you are familiar with the Linux command line you may have used the ‘df’ command to get an overview of the size of disks attached to your system along with used space and available space. Although very useful information, at times you need to know the size of a particular directory and its contents. In this post I will be showing you how to list the size of a directory and its contents on your system.

 

Your first thought might be to use the ‘ls’ command to get this information which we can use to get size information on any single file but ‘ls’ will not add up all of the sub files and directories and give us a total like we want. Instead the command we will use to give us this information with be ‘du’, if we were to use this command by itself on a directory like in the example below.

Depending on the contents of the directory we are checking we could get a ton of output as it will give us not only an accumulative total size of all the files and folders but it will also recursively list out every single file and folder along with size and often that is too much information.

 

To only get the accumulative total we can pass in the -s (summarize) option which will give us the output below

This will give us the total size in Kilobytes which is fine but when it returns a value that is a few gigabytes that can be hard to read so we can also make it more human readable by passing in the -h (human readable) flag. Below is an example of using du with the -s and -h flags

We can now see it gives the output in a more human readable format.

 

You can use this command on any directory that is directly attached to your machine or any network storage. If there are any questions drop a line in the comments below and I will get back to you!

Be the first to comment

Leave a Reply

Your email address will not be published.


*