How to Get the Size of a Directory in Linux?

If you are a windows user and new to Linux, don’t worry about the directories, as they are similar to the folders in Windows. Different directories can be made in Linux using the mkdir command utility to store the files. To store the files, we should know the directory size, which we can find out by running the different commands.

In this post, different methods of finding the directory size have been discovered for Linux-based operating systems. This guide will elaborate on how to get the size of a director using three different methods mentioned below:

Now let’s discuss these methods in detail in the next section and see how you can utilize them.

Method 1: Using the du Command

The directory size can be discovered by utilizing the “du” command. The general syntax of the “du” command is as follows:

$ sudo du <options> <directory>
  • du: The du represents the disk usage
  • directory: The directory will be replaced with the path of the specified directory.
  • You must have sudo privileges to access the system directories.

Find the Size of the Current Directory

The recursive behavior of the du command gives us the size of its directories and sub-directories using the du command. To find the disk usage of the current directory, use the following command:

$ du

The output shows the size and complete path of the present working directory & its sub-directories.

Find the Total Disk Usage of the Main Directory

To check the total disk usage of the main directory only, not its sub-directories, use the “s” flag of the du command. To find the size of the home (~) directory, use the below-written command:

$ sudo du -s

Find Disk Size in Human Readable Form

If you want to show the disk size usage of the directories in a human-readable mode, use the “h” option of the du command as shown below:

$ sudo du -h

The output shows the respective size of the directories in “KiloBytes” and “MegaBytes”.  

Find the Size of the Specific Directory

We can get the size of the specified directory by putting its path with the du command. To find the size of all the directories of the “root” folder, execute the below command:

$ sudo du -h /root

The same could be done using another command using the “c” option in the du command, as discussed in the next section.

Find the Total Usage of the Specific  Directory

Similarly, the “c” flag of the du command is used to get the total usage of a specific directory. To get the complete total for the root folder, utilize the following command:

$ sudo du -c /root

Find the Total Size Consumed by the Home Directory

If you want to get a complete total size for the home directories (in my case: itslinuxfoss and peter) in a more readable format, utilize the following command: 

$ sudo du -shc /home/*

Find the Size of Main Directory and its Specific Level Sub Directories

We can find the size of the main directory and its specific levels of the sub-directories. To get the size of the main directory & first level sub-directory of the “root” folder, use the below command:

$ sudo du -h --max-depth=1 /root

Find the Highest and Lowest Spaced Disk Using du Command

The sort option of the du command allows you to get the directories of the highest or lowest disk sizes. To get the top three directories taking the highest disk size, the “root” folder can be found by executing the below command:

$ sudo du -h /root/ | sort -rh | head -3

To get the three minimum disk taking directories of the “root” directory, use the below command utility:

$ sudo du -h /root/ | sort -rh | tail -3

Sort a Directory and its Sub Directories According to Size

To get all the files sorted according to sizes of the directories, we can use the “n” numeric and “r” recursive flag of the du command. To get a list of sorted disk usage directories of the root folder, execute the below command:

$ du ~/root | sort -n -r | less

Find the Directory Size Excluding a Specified Sub-Directory

If you want to find the size of a specific directory and don’t want to include a specific sub-directory, the “exclude” option of the du command is used. For example: to exclude the /usr/local/group directory for finding the size of the /usr directory, use the below command:

$ sudo du -sh —-exclude /usr/local/group

Find the Directory Size Excluding the Files of Different System

To exclude the files of the different system in the directory, and find the directory size the “x” option is used. For instance: For finding the directory size of “/root” without including the other system’s files, the below command will be used:

$ sudo du -shx /root

Method 2: Using the tree Command

We can get the visual representation of the directory size in Linux using the tree command. In some distros, the tree package is included on installation, but if you don’t have it, use the below commands to install it:

For Ubuntu:

$ sudo apt install tree

For Centos:

$ sudo yum install tree

For Fedora:

$ sudo dnf install tree

The syntax for the tree command is as follows:

$ tree <options> <directory>
  • tree – It shows the “tree” command is executed.
  • options – Put any du command available option here.
  • directory – Path of the desired directory.

Find the Size of the Current Working Directory

The tree command uses the lines and colors to show the structure of the directories. To get the size of the current working (s flag) directory with its size visually, use the below-mentioned command:

$ tree -sh

Find the Size of a Specified Directory

The size of a specific directory can find out using the tree command with the directory path. To get the size of the root folder, utilize the tree command as follows:

$ tree -h /home

Method 3: Using the ncdu Command

The ncdu (NCurses Disk Usage) package is used to get the size of the directories. It is not installed in all the distros by default; utilize the below-mentioned commands:

For Ubuntu:

$ sudo apt install ncdu

For Centos:

$ sudo yum install ncdu

For Fedora:

$ sudo dnf install ncdu

The general syntax of the ncdu command is written below:

$ ncdu <options> <directory>
  • ncdu – It shows the execution of the ncdu command.
  • option – Choose the tree command built-in option.
  • directory – Path of the desired directory

Replace the options with any of the available ncdu options and directory with the complete path of the directory.

Find the Size of the Current Directory

To get the size of the current working directory utilizing the ncdu command, run the below command:

Note: In this case, the current directory is “/home/itslinuxfoss”. 

$ ncdu

The output shows the numerical value of the respective directory’s size with its relative size (## column) and file directory. 

Find Size of the Specified Directory (Recursively)

To get the size of a specified directory; for example, “/home/itslinuxfoss/Desktop”, use the below command by replacing the specific directory path with /Desktop:

Note: “r” flag is used to get the directory size recursively.

$ ncdu -r ~/Desktop

The output shows the size and name of the file in the Desktop directory.

That’s the end!

Conclusion

The “du”, “tree”, and the “ncdu” command line utilities are exercised to get the size of a directory in Linux. By determining the directory size, you can find which directory consumes the most space. With this in mind, we have discussed the above-stated commands to get the size of a directory in Linux. All the possibilities to get the directory size are demonstrated in this post.