8 Examples - Linux du Command With All Its Options

September 6, 2011 | By
| 1 Reply More

The Disk Usage (du) is one of the best command 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 large amount of disk space on your hard disks or other storage mediums.

du command Syntax.

du [options] [file or directory name]

Here, the meaning of square bracket is they are the optional items. If you use the du command without any option or any argument then it will list names of files and directories and they space consumption for the given directory tree. For instance:

[root@LinuxServer Desktop]# du /etc
4 /etc/openldap/cacerts
320 /etc/openldap/schema
80 /etc/openldap/slapd.d/cn=config/cn=schema
152 /etc/openldap/slapd.d/cn=config
160 /etc/openldap/slapd.d
492 /etc/openldap
… [output truncated]
68 /etc/kde
8 /etc/pcmcia
47248 /etc

Here, the space occupied by any directory is the space of all the files and subdirectories of that directory. The last line of the output gives the total space occupied for that directory.

Different options that can be used with du command.

Same like other Linux or Unix commands, du also has different number of options and some of them are most commonly used. Even these options can also change little bit based on the operating system or its version.

Example: 1

[root@LinuxServer Desktop]# du -h /etc
4.0K /etc/openldap/cacerts
320K /etc/openldap/schema
80K /etc/openldap/slapd.d/cn=config/cn=schema
152K /etc/openldap/slapd.d/cn=config
160K /etc/openldap/slapd.d
492K /etc/openldap
… [output truncated]
36K /etc/kde/kdm
68K /etc/kde
8.0K /etc/pcmcia
47M /etc

-h is one of the good options used with du command. Here, -h means human readable. This means it makes our output easier to read. It will display the output in kilobyte (K), megabyte (M) and gigabyte (G). So, we can say above command is used to display the size of all directories and subdirectories and also total size of our current directory formatted with appropriate K, M or G.

Example: 2

[root@LinuxServer Desktop]# du -s /etc
47248 /etc

This option summarizes only the total amount of total disk space occupied by that particular directory. This will also suppress the individual reports for all the files and subdirectories.

Example: 3

[root@LinuxServer Desktop]# du -a /etc | more
0 /etc/rc2.d
4 /etc/dracut.conf
8 /etc/sensors3.conf
4 /etc/nscd.conf
4 /etc/openldap/ldap.conf
….. [output truncated]
8 /etc/man.config
4 /etc/pcmcia/config.opts
8 /etc/pcmcia
47248 /etc

The option –a is to report the total disk space usage for every directory at every level. Even this option is also useful to report, the consumption of space for each file in the directory tree. This means in the above example, it would list the names and size of each directory and files inside /etc directory.

Example: 4

[root@LinuxServer etc]# du -h s* | more
24K sabayon/profiles
32K sabayon
24K samba
4.0K samba4
8.0K sane.d/dll.d
…[output truncated]

In this option, * means wildcard means it will try to match with any character. So, this options can be used to find the output of all those items whose names begin, ends or contain specific characters. In the above example, it will display the size of all the files and directories from the current directory whose names normally starts with S.

Example: 5

[root@LinuxServer etc]# du -hc s* | more
24K sabayon/profiles
32K sabayon
24K samba
4.0K samba4
…[output truncated]
4.0K systemtap/script.d
220K systemtap
20M total

We can add –c option with our du command which generally provides a grand total of files and directories listed. In case of above example, it will list the grand total of all files listed in the current directory starting with the name s. Another example is as below where we are getting the size of each gif files available in current directory with its grand total.

du -hc *.gif

Example: 6

[root@LinuxServer etc]# du --max-depth=1
492 ./openldap
4 ./dracut.conf.d
28 ./corosync
8 ./matchbox
32 ./avahi
….[output truncated]

This option instructs du command to list the subdirectories and its size to our desired depth level. For example, the above example lists the directories to first tier only in the current direcotyr 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.

Example: 7

[root@LinuxServer Desktop]# du -h –exclude=’*.obj’
[root@LinuxServer Desktop]# du -h –exclude=’*.jpg’

Exclude files that match PATTERN. The above examples do not count *.obj or *.jpg files.

Example: 8

[root@LinuxServer Desktop]# du -sk .[A-z]* *| sort -n

Display everything sorted by size.

[root@LinuxServer Desktop]# du -sk * | sort -nr | head -3

To find top 3 directories by size.

Filed Under : LINUX COMMANDS, LINUX HOWTO

Tagged With :

Free Linux Ebook to Download

Comments (1)

Trackback URL | Comments RSS Feed

  1. Nacho says:

    thanks, with this examples I just set a cron with the precise information I need every month. thanks a lot. greetings from Argentina.

Leave a Reply

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