How to Check the Size of a Directory in Linux
Last Updated :
14 Jan, 2025
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 the Size of a directoryIn 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.
Checking the size of the current directoryNote: 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 the size of a specific subdirectory3. 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 the size of multiple directoriesIt 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)
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)
size in megabytes of directories5. 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 size of a directoryThe total sizes of multiple subdirectories can be obtained as weusing use the following command:
du -sh dir1/ dir2/ dir3/
The total size of multiple directoryThe grep command can also be used to obtain the total size of a directory.
du -ch Documents/ | grep total
Grep command to get the size of a directory6. 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 depthIn 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 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 commandBy 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 CommandTo 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 the Size of the Directory using the treeFor obtaining more information about the tree command you can check out the man page the of tree.
man tree
Help page tree commandBy 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
- 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.
Similar Reads
How to Remove Directory in Linux In Linux, directories are used to organize files and other directories into a hierarchical structure. Just like folders in Windows, directories in Linux can contain files, other directories, and links. Removing directories in Linux is a common task that you might need to perform as you manage files
5 min read
How to Check Disk Space in Linux Efficient disk space management is important for maintaining the performance and stability of your Linux system. Over time, files and applications can fill up your storage, potentially causing slowdowns or errors. Commands to Check DIsk Space in LinuxKnowing how to check disk space in Linux helps yo
6 min read
How to Manage Directories in Linux? Directories in Linux or any operating system help to organize the data and files, which is then easier to find and manage. In this article, you will learn everything you will need to manage directories in Linux. We will try to cover every topic from creating, and copying to deleting the directories
6 min read
How to check file size in Python? Prerequisites: os pathlib Given a file, the task here is to generate a Python Script to print its size. This article explains 2 methods to do so. ApproachImport moduleGet file sizeFile in use Name: Data.csv Size: 226 bytes Method1: Using pathlib Path().stat().st_size() function of pathlib module get
1 min read
Watcherd Shell Listener for Directory Changes in Linux Watcherd is a tool for Linux that helps to monitor directory changes and print whatever changes are done like adding and deleting for a particular directory and execute commands and shell scripts according to a particular event. Installing Watcherd on Linux Step 1: Downloading watcherd. Since watche
2 min read