Table of Contents
- Basic Syntax of the du Command
- Find Current Directory Size
- Find the Size of the Specific Directory
- Display the Directory Size in Human Readable Format
- Display the Directory Size in a Specific Format
- Find the Total Size Of The Directory
- Find the Size of Both Files and Directories
- Sort Directories By Their Size
- Find the Largest File or Directory
- Scan up to a Chosen Subdirectory Level
- Conclusion
Finding the size of files and directories from the Linux command line can feel confusing if you are new to Linux. Fortunately, Linux provides a powerful utility called du (Disk Usage) that helps you analyze disk space usage easily.
The du command shows how much disk space files and directories consume. With different options, you can view sizes in human-readable format, calculate totals, sort directories by size, and even find the largest files on your system.
In this guide, you will learn how to check directory and file sizes in Linux using the du command, with clear examples.
Basic Syntax of the du Command
The basic syntax of the du command is shown below:
du [OPTION]... [FILE]...
A brief explanation of commonly used option is shown below:
- -c – Produce a grand total
- -h – Print sizes in a human-readable format
- -S – For directories do not include the size of subdirectories
- -a – Display the disk usage of all items including files and directories
- -k – Display the disk usage size in kilobytes
- -m – Display the disk usage size in megabytes
- -X – Exclude certain file type
Also Read
How to Check Size of Files and Directory on Linux
Find Current Directory Size
Running du without any options shows the disk usage of the current directory and all its subdirectories.
du
You will get the following output:
8 ./html/example.com 20 ./html 3424 ./log/installer 84 ./log/apache2 236 ./log/apt du: cannot read directory './log/gdm3': Permission denied 4 ./log/gdm3 483588 ./log/journal/cfefe3a7c8694e51879fb521a2021b2e 483592 ./log/journal 64 ./log/cups 500192 ./log 509272 .
Find the Size of the Specific Directory
To check the size of the specific directory, run the du command by specifying a directory:
du /var/www/html
This command will display the size of the html directory, including all of its sub-directories:
8 /var/www/html/example.com 20 /var/www/html
If you want to find the size of the multiple directories, run the following command:
du /home/vyom/Music/ /home/vyom/Downloads
You will get the following output:
4996 /home/vyom/Music/ 8 /home/vyom/Downloads/html/example.com 20 /home/vyom/Downloads/html 3424 /home/vyom/Downloads/log/installer 84 /home/vyom/Downloads/log/apache2 236 /home/vyom/Downloads/log/apt 4 /home/vyom/Downloads/log/gdm3 483588 /home/vyom/Downloads/log/journal/cfefe3a7c8694e51879fb521a2021b2e 483592 /home/vyom/Downloads/log/journal 64 /home/vyom/Downloads/log/cups 500192 /home/vyom/Downloads/log 509272 /home/vyom/Downloads
Display the Directory Size in Human Readable Format
By default, the du command will display size in bytes. You can use the –h option with the du command to display the size of the directory in a human-readable format.
du -h Downloads/
You will get the following output:
8.0K Downloads/html/example.com 20K Downloads/html 3.4M Downloads/log/installer 84K Downloads/log/apache2 236K Downloads/log/apt 4.0K Downloads/log/gdm3 473M Downloads/log/journal/cfefe3a7c8694e51879fb521a2021b2e 473M Downloads/log/journal 64K Downloads/log/cups 489M Downloads/log 498M Downloads/
Display the Directory Size in a Specific Format
You can also display the directory size in KB, MB, or GB format. For example, to display the directory size in megabytes, use the -m option:
du -m Downloads/
You will get the following output:
1 Downloads/html/example.com 1 Downloads/html 4 Downloads/log/installer 1 Downloads/log/apache2 1 Downloads/log/apt 1 Downloads/log/gdm3 473 Downloads/log/journal/cfefe3a7c8694e51879fb521a2021b2e 473 Downloads/log/journal 1 Downloads/log/cups 489 Downloads/log 498 Downloads/
To display the directory size in kilobytes, use the -k option:
du -k Downloads/
You will get the following output:
8 Downloads/html/example.com 20 Downloads/html 3424 Downloads/log/installer 84 Downloads/log/apache2 236 Downloads/log/apt 4 Downloads/log/gdm3 483588 Downloads/log/journal/cfefe3a7c8694e51879fb521a2021b2e 483592 Downloads/log/journal 64 Downloads/log/cups 500192 Downloads/log 509272 Downloads/
Find the Total Size Of The Directory
You can use the du command with the -hs option to display the total size of the specified directory in a human-readable format.
du -sh /etc/
You will get the total size of the /etc directory in the following output:
16M /etc/
If you want to display the grand total of the combined directories in human-readable format, use the -c option:
du -csh Downloads/ Music/ Pictures/
You will get the following output:
498M Downloads/ 4.9M Music/ 2.6M Pictures/ 505M total
Find the Size of Both Files and Directories
You can use the du command with the -a option to display the size of both files and directories:
du -ah Downloads/
You will get the following output:
8.0K Downloads/LocalSettings.php 4.0K Downloads/html/index.html 4.0K Downloads/html/index.nginx-debian.html 4.0K Downloads/html/example.com/index.html 8.0K Downloads/html/example.com 20K Downloads/html 8.9M Downloads/apache-zookeeper-3.5.6-bin.tar.gz 4.0K Downloads/image.png 8.9M Downloads/
Also Read
How to Use Rsync to Copy/Sync Files Between Servers
Sort Directories By Their Size
If your system is running out of disk space and you want to find out which directories consume the most disk size, use the following command to sort all directories by their size:
du -h --max-depth=1 /var/log | sort -hr
You will get the following output:
1.9G /var/log/journal 1.9G /var/log 3.4M /var/log/installer 476K /var/log/teamviewer15 240K /var/log/apt 100K /var/log/unattended-upgrades 84K /var/log/apache2 68K /var/log/nginx 64K /var/log/postgresql 64K /var/log/cups 40K /var/log/mysql 8.0K /var/log/hp 4.0K /var/log/sysstat 4.0K /var/log/speech-dispatcher 4.0K /var/log/gdm3 4.0K /var/log/dist-upgrade
Find the Largest File or Directory
You use the du command with sort to find the largest file or directory in your system.
Run the du command with -a option to find and display the largest file and directory:
du -a / | sort -n -r | head -n 10
You should see the following output:
1653740 / 931232 /usr 483812 /swapfile 425916 /usr/lib 268820 /usr/src 164476 /usr/share 156912 /var 151216 /usr/lib/x86_64-linux-gnu 142644 /var/lib 137388 /usr/lib/modules
Scan up to a Chosen Subdirectory Level
By default, the du command scans all subdirectories recursively, which can produce a very long output. When you only want a summary view of disk usage, you can limit how deep du scans using the --max-depth option.
For example, to scan only the selected directory itself and ignore all subdirectories, set --max-depth=0.
du -h --max-depth=0 ./Downloads/
Output.
2.7G ./Downloads/
This shows only the total size of the Downloads directory, without listing any subfolders.
To view the size of direct child directories (one level deep), use: