In this tutorial, we will look at how to check the size of a directory in Linux. This task is done using the du command which is a standard Unix/Linux program. The abbreviation du is short for disk usage.
Checking the size of a directory may be useful, for example, on something like a Linux web server where we may want to know what the size of our web root is for backup purposes. The simple fact is, being able to check the size of a directory in Linux is very useful in a number of ways.
In the most common case, we will want to check the total size of all the contents of a directory and we want the number we get to be in a human-readable format such as KB or MB, etc.
For example, if we wanted to check the size of /var/www/example.com/ we would use the following command:
du -sh /var/www/example.com/
The result we get would be something like:
In the example, we found out that our directory had contents totalling 78MB.
The -s flag used with our command tells the utility that we only want a summary of the size of the directory, i.e. we want the command to run non-recursively. If we didn't use that flag, the du command would output the sizes of all the directories located under the directory we specified in addition to the size of the directory we wanted to check.
The -h flag used tells the utility to output the result in a human-readable form. In our case, it means that we will get the directory size in MBs and not in the default KB which would give a large number.
If we want to see the size of all the directories located under the directory we specify, we simply need to leave off the summary option like this:
du -h /var/www/example.com/
A possible result of that command could be:
28M /var/www/example.com/logs 43M /var/www/example.com/public_html/images 50M /var/www/example.com/public_html 78M /var/www/example.com/
Depending on the amount of directories located inside of the target directory, we may get a large amount of output. Try it for yourself and see.
That is all there is to checking the size of a directory in Linux. We hope you found this tutorial useful.