How to get the Size of a Directory in Linux

In our everyday interaction with computers, knowing the size of files and folders in our systems is essential. With Linux systems, getting the size of files and directories in the GUI mode is straightforward. All you need to do is right-click on the specific directory and select the properties option. However, knowing how to get the size via the command-line (Terminal) can be equally important. If you are a system admin managing a server without a GUI, this method will come in handy.

This brief tutorial will give you a detailed guide on how to get the size of a Directory in Linux systems via the command-line.

Getting the Size of a Directory in Linux

To get the size of a directory via Terminal, we will use the du command. DU stands for Disk Usage. It lists the amount of space occupied by different files and directories.

The general syntax of the du command is as follows;

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

If you execute the du command on a specified directory, it will summarize every sub-director disk usage. If no path is specified, the du command will disk usage report of the current working directory.

Let’s run the du command on our home directory. Take a closer look at the output.

$ du

du command
du command

The du command has given a detailed disk usage report of the directories and sub-directories present from the image above.

To display the size of a specified directory, e.g., .cache (hidden folder), run the command below.

du .cache

du command
du command

To display the output in ‘human-readable format,‘ use the -h option as in the example below.

du -h .cache

du command
du command

You can now see the size of directories in kilobytes (K) and megabyte (M) from the image.

Alternatively, you can specify the unit size that you want the du command to use. That can be -k for kilobytes and -m for megabytes.

du -k .cache/

du -m .cache/

To list sub-directories’ size with those occupying the largest size on the top, we use the -hr option. See the command below.

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

From the above output, all sub-directories occupying the largest disk space are displayed at the top. You can increase the du report deep level by increasing the –max-depth parameter to view more sub-directories.

To get the size of a directory omitting the sub-directories, we will use the -sh option.

sudo du -sh /var

The output will look as below.

sudo du -sh var
sudo du -sh var

Let’s take a look at the arguments used in the above command.

sudo: Since the root user owns most files and directories, we will need to use the sudo command to get elevated privileges.

s: To display only the size of the main directory and omit sub-directories.

h: Print the unit size in a human-readable format.

/var: The path to directory/ folder you want to get the size./var:

Conclusion

I believe the example above has given you a clear guide of getting the size of a directory using the du command. If you are managing a Remote Desktop server via utilities like Putty, you are limited to using the command-line with no access to a GUI. The above will come in handy when trying to get the size of directories.

If you have any questions or remarks about this tutorial, feel free to leave a comment below.

Leave a comment

Your email address will not be published. Required fields are marked *