How to Get Size of Directory in Linux
Abstract: short for disk usage. The syntax for du usage is du [OPTION]... [FILE] [directory] Check Size of a Directory To find out the size of a directory run #
In this article, we focus on how you can check the size of Directory on Linux Systems. This we are going to achieve using the command line interface.
The command used to retrieve the file sizes is the du command, short for disk usage.
The syntax for du usage is
du [OPTION]... [FILE] [directory]
Check Size of a Directory
To find out the size of a directory run
# du -sh
Let's break this command down further
-s , --summarize - Displays only a total for each argument
-h , --human-readable - This prints the sizes in a human readable format. e.g 50G , 285M
For example, to get the size of a directory, run
# du -sh directory_name/
In this case, I have a directory called 'django' in my current working directory. To find its size, I ran,
# du -sh /django
Sample Output
252M django/
Display Directory size in KB/MB or GB
You can also decide to display disk usage size in Kb, MB or GB.
To accomplish this simply use -k for kilobytes, and -m for megabytes. Using our sample directory, the syntax will be
# du -k django/
Sample Output
To display the usage in Megabytes run,
# du -m django
Sample Output
As you may have noticed, the commands only display the disk usage of the directory but not the file sizes. To display the file sizes, use the -a flag as shown. the -a flag specifies all files and directories
# du -ah /django
Sample output
Find Total Directory UsageTo view the grand total usage of all files and directories in your directory, append the -c flag as shown
# du -csh django/
Output
252M django/
252M total
If you want to display only the grand total of a given directory including all the sub-directories, use grep command with ‘du’ command like below.
# du -ch django | grep docs
Sample Output
Find Sub Folder Disk UsageTo show which subfolders take up disk space. Run below command
# du -h --max-depth=1 | sort -hr
Sample Output
For more information about the du command run
# man du
That's all we had for you folks. We hope you found this tutorial helpful. Keep tuned in for more helpful topics.
Read Also:- How to Find Large Files & Directories in Linux