Open In App

How to Check the Size of a Directory in Linux

Last Updated : 14 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Many times during working on Linux we may encounter situations, where we want to check the total size occupied by a directory, especially when working with large files and directories. 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.

Linux offers built-in tools like the ducommand and graphical file managers to quickly check the size of directories, whether you prefer the terminal or a GUI approach.

Checking Size
Checking the Size of a directory

In this article, we’ll explore effective methods to check the size of on a Linux system, making it easier to manage your storage efficiently.

Checking Size of a Directory in Linux

There are several ways to check the size of a directory in Linux. Common methods include using the du command for detailed disk usage, df to analyze overall filesystem usage, and graphical tools like File Manager or ncdu for a user-friendly approach:

By du Command

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:

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

Note: The sizes listed are in kilobytes.

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. 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

By 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

By ncdu Command

The ncdu (NCurses Disk Usage) command provides an interactive way to analyze the size of directories in Linux. It displays a clear, sortable list of directory sizes, making it easy to identify space-hogging directories.

Use the below command:

ncdu
Screenshot-from-2025-01-14-11-05-23
  • Directory and File Sizes: Displays sizes of all files and subdirectories in the selected directory.
  • Human-Readable Format: Shows sizes in KB, MB, or GB for clarity.
  • Sorted List: Automatically sorts directories and files by size for easy analysis.
  • Interactive Navigation: Allows you to browse through subdirectories interactively.
  • Total Disk Usage: Displays the total size of the selected directory.
  • Deletion Option: Lets you delete unnecessary files directly from the interface.

Why Checking Directory Size in Linux is Important

Checking directory size in Linux is important because:

  • It Monitors Disk Usage: Helps track storage consumption across directories.
  • Identifies Large Directories: Pinpoints directories using excessive space.
  • Prevents Disk Full Errors: Avoids storage-related system issues.
  • Optimizes System Performance: Ensures enough space for applications to run smoothly.
  • Supports Storage Planning: Helps decide when to add or upgrade storage.
  • Maintains Filesystem Organization: Keeps the directory structure clean and efficient.

Also Check -

Conclusion

Knowing how to check the size of a directory in Linux is essential for managing disk space effectively. Whether you use the du command for precise details or a file manager for a visual approach, these methods make it simple to monitor and control storage usage. Regularly checking directory sizes helps keep your system organized, prevents storage issues, and ensures optimal performance.


Next Article

Similar Reads