Linux Get Size of Directory: An Ultimate Guide
Knowing the size of a directory in Linux is essential for system administrators and developers. It enables them to understand the disk space consumption and optimize the storage resources accordingly. In this article, we will discuss the various methods to get the size of a directory in Linux, including the most popular “du” command and the less-known “tree,” “ncdu,” “find,” and “stat” commands.
We have researched various sources, including Linuxize, Unix Stack Exchange, PhoenixNAP, Stack Abuse, and OSTechNix, to provide you with the most comprehensive guide on getting the size of a directory in Linux. Let’s dive into the details.
Using the “du” Command
The “du” command is the most commonly used method to get the size of a directory in Linux. It stands for “Disk Usage” and measures the disk space consumed by the files and directories. Here’s how to use it:
Basic Usage
To get the size of a directory, open the terminal and type the following command:
du <directory>
For example, to get the size of the “/home/user/Documents” directory, type:
du /home/user/Documents
This will display the size of the directory in bytes.
Human-Readable Output
The output of the “du” command is not very user-friendly as it displays the size in bytes. To make it more readable, you can use the “-h” option, which stands for “human-readable.” This option displays the size in a more understandable format, such as KB, MB, and GB.
To get the size of a directory in a human-readable format, use the following command:
du -h <directory>
For example, to get the size of the “/home/user/Documents” directory in human-readable format, type:
du -h /home/user/Documents
This will display the size of the directory in a more readable format.
Displaying Largest Directories
The “du” command can be combined with other commands using pipes to print the largest directories within a directory. The following command will display the top 10 largest directories within the “/home/user/Documents” directory:
du -h /home/user/Documents | sort -hr | head -n 10
This command uses the “sort” and “head” commands to display the largest directories in a human-readable format.
In summary, the “du” command is a powerful tool to get the size of a directory in Linux. It offers various options to display the size in a human-readable format and to print the largest directories within a directory.
Using the “tree” Command
The “tree” command is a lesser-known tool to get the size of a directory in Linux. It lists the contents of a directory in a tree-like format and displays the size of each file and subdirectory. Here’s how to use it:
Basic Usage
To get the size of a directory using the “tree” command, open the terminal and type the following command:
tree -h <directory>
For example, to get the size of the “/home/user/Documents” directory, type:
tree -h /home/user/Documents
This will display the size of the directory along with the size of each subdirectory and file in a tree-like format.
Customizing Output
The “tree” command offers various options to customize the output. For example, the “-L” option limits the depth of the tree to a specific level. The following command will display the size of the “/home/user/Documents” directory up to a depth of 2 levels:
tree -h -L 2 /home/user/Documents
This will display the size of the “/home/user/Documents” directory and its immediate subdirectories.
Displaying Largest Directories
The “tree” command can also be used to display the largest directories within a directory. The following command will display the top 10 largest directories within the “/home/user/Documents” directory:
tree -h /home/user/Documents | tail -n +2 | sort -hr | head -n 10
This command uses the “tail,” “sort,” and “head” commands to display the largest directories in a human-readable format.
In summary, the “tree” command is a useful tool to get the size of a directory in Linux. It displays the size of each file and subdirectory in a tree-like format and offers various options to customize the output.
Using the “find” and “stat” Commands
The “find” and “stat” commands can be used together to get the size of a directory in Linux. Here’s how to use them:
Basic Usage
To get the size of a directory using the “find” and “stat” commands, open the terminal and type the following command:
find <directory> -type f -print0 | xargs -0 stat --format='%s' | paste -sd+ - | bc
For example, to get the size of the “/home/user/Documents” directory, type:
find /home/user/Documents -type f -print0 | xargs -0 stat --format='%s' | paste -sd+ - | bc
This will display the size of the directory in bytes.
Human-Readable Output
To get the size of a directory in a human-readable format, you can use the “awk” command to convert the output to a more readable format. The following command will display the size of the “/home/user/Documents” directory in a human-readable format:
find /home/user/Documents -type f -print0 | xargs -0 stat --format='%s' | paste -sd+ - | bc | awk '{ sum=$1 ; hum[1024**3]="Gb";hum[1024**2]="Mb";hum[1024]="Kb"; for (x=1024**3; x>=1024; x/=1024){ if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x];break } } if (sum<1024) printf "%d bytes\n",sum }'
This will display the size of the directory in a human-readable format, such as KB, MB, and GB.
Limitations
The “find” and “stat” commands only include file sizes and ignore auxiliary information or compression. This means that the size displayed may not be the actual space consumed by the directory.
In summary, the “find” and “stat” commands can be used together to get the size of a directory in Linux. They offer the option to display the size in a human-readable format, but may not display the actual space consumed by the directory.
Using the “ncdu” Command
The “ncdu” command is a powerful tool to get the size of a directory in Linux. It stands for “NCurses Disk Usage” and displays the disk usage in a user-friendly, interactive interface. Here’s how to use it:
Installation
Before using the “ncdu” command, you need to install it on your system. To install it on Ubuntu or Debian, use the following command:
sudo apt-get install ncdu
For other Linux distributions, you can check your package manager or download the source code from the official website.
Basic Usage
To get the size of a directory using the “ncdu” command, open the terminal and type the following command:
ncdu <directory>
For example, to get the size of the “/home/user/Documents” directory, type:
ncdu /home/user/Documents
This will display the size of the directory in a user-friendly, interactive interface.
Customizing Output
The “ncdu” command offers various options to customize the output. For example, the “-rr” option allows you to display the size of a directory and its subdirectories recursively. The following command will display the size of the “/home/user/Documents” directory and its subdirectories:
ncdu -rr /home/user/Documents
This will display the size of the directory and its subdirectories in a user-friendly, interactive interface.
Displaying Largest Directories
The “ncdu” command can also be used to display the largest directories within a directory. After running the command, press the “d” key to sort the directories by size, then press the “r” key to reverse the order. This will display the largest directories at the top.
In summary, the “ncdu” command is a powerful tool to get the size of a directory in Linux. It offers a user-friendly, interactive interface and various options to customize the output.
Comparing the Different Methods
All the methods discussed in this article have their own advantages and limitations. Here’s a comparison of the different methods:
“du” Command
The “du” command is the most commonly used method to get the size of a directory in Linux. It’s simple, fast, and displays the actual space consumed by the directory. However, it may not be as user-friendly as other methods and may require additional commands to display the largest directories.
“tree” Command
The “tree” command is a lesser-known tool to get the size of a directory in Linux. It displays the size of each file and subdirectory in a tree-like format and offers various options to customize the output. It’s user-friendly and can display the largest directories within a directory. However, it may not be as fast as other methods and may not display the actual space consumed by the directory.
“find” and “stat” Commands
The “find” and “stat” commands can be used together to get the size of a directory in Linux. They offer the option to display the size in a human-readable format, but may not display the actual space consumed by the directory. They require more commands to display the largest directories.
“ncdu” Command
The “ncdu” command is a powerful tool to get the size of a directory in Linux. It offers a user-friendly, interactive interface and various options to customize the output. It can display the largest directories within a directory. However, it may not be as fast as other methods and requires additional installation.
In summary, each method has its own advantages and limitations. The choice of method depends on the user’s preference and the task at hand.
Final Thoughts
Getting the size of a directory in Linux is a simple task but can be useful in various scenarios. The methods discussed in this article offer different options to display the size of a directory and its subdirectories. The choice of method depends on the user’s preference and the task at hand.
Before you go, make sure to check out our other great content on Linux and related topics. We have articles on various Linux commands, tools, and applications to help you become a Linux pro.
Thank you for reading, and happy Linux-ing!
Questions and Answers
What is the fastest way to get the size of a directory in Linux?
The “du” command is the fastest way to get the size of a directory in Linux.
How to display the largest directories in a directory in Linux?
Use the “du” command with pipes and the “sort” command to display the largest directories in a directory in Linux.
What is the “tree” command in Linux and how does it work?
The “tree” command is a tool in Linux to display the size of each file and subdirectory in a tree-like format.
How to exclude certain file types while getting the size of a directory in Linux?
Use the “du” command with pipes and the “find” command to exclude certain file types while getting the size of a directory in Linux.
What is the “ncdu” command in Linux and how does it work?
The “ncdu” command is a powerful tool in Linux to get the size of a directory with a user-friendly, interactive interface.
What are the different methods to get the size of a directory in Linux?
The different methods to get the size of a directory in Linux are the “du,” “tree,” “find” and “ncdu” commands.