Commands

Checking Directory Size with du Command in Linux

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.

Abhishek Prakash
Abhishek Prakash

Table of Contents

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
total 56K
-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 4 KB. 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.

If you want to check the directory size in Linux, you can use this command:

du -sh path_to_directory

This will give you the total size of the said directory in human-readable format, i.e. KB, MB or GB.

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:

tree tutorials
.
├── agatha.txt
├── bash_script.sh
├── cpluplus.cpp
├── prog.py
├── services
├── sherlock.txt
├── sleep.sh
├── target
│   ├── agatha.txt
│   ├── file1.txt
│   └── past
│       ├── file1.txt
│       ├── file2.txt
│       └── source1
│           └── source2
│               └── file1.txt
├── test_dir
│   ├── c.xyz
│   ├── myzip1.zip
│   └── myzip2.zip
└── text-file.txt
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.

du tutorials

This is the output for the tutorials directory.

8 tutorials/target/past/source1/source2
12 tutorials/target/past/source1
24 tutorials/target/past
36 tutorials/target
12 tutorials/test_dir
100 tutorials

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:

8.0K tutorials/target/past/source1/source2
12K tutorials/target/past/source1
24K tutorials/target/past
36K tutorials/target
12K tutorials/test_dir
100K tutorials

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:

4.0K tutorials/cpluplus.cpp
4.0K tutorials/prog.py
20K tutorials/services
4.0K tutorials/text-file.txt
4.0K tutorials/bash_script.sh
4.0K tutorials/target/file1.txt
4.0K tutorials/target/past/file2.txt
4.0K tutorials/target/past/file1.txt
4.0K tutorials/target/past/source1/source2/file1.txt
8.0K tutorials/target/past/source1/source2
12K tutorials/target/past/source1
24K tutorials/target/past
4.0K tutorials/target/agatha.txt
36K tutorials/target
4.0K tutorials/sherlock.txt
4.0K tutorials/test_dir/myzip1.zip
4.0K tutorials/test_dir/myzip2.zip
0 tutorials/test_dir/c.xyz
12K tutorials/test_dir
4.0K tutorials/sleep.sh
4.0K tutorials/agatha.txt
100K tutorials

Bonus Tip: Solving the mystery of 4 KB

Note that the sizes may seem like that have been rounded off. I mean all the sizes are in the multiple of 4K. In fact, apart from the empty files, all the files are of at least 4 KB in size. Is that a coincidence? Not really.

Even if the text of the file is in bytes, the minimum file size is 4K because that’s the minimum block size of the filesystem. Irrespective of the size of the text on the file, it will be allotted at least one block of 4KB memory on the disk.

And since the memory blocks are 4KB in size, the sizes you’ll see will always be in the multiple of 4KB.

Now, you probably already know that everything is file in UNIX/Linux. A directory is essentially a file that has the information about all the location of all the files it ‘contains’.

So, when you use the ls command, it treats the directory as a file and shows its size which is one memory block and thus the size displayed is 4KB.

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:

100K tutorials

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:

36K tutorials/target/
12K tutorials/test_dir

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:

36K tutorials/target/
12K tutorials/test_dir
48K 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.

36K tutorials/target
12K tutorials/test_dir
100K tutorials

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:

4.0K tutorials/target/past/source1/source2
8.0K tutorials/target/past/source1
12K tutorials/target/past
16K tutorials/target
12K tutorials/test_dir
68K tutorials

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.

100K tutorials
36K tutorials/target
12K tutorials/test_dir

Of course the top one is the directory itself but the second one gives you the biggest subdirectory.

You can further combine with the head command or the tail command to get the x largest file or smallest files/directories.

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?



Join the conversation.