How to Use du Command in Linux

du command in Linux

The du (disk usage) is the command used in Linux to check the directory size including its other contents as well as the size of individual files.

When files and directories started consuming a large amount of disk space on your hard disks then du is the best command to tell which directories or files causing this.

In this tutorial, we learn how to use du command in Linux to check disk usage (files or directories).

Du command & Syntax

The following line shows the syntax of du command:

du [options] [file or directory name]

By default, without any options, it displays the disk usage of the given file or directory and for each of the subdirectories in bytes. If no file or directory name mentioned then du commad display disk usage of the current working directory.

# du /boot
2508    /boot/grub2/fonts
3068    /boot/grub2/i386-pc
5592    /boot/grub2
4       /boot/efi/EFI/centos
8       /boot/efi/EFI
12      /boot/efi
16      /boot/loader/entries
20      /boot/loader
152440  /boot

Du command with -a option prints the disk usage of all the files within the directory.

# du -a /boot
7920    /boot/vmlinuz-4.18.0-147.5.1.el8_1.x86_64
3752    /boot/
4       /boot/grub2/
4       /boot/grub2/grub.cfg
4       /boot/grub2/grubenv
2504    /boot/grub2/fonts/unicode.pf2
2508    /boot/grub2/fonts
8       /boot/grub2/i386-pc/cs5536.mod
4       /boot/grub2/i386-pc/gcry_rsa.mod
8       /boot/grub2/i386-pc/lsacpi.mod
52      /boot/grub2/i386-pc/gcry_camellia.mod
4       /boot/grub2/i386-pc/cpio_be.mod
152440  /boot

You can see that it prints the total size at the end of the command where '152440' is the total size (in bytes) of '/boot' directory.

Get total size of specified directory

To display the total size of a  specified directory you have to use -s option but will not report subdirectories. The following example, it shows the total size of  '/boot' directory.

# du -s /boot
152440  /boot

You can print total disk usage from multiple directories as follows:

# du -s /boot /var
152440 /boot
332168 /var

Human readable format

So far we see the disk size was showing in bytes that is not much human-readable format. In order to print the output in kilobyte (K), megabyte (M) and gigabyte (G) we have to use -h option.

# du -h /boot
2.5M    /boot/grub2/fonts
3.0M    /boot/grub2/i386-pc
5.5M    /boot/grub2
4.0K    /boot/efi/EFI/centos
8.0K    /boot/efi/EFI
12K     /boot/efi
16K     /boot/loader/entries
20K     /boot/loader
149M    /boot

The --si option is like -h option but it uses powers of 1000 instead of 1024.

# du --si /boot
2.6M    /boot/grub2/fonts
3.2M    /boot/grub2/i386-pc
5.8M    /boot/grub2
4.1k    /boot/efi/EFI/centos
8.2k    /boot/efi/EFI
13k     /boot/efi
17k     /boot/loader/entries
21k     /boot/loader
157M    /boot

To get the total size of a specified directory in a human-readable format which is very commonly used du command, use the following command:

# du -sh /boot
149M /boot

Combine grand total size

This is very useful when you like to combine the total size on multiple directories. The following example du command combine (-c) the total disk size of specified (-s) '/boot' and '/var' directory and  print at the last line in human-readable format (-h):

# du -csh /boot/ /var
149M    /boot/
317M    /var
466M    total

Display individual size of all files and directories

Using wildcard (*) we can print the individual size of each file and directories and then combine total size to print in the last line.

# du -csh /boot/*
184K    /boot/config-4.18.0-147.5.1.el8_1.x86_64
180K    /boot/config-4.18.0-80.7.1.el8_0.x86_64
12K     /boot/efi
0       /boot/grub
5.5M    /boot/grub2
53M     /boot/initramfs-0-rescue-3e729c2d7c094902af0333ce40564ffe.img
23M     /boot/initramfs-4.18.0-147.5.1.el8_1.x86_64.img
23M     /boot/initramfs-4.18.0-80.7.1.el8_0.x86_64.img
16M     /boot/initramfs-4.18.0-80.7.1.el8_0.x86_64kdump.img
20K     /boot/loader
3.7M    /boot/
3.6M    /boot/
7.6M    /boot/vmlinuz-0-rescue-3e729c2d7c094902af0333ce40564ffe
7.8M    /boot/vmlinuz-4.18.0-147.5.1.el8_1.x86_64
7.6M    /boot/vmlinuz-4.18.0-80.7.1.el8_0.x86_64
149M    total

