11 Linux du Commands to Check Disk Usage Size of Files and Folders

Posted on : June 20, 2013 , Last Updated on : April 7, 2017 By
| Reply More

The Linux disk usage du command is one of the best commands in Linux to check the directory size including its other contents as well as the size of individual files. This command is also very useful in checking the space hogs mean those files and directories that are consuming a large amount of disk space on your hard disks or other storage mediums. In this article, we will explore options to this useful command.

1. Default Behavior

du [options] [file or directory name]

Here, the meaning of square bracket is they are the optional items. By default, without any option, it displays the disk usage for each of the subdirectories in the current working directory.

# du /boot/
4    /boot/grub/locale
2348    /boot/grub/fonts
2480    /boot/grub/i386-pc
7212    /boot/grub
112180    /boot

du -a  displays the disk usage of all the files and directories

# du -a /boot/
3188    /boot/config-4.4.0-66-generic
15220    /boot/initrd.img-4.4.0-70-generic
3792    /boot/System.map-4.4.0-71-generic
188    /boot/config-4.4.0-70-generic
10816    /boot/initrd.img-4.4.0-66-generic
6920    /boot/vmlinuz-4.4.0-70-generic
6920    /boot/vmlinuz-4.4.0-72-generic
1220    /boot/abi-4.4.0-72-generic
188    /boot/config-4.4.0-71-generic
1220    /boot/abi-4.4.0-70-generic
3792    /boot/System.map-4.4.0-72-generic
3792    /boot/System.map-4.4.0-70-generic
188    /boot/config-4.4.0-72-generic
6924    /boot/vmlinuz-4.4.0-66-generic
15220    /boot/initrd.img-4.4.0-72-generic
112180    /boot

You can see that it prints the total size at the end of the command where we see 112180 /boot but we have a long list of files

 2. Producing grand total readable

We can have each size for subdirectories in the current working directory and the grand total size printed in the last line of output.

# du -c /boot/
4    /boot/grub/locale
2348    /boot/grub/fonts
2480    /boot/grub/i386-pc
7212    /boot/grub
112180    /boot
112180    total

we can display only the total count

# du -s /boot
112180    /boot

3. Human readable format

Now we can have the size in our output easier to read. It will display the output in kilobyte (K), megabyte (M) and gigabyte (G). It is human readable method

# du -h /boot
4.0K    /boot/grub/locale
2.3M    /boot/grub/fonts
2.5M    /boot/grub/i386-pc
7.1M    /boot/grub
110M    /boot

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

# du --si /boot
4.1k    /boot/grub/locale
2.5M    /boot/grub/fonts
2.6M    /boot/grub/i386-pc
7.4M    /boot/grub
115M    /boot

4. Associate options of du command

We can associate differents options of our command to have a more powerful result. For example, to have the total folder size + each folder size with wildcard (*)

# du -shc /boot/*
1.2M    /boot/abi-4.4.0-66-generic
1.2M    /boot/abi-4.4.0-70-generic
1.2M    /boot/abi-4.4.0-71-generic
1.2M    /boot/abi-4.4.0-72-generic
188K    /boot/config-4.4.0-66-generic
188K    /boot/config-4.4.0-70-generic
188K    /boot/config-4.4.0-71-generic
188K    /boot/config-4.4.0-72-generic
7.1M    /boot/grub
11M    /boot/initrd.img-4.4.0-66-generic
15M    /boot/initrd.img-4.4.0-70-generic
15M    /boot/initrd.img-4.4.0-71-generic
15M    /boot/initrd.img-4.4.0-72-generic
3.8M    /boot/System.map-4.4.0-66-generic
3.8M    /boot/System.map-4.4.0-70-generic
3.8M    /boot/System.map-4.4.0-71-generic
3.8M    /boot/System.map-4.4.0-72-generic
6.8M    /boot/vmlinuz-4.4.0-66-generic
6.8M    /boot/vmlinuz-4.4.0-70-generic
6.8M    /boot/vmlinuz-4.4.0-71-generic
6.8M    /boot/vmlinuz-4.4.0-72-generic
110M    total

This format can further be customized with -B or --block-size= option. The block size is provided by this option. SIZE can be one of following: KB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y. For example,

# du /boot/ -BKB
5kB    /boot/grub/locale
2405kB    /boot/grub/fonts
2540kB    /boot/grub/i386-pc
7386kB    /boot/grub
114873kB    /boot/

# du /boot/ -BM
1M    /boot/grub/locale
3M    /boot/grub/fonts
3M    /boot/grub/i386-pc
8M    /boot/grub
110M    /boot/

If you do not want the size of subdirectories to be included, use -S option.

# du /boot/ -Sh
4.0K    /boot/grub/locale
2.3M    /boot/grub/fonts
2.5M    /boot/grub/i386-pc
2.4M    /boot/grub
103M    /boot/

# du /boot/ -ch
4.0K    /boot/grub/locale
2.3M    /boot/grub/fonts
2.5M    /boot/grub/i386-pc
7.1M    /boot/grub
110M    /boot/
110M    total

You can the difference of our result

5. Apparent size

The --apparent-size option prints apparent sizes rather than disk usage.

# du --apparent-size -h /boot
4.0K    /boot/grub/locale
2.3M    /boot/grub/fonts
2.0M    /boot/grub/i386-pc
6.6M    /boot/grub
109M    /boot

6. Playing with 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).

7. Limit directory traversal levels

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/

8. Exclude files

Suppose we wish to remove grub related files or jpg files, then we can use --exclude option

# du -ah --exclude=grub /boot/
188K    /boot/config-4.4.0-66-generic
15M    /boot/initrd.img-4.4.0-70-generic
3.8M    /boot/System.map-4.4.0-71-generic
188K    /boot/config-4.4.0-70-generic
11M    /boot/initrd.img-4.4.0-66-generic
6.8M    /boot/vmlinuz-4.4.0-70-generic
6.8M    /boot/vmlinuz-4.4.0-72-generic
1.2M    /boot/abi-4.4.0-72-generic
188K    /boot/config-4.4.0-71-generic
1.2M    /boot/abi-4.4.0-70-generic
3.8M    /boot/System.map-4.4.0-72-generic
3.8M    /boot/System.map-4.4.0-70-generic
188K    /boot/config-4.4.0-72-generic
6.8M    /boot/vmlinuz-4.4.0-66-generic
15M    /boot/initrd.img-4.4.0-72-generic
1.2M    /boot/abi-4.4.0-71-generic
6.8M    /boot/vmlinuz-4.4.0-71-generic
15M    /boot/initrd.img-4.4.0-71-generic
1.2M    /boot/abi-4.4.0-66-generic
3.8M    /boot/System.map-4.4.0-66-generic
103M    /boot/

In the below two examples *.obj or *.jpg files are excluded.

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

9. Print the 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/

10. Display by size

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

:/boot# # 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

11. Checking the version of command

As with most of the commands, --version option prints the version of du.

# du --version
du (GNU coreutils) 8.25
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Torbjörn Granlund, David MacKenzie, Paul Eggert,
and Jim Meyering.

Conclusion

We have seen how we can manipulate du command to have interesting results. It is helpful when used with -h option because it displays output readable by humans.

Filed Under : LINUX COMMANDS

Tagged With :

Free Linux Ebook to Download

Leave a Reply

All comments are subject to moderation.