Linux du Command All Options To Study

June 20, 2013 | By
| Reply More

linux du command

The linux disk usage command, du estimates the disk space used by files and (recursively for) directories. What files and folders are consuming how much disk space is reported by this command. In this article, we explore options to this useful command.

du command and options

1. Default Behavior

By default, without any option, it displays disk usage of all the directories only.

# du /boot/
12 /boot/lost+found
236 /boot/grub
9875 /boot/

With -a or --all option, not only files, but directories are also listed.

# du -a /boot/
3311 /boot/initrd-latest.img
71 /boot/config-2.6.18-238.el5
12 /boot/lost+found
2 /boot/.vmlinuz-2.6.18-238.el5.hmac
1854 /boot/vmlinuz-2.6.18-238.el5
3312 /boot/initrd-2.6.18-238.el5.img
8 /boot/grub/iso9660_stage1_5
34 /boot/grub/splash.xpm.gz
2 /boot/grub/grub.conf
8 /boot/grub/vstafs_stage1_5
8 /boot/grub/ffs_stage1_5
9 /boot/grub/jfs_stage1_5
9 /boot/grub/e2fs_stage1_5
105 /boot/grub/stage2
2 /boot/grub/stage1
11 /boot/grub/reiserfs_stage1_5
8 /boot/grub/ufs2_stage1_5
10 /boot/grub/xfs_stage1_5
8 /boot/grub/minix_stage1_5
1 /boot/grub/menu.lst
9 /boot/grub/fat_stage1_5
2 /boot/grub/device.map
236 /boot/grub
963 /boot/System.map-2.6.18-238.el5
112 /boot/symvers-2.6.18-238.el5.gz
9875 /boot/

2. Producing the grand total

With -c option, total is printed in the last line of output. You can add h option along with c (say -ch) so it will be more understandable result.

# du -c /boot/
12 /boot/lost+found
236 /boot/grub
9875 /boot/
9875 total

If you want to display just the total (as in the above command) and nothing else, you can also summarize with -s option:

# du /boot/ -s
9875 /boot/

Total folder size + each folder size

# du -shc *
4.0K anaconda-ks.cfg
4.0K Desktop
4.0K Documents
4.0K Downloads
37M equallogic-host-tools-1.2.0-2.iso
12K install.log
4.0K install.log.syslog
4.0K memstat.sh
4.0K Music
4.0K Pictures
4.0K Public
4.0K Templates
4.0K Videos
37M total
#

Total folder size + each folder size + sorted

# du -shc * | sort -h
4.0K anaconda-ks.cfg
4.0K Desktop
4.0K Documents
4.0K Downloads
4.0K install.log.syslog
4.0K memstat.sh
4.0K Music
4.0K Pictures
4.0K Public
4.0K Templates
4.0K Videos
12K install.log
37M equallogic-host-tools-1.2.0-2.iso
37M total
#

3. Apparent size

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

# du /boot/ --apparent-size
12 /boot/lost+found
210 /boot/grub
9794 /boot/

4. Human readable format

The -h option prints the output in human readable format (e.g., 1K 234M 2G).

# du /boot/ -h
12K /boot/lost+found
236K /boot/grub
9.7M /boot/

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

# du /boot/ --si
13k /boot/lost+found
242k /boot/grub
11M /boot/

This format can further be customized with -B or --block-size=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
13kB /boot/lost+found
242kB /boot/grub
10112kB /boot/

# du /boot/ -B2KB
7 /boot/lost+found
121 /boot/grub
5056 /boot/

# du /boot/ -BK
12K /boot/lost+found
236K /boot/grub
9875K /boot/

# du /boot/ -BM
1M /boot/lost+found
1M /boot/grub
10M /boot/

The -b or --bytes option is equivalent to `--apparent-size --block-size=1'

# du /boot/ -b
12288 /boot/lost+found
214693 /boot/grub
10028389 /boot/

# du /boot/ --apparent-size --block-size=1
12288 /boot/lost+found
214693 /boot/grub
10028389 /boot/

Similarly, -k and -m options are equivalent to --block-size=1K and --block-size=1M respectively.

# du -k /boot/
12 /boot/lost+found
236 /boot/grub
9875 /boot/

# du -m /boot/
1 /boot/lost+found
1 /boot/grub
10 /boot/

5. 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 --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).

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

# du /boot/
12 /boot/lost+found
236 /boot/grub
9875 /boot/

# du /boot/ -S
12 /boot/lost+found
236 /boot/grub
9627 /boot/

6. Limit directory traversal levels

The -d or --max-depth=N option limits the directory traversal to Nth level. According to the man page of du command, "print the total for a directory (or file, with --all) only if it is N or fewer levels below the command line argument; --max-depth=0 is the same as --summarize"

# du --max-depth=1 -h /
100K /tftpboot
7.7M /bin
37M /sbin
8.0K /srv
5.7M /root
0 /sys
2.7G /dump
90M /lib
36K /tmp
88K /dev
16K /aanchal
111M /etc
20K /javaproject
8.0K /shared
8.0K /opt
0 /selinux
1.3M /home
0 /net
9.7M /boot
8.0K /media
20K /share
300K /lost+found
0 /proc
8.0K /mnt
0 /misc
85M /var
2.1G /usr
5.1G /

7. Exclude files from being displayed

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

# du -a /boot/ --exclude=grub
3311 /boot/initrd-latest.img
71 /boot/config-2.6.18-238.el5
12 /boot/lost+found
2 /boot/.vmlinuz-2.6.18-238.el5.hmac
1854 /boot/vmlinuz-2.6.18-238.el5
3312 /boot/initrd-2.6.18-238.el5.img
963 /boot/System.map-2.6.18-238.el5
112 /boot/symvers-2.6.18-238.el5.gz
9639 /boot/

8. 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
12 2012-07-03 03:47 /boot/lost+found
236 2012-07-03 13:23 /boot/grub
9875 2012-08-03 10:47 /boot/

9. Checking the version of command

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

# du --version
du (GNU coreutils) 5.97
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software. You may redistribute copies of it under the terms of
the GNU General Public License .
There is NO WARRANTY, to the extent permitted by law.

Written by Torbjorn Granlund, David MacKenzie, Paul Eggert, and Jim Meyering.

Filed Under : LINUX COMMANDS

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.