Open In App

How to Get Total Size of a Directory in Linux

Last Updated : 29 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Many times during working on Linux we may encounter situations, where we want to look at the total size occupied by a directory. Knowing the file or directory size can help a user perform tasks like storage allocation, size comparison, etc. At that time knowledge of some commands might be helpful to get the size of a directory in Linux. Usually, when we list the contents in a directory we see that the size is in kilobytes. That is not the actual size occupied but it is the metadata about the directory.

Checking Size

Checking the Size of a directory

Let us see some ways to get the total size of a directory in Linux.

 1. Listing the size of the present directory in Linux

 Du in Linux is short for disk usage, this command by default lists the sizes of the main directory and is also capable of listing the sizes of the sub-directories. The below command will show the size of the present directory.

Note: The sizes listed are in kilobytes.

Syntax of du:

du 

Let us find out the sizes of the directories present in the home folder.

Currently, the directory we are in is home.

Checking the size of the current directory

2. Listing the size of a specific subdirectory in Linux

If we want to see the size of the subdirectory inside the home folder use the below command:

du mojha/ 

Note: mojha is the directory present inside the home folder.

Checking size of the subdirectory

Checking the size of a specific subdirectory

3. Listing size of multiple directories at the same time in Linux

To save us some time we can also list the sizes of multiple directories together. In the below command, we will find the sizes of two directories (/Desktop and /Music).

du Desktop/ Music/
Checking size of multiple directories

Checking the size of multiple directories

4. Listing the size of a Directory in human-readable format in Linux

It is also possible to get the output in a proper format, which is easy to read and understand. Use the below command:

du -h 

OR

du -h <directory_name> (For specific directory)
Easy output display

Size of a directory in Human-Readable format

Note: that the sizes now have a suffix that denotes whether they are in Kb, Mb, or Gb. This makes it a lot easier to read and understand as compared to the above methods.

If we want to get the size of a directory in a specific format, say in megabytes then we can use the below command:

du -m <directory_name>/ (m for megabyte)
du -k <directory_name>/ (k for kilobyte)
The sizes are in Megabytes.

size in megabytes of directories

5. Listing the total size of the directory in Linux

By using the -s and -h flags with the du command we can get the total size of a directory, use the below command:

du -sh
The total size of the directory is 201 MB.

The size of a directory

The total sizes of multiple subdirectories can be obtained as weusing use the following command:

du -sh dir1/  dir2/  dir3/
Total sizes of multiple subdirectories

The total size of multiple directory

The grep command can also be used to obtain the total size of a directory.

du -ch Documents/  |  grep total
Using grep command

Grep command to get the size of a directory

6. Listing size of directories up to a specific length in Linux

If the file tree of your system is a very large one then you can specify the maximum depth up to which the search should take place. For this we use the –max-depth flag, we can specify the length. e.

i.e 1,2,3 or 4 as per our needs.

du -h --max-depth=3 ~/Downloads
Maximum depth

Maximum depth

In the above command, the file sizes are from 3 levels deep within the file tree.

7. Using the tree command

The tree command visually shows the sizes of the Directory and flies. If the tree command is not installed you can download it using the below command:

sudo yum install tree

The subdirectories are shown as lines that look like the branches of trees, hence the name tree command. 

tree 
Using tree command

Using Tree Command

To get the size of a directory in a human-readable format use the -h flag.

tree -d -h (-d flag is used to indicate only directories)

OR

tree --du -h (--du flag is used to print the accumulation of sizes)
Get human-readable output

get the Size of the Directory using the tree

For obtaining more information about the tree command you can check out the man page the of tree.

man tree
Help page og tree command

Help page tree command

8. Listing the files according to their sizes

Sometimes we may want to check out which file is taking up the most space on the system, then we can sort the files according to their sizes and see the result. Use the below command for that:

du -h --max-depth=1 |  sort -hr
The files are sorted descendingly.

The files are sorted discerningly.

So these were the few commands that can help you get the size of your directory as well as allow you to get the result in a particular format. There are a lot of ways to achieve the same things, you can use some other commands as well. Check out the man du page for more information about the du command.

man page for du command

man page for du command

Conclusion

In this article we discussed that how can we fide the size of the directory in Linux . We discussed different methods to get the size of directories ,like how to get size of current directory , to get size of a specific sub directory , to get size of multiple directories and so on .One can easy understand all the conseptual and practical of how to get size of directory in Linx just by going thorough this article, it is also important of a Linux user to be well versed with these concept of get size of a directory.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads