How to Find Total Size of Directory in Linux using du Command?

How to Find Total Size of Directory in Linux using du Command?

Last week, my Linux was crashed while updating broken installed packages. Because of this, I was not able to login Ubuntu through GUI.

While recovering my Linux system I come across this easiest way to find total size of directory in Linux.

When my Linux crashed, I had very crucial data in my hard disk. And I wanted to take its complete backup. The only option was to log in through the terminal console and copy the data to an external USB drive.

To copy the data, I need to know how much data do I have in my disk directory. So again as I wanted to take the full backup of my disk drive, the question sparked in my mind.

Is there any simple command to display the total aggregate size of all files in the hard disk directories?

Here comes I use ‘du’ command to determine data size in the directory. I thought writing here may help many.

How to Find Total Size of Directory in Linux?

You can use ‘du’ command. du stands for ‘disk usage’.

This command is to get the information about disk usage. It works recursively on all the files in the specified directory.

So here is a command.

du -hs /path/to/directory

Here,

-h: This parameter is to get the value in human-readable format.
Example: It will display 4.6G instead of 4789688 and it is convenient for the user.

-s: This parameter provides a summary of data usage for all files in specified directory.

If you are interested in getting disk usage for each file in the directory, skip using ‘s’ parameter.

In my case of crashed Linux, I find out the size of the data in the hard disk. Then I mounted USB flash drive through the terminal and taken all data backups. Running these commands come as the rescue operation for me.

Disk usage of the current directory:

To find total size of directory in Linux for the current directory, simply run following command in terminal

du -hs .

Here,

'.' indicates current directory path.

Note: These all commands are tested on Ubuntu Linux system, and it is supposed to work on any Linux system.

Wrapping Up!

Many programmers ask me about how to become good at Linux commands and my opinion about the preferred programming language. If you have the same question, I would suggest you, start learning shell scripting along with Python. Once you will get the good command over there; trust me, it will be so fun to use Linux and doing programming with Python.

‘du’ commands are extremely handy to find total size of directory in Linux through the terminal, especially when you don’t have access to GUI.

If you face any problem while running these commands, feel free to write in the comment.

Leave a Reply

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