Du command with Pattern matching

Using du command you can perform pattern matching. In the following command it shows how to find disk size of all directories starting with 'bo'.

# du -sch /bo*
149M    /boot
217M    /bootstrap
366M    total

Using du command to get Apparent size

The --apparent-size option prints apparent sizes (actual amount of data in the file) rather than disk usage.

# du --apparent-size /boot
2505    /boot/grub2/fonts
2544    /boot/grub2/i386-pc
5057    /boot/grub2
4       /boot/efi/EFI/centos
8       /boot/efi/EFI
12      /boot/efi
6       /boot/loader/entries
10      /boot/loader
151852  /boot

Check directory size which include hard links

Using the -l or --count-links option, the sizes are counted many times if hard linked. By default, the hard links are not displayed.

For example

# ls -li
 total 648
 755150 -rw-r--r-- 1 root root 4719 Apr 19 00:46 file1
 755152 -rw-r--r-- 2 root root 312519 Apr 19 00:47 file2
 755152 -rw-r--r-- 2 root root 312519 Apr 19 00:47 file2.hard
 755153 lrwxrwxrwx 1 root root 19 Apr 19 00:57 file2.soft -> /etc/sysconfig/init

Here, file2.hard is the hard link for file2. Now notice the output with -l and without it.

# du -ah
 4.0K ./file2.soft
 316K ./file2
 12K ./file1
 340K .

# du -ahl
 4.0K ./file2.soft
 316K ./file2
 12K ./file1
 316K ./file2.hard
 656K .

The default behavior for symbolic links is not to dereference them. For dereferencing symbolic links, -L or --dereference option is used

# du -ahL
 8.0K ./file2.soft
 316K ./file2
 12K ./file1
 344K .

(Note that size for file2.soft has changed now)

The -P or --no-dereference option does not dereference these symlinks (which is the default behavior as stated above).

Display disk usage at 'N' level sub directories

This option instructs du command to list the subdirectories and its size to our desired depth level.

For example, the below example lists the directories to the first tier only in the current directory tree and their size. Even total consumption of space is also reported here. In case if we set the --max-depth= to zero, then du command will not list any subdirectories and it will only report the size of selected directory.

# du --max-depth=1 -h /boot/
7.1M /boot/grub
110M /boot/

Du command to exclude files

Du command can exclude specific directory using the --exclude option. In the following example I have excluded 'grub2' directory:

# du  --exclude=grub2 /boot/
4       /boot/efi/EFI/centos
8       /boot/efi/EFI
12      /boot/efi
16      /boot/loader/entries
20      /boot/loader
146848  /boot/

Below are the two examples to show you how to exclude '*.obj' or '*.jpg' files.

# du -h –exclude=’*.obj’
# du -h –exclude=’*.jpg’

Use du command to print modification time

The modification time of files and/or directories (or of files/directories in the subdirectories) can be displayed with --time option.

# du /boot/ --time
4    2017-03-23 15:03    /boot/grub/locale
2348    2017-03-23 15:03    /boot/grub/fonts
2480    2017-03-23 15:03    /boot/grub/i386-pc
7212    2017-04-05 08:31    /boot/grub
112180    2017-04-05 08:31    /boot/

Use du with other commands

Du command can be combined with other command using pipes (|). Lets check few examples

If we want to find top 3 directories by size in the current working directory:

# du -skh * | sort -nr | head -3
386M    lib
252M    share
154M    lib64

Display all files and directories sorted by size:

# du -sk * | sort -n
12160   libexec
37552   sbin
58900   bin
70048   src
157456  lib64
257492  share
394504  lib


In this tutorial, we learned how to use du command in Linux to estimate disk usage for files and directories. I hope you enjoyed reading and please leave your suggestions in the below comment section.

Leave a Comment