Brief: The du command in Linux is used for checking the size of directory. Here are various ways you can find the size of directory in Linux with the du command.
Knowing the size of a file is easy in Linux. All you have to do is to use the -l and -h option with the ls command and it will show you the file size along with file permissions and file timestamps. Here’s a sample output:
ls -lh tutorials
-rwxr--r-- 1 abhishek abhishek 456 Mar 6 16:21 agatha.txt
-rw-r--r-- 1 abhishek abhishek 0 Apr 16 19:53 a.t
-rwxr--r-- 1 abhishek abhishek 140 Mar 22 16:41 bash_script.sh
-rw-rw-r-- 1 abhishek abhishek 95 Feb 11 13:12 cpluplus.cpp
-rw-r--r-- 1 abhishek abhishek 163 Apr 13 15:07 prog.py
-rw-r--r-- 1 abhishek abhishek 19K Mar 18 18:46 services
-rw-r--r-- 1 abhishek abhishek 356 Dec 11 21:35 sherlock.txt
-rwxrw-r-- 1 abhishek abhishek 72 Jan 21 15:44 sleep.sh
drwxr-xr-x 3 abhishek abhishek 4.0K Jan 4 20:10 target
drwxr-xr-x 2 abhishek abhishek 4.0K Apr 16 18:27 test_dir
-rw-rw-r-- 1 abhishek abhishek 55 Mar 11 16:28 text-file.txt
You would notice something strange. The ls command shows the size of all the directories as 4KB. That cannot be correct, can it be? Of course not.
The size of a folder or directory in Linux can be found using the du command. du here stands for disk usage. I’ll explain the logic behind the 4.0K size for the directories later in this tutorial. For the moment, let’s focus on getting the directory size.
Using du command to get directory size in Linux
I am going to show you various examples of the du command that you can use to check the directory size and the disk utilization.
The syntax for the du command is pretty simple.
du [option] path_to_file_or_directory
Let’s see how to use the du command to get the file and directory size information in Linux.
Here’s the structure of the “tutorials” directory I am going to use in this tutorial:
│ ├── agatha.txt
│ ├── file1.txt
│ └── past
│ ├── file1.txt
│ ├── file2.txt
│ └── source1
│ └── source2
│ └── file1.txt
│ ├── c.xyz
│ ├── myzip1.zip
│ └── myzip2.zip
5 directories, 16 files
Now if I run the du command in the “tutorials” directory, it will show the sizes of all the subdirectories and then sum the sizes of all the subdirectories and the files at the bottom of it.
This is the output for the tutorials directory.
Show disk size in human readable format
Now the problem with the above output of the du command is that you don’t know if the 100 is 100KB or 100MB or 100GB. Don’t worry, you can change this behavior and display the directory size in a human readable format with the option -h.
du -h tutorials
Here’s the much easier to read output now:
You can use -m option for MB and -k option for KB instead of -h. But even if the size is less than 1 MB, it will always show the size as 1MB. This is why using -h option is always a better option.
Show the sizes of the files as well
Did you notice that the tutorials directory has several files but they don’t show up in the du command output? It’s because though the file size are counted in the total sum of the directory size, the files are not displayed by default.
To display the size of files along with the directories, you can use the -a option. It would be better if you combine it with -h option to get the sizes in human readable format.
du -ah tutorials
Now the output will show the files along with the directories:
Solving the mystery of 4 KB
Show only the total size of the directory in Linux
If you find the output of the du command too verbose and would like to see just the total size of the directory in a human readable format, you can use the sum option -s.
du -sh tutorials
Now the output will be just one line showing the total size of the directory:
Show the disk usage by multiple directories
It’s not that you are restricted to check the size of only one directory at a time. You can specify multiple directories in the du command.
For example, I am going to use the -sh options to show the total size of two directories here.
du -sh tutorials/target/ tutorials/test_dir
The output will show the size of both the directories individually:
Show the grand total of all the directory sizes
In the above example, you saw the total sizes of both the directories individually. You can use the option -c to show a grand total for the sum of all the directories in the output.
du -csh tutorials/target/ tutorials/test_dir
As you can see in the output, it sums up the sizes and gives you the grand total:
Don’t show the sizes of the subdirectories
What if you want to check the sizes of the all the directories in the current folder? By default, du command will go into the subdirectories of all the directories and the output becomes difficult to figure out specially if you have too many nested directories.
What you can do is to define the depth level to check while showing the sizes for the subdirectories.
So, if you want to see the sizes of the directories in the current folder, you can set the depth to 1 like this:
du -h --max-depth=1 tutorials
Now the output will show the subdirectories only in the current directory. It won’t go further than this.
If you cannot remember the –max-depth, you can use its short form -d flag:
du -h -d1 tutorials
Exclude certain type of files while calculating disk size
The du command gives you option to exclude certain type of files. You can use regex along with the –exclude option.
For example, to calculate the sum of all the files excluding files with extension txt, this command can be used:
du -h --exclude="*.txt" tutorials
And now if you see the output, the total size of the directory would have been reduced:
Bonus Tip: Finding the biggest subdirectory
You can combine the output of the du command with the sort command to sort the directories by the order of their size.
du -h --max-depth=1 tutorials | sort -rh
This will show the directories in the reverse order of their size i.e. the biggest directory on the top.
Of course the top one is the directory itself but the second one gives you the biggest subdirectory.
Did you find it useful?
I have tried to explain all the essential usage of the du command in Linux. But as always, there are many more options available for the command that you can find in its man page. If you just wanted to find out the size of a directory in Linux, this tutorial should give you enough information. If you want to check disk space in Linux, use the df command.
Did you like the tutorial? Did it help you? Let me know in the comments. If you have some other cool tip about du command, why not share it with us?