du command in linux

Satish Tiwary's picture
du command in linux

du command line utility  mostly used to display or find information related to hard disk and disk usage.

du utility Explanation:-

1.du command is used to see the disk usage.
At present i am inside the /var/ftp/ Directory and i want to see the complete Disk Usage Statistics of this
Directory and it's sub-directories.

[root@satish ftp]# du
10584   ./Server/repodata
2638388 ./Server
2638392 .

 

2.Now to see the above statistics in human readable format use option -h with du

 

[root@satish ftp]# du -h
11M     ./Server/repodata
2.6G    ./Server
2.6G    .

 

3.Now if you want to see the disk usage statistics of any particular directory along with it's sub-directories you can use the below command.

[root@satish ~]# du /tmp/
4       /tmp/ssh-wXKGzB3421
4       /tmp/virtual-root.bsjXxG
4       /tmp/keyring-ulhVbt
4       /tmp/gconfd-satish
28      /tmp/alexa/toolbar@alexa.com/2.17/toolbar
32      /tmp/alexa/toolbar@alexa.com/2.17
36      /tmp/alexa/toolbar@alexa.com
40      /tmp/alexa
4       /tmp/ksocket-root
4       /tmp/virtual-root.NcMyoT
4       /tmp/virtual-root.L4MSYT
8       /tmp/gconfd-root/lock
16      /tmp/gconfd-root
4       /tmp/virtual-root.qgqHyO
4       /tmp/kde-root
4       /tmp/.ICE-unix
4       /tmp/.font-unix
4       /tmp/keyring-5yYf0e
4       /tmp/.X11-unix
4       /tmp/keyring-bbxuTi
8       /tmp/orbit-root
4       /tmp/keyring-jzXi43
5780    /tmp/

4.To see the above command in human readable format use below command.

[root@satish ~]# du -h /tmp/
4.0K    /tmp/ssh-wXKGzB3421
4.0K    /tmp/virtual-root.bsjXxG

4.0K    /tmp/keyring-ulhVbt
4.0K    /tmp/gconfd-satish
28K     /tmp/alexa/toolbar@alexa.com/2.17/toolbar
32K     /tmp/alexa/toolbar@alexa.com/2.17
36K     /tmp/alexa/toolbar@alexa.com
40K     /tmp/alexa
4.0K    /tmp/ksocket-root
4.0K    /tmp/virtual-root.NcMyoT
4.0K    /tmp/virtual-root.L4MSYT
8.0K    /tmp/gconfd-root/lock
16K     /tmp/gconfd-root
4.0K    /tmp/virtual-root.qgqHyO
4.0K    /tmp/kde-root
4.0K    /tmp/.ICE-unix
4.0K    /tmp/.font-unix
4.0K    /tmp/keyring-5yYf0e
4.0K    /tmp/.X11-unix
4.0K    /tmp/keyring-bbxuTi
8.0K    /tmp/orbit-root
4.0K    /tmp/keyring-jzXi43
5.7M    /tmp/

Note: -h  option with “du” command provides results in “Human Readable Format“.

What it means that now you can see sizes in Bytes, Kilobytes, Megabytes, Gigabytes etc.

 

5.Use "-s" option to see the Grand Total size of any directory.

[root@satish ~]# du -hs /var/ftp/
2.6G    /var/ftp/

6. Using "-ah" option we can view the disk Usage information of all file and folders in a directory.

 

[root@satish ~]# du -ah /home/u1/
4.0K    /home/u1/.bashrc
4.0K    /home/u1/.emacs
101M    /home/u1/a1
4.0K    /home/u1/.mozilla/extensions
4.0K    /home/u1/.mozilla/plugins
12K     /home/u1/.mozilla
4.0K    /home/u1/new
4.0K    /home/u1/vivek.sh
4.0K    /home/u1/.bash_logout
4.0K    /home/u1/.bash_profile
4.0K    /home/u1/.kde/Autostart/.directory
8.0K    /home/u1/.kde/Autostart
12K     /home/u1/.kde
101M    /home/u1/

 

 

7.Using "kh" option we can see the disk usage of a directory and it's sub-directories in kilobytes blocks.

[root@satish ~]# du -hk /home/satish/
4       /home/satish/Desktop/win7
1022720 /home/satish/Desktop
4       /home/satish/.eggcups
24      /home/satish/mail/.imap/Trash
28      /home/satish/mail/.imap/Sent
24      /home/satish/mail/.imap/INBOX
24      /home/satish/mail/.imap/Drafts
104     /home/satish/mail/.imap
116     /home/satish/mail

 

 

8.Use -mh option to see disk usage information in MB

 

[root@satish ~]# du -mh /home/slashroot/
8.0K    /home/slashroot/.mozilla/extensions
8.0K    /home/slashroot/.mozilla/plugins
24K     /home/slashroot/.mozilla
64K     /home/slashroot/

 

9.Use "-ch"option to see grand total disk usage at the last line.

[root@satish ~]# du -ch /tmp/
4.0K    /tmp/ssh-wXKGzB3421
4.0K    /tmp/virtual-root.bsjXxG
4.0K    /tmp/keyring-ulhVbt
4.0K    /tmp/gconfd-satish
28K     /tmp/alexa/toolbar@alexa.com/2.17/toolbar
32K     /tmp/alexa/toolbar@alexa.com/2.17
36K     /tmp/alexa/toolbar@alexa.com
40K     /tmp/alexa
4.0K    /tmp/ksocket-root
4.0K    /tmp/virtual-root.NcMyoT
4.0K    /tmp/virtual-root.L4MSYT
8.0K    /tmp/gconfd-root/lock
16K     /tmp/gconfd-root
4.0K    /tmp/virtual-root.qgqHyO
4.0K    /tmp/kde-root
4.0K    /tmp/.ICE-unix
4.0K    /tmp/.font-unix
4.0K    /tmp/keyring-5yYf0e
4.0K    /tmp/.X11-unix
4.0K    /tmp/keyring-bbxuTi
8.0K    /tmp/orbit-root
4.0K    /tmp/keyring-jzXi43
5.7M    /tmp/
5.7M    total

 

 

10. To get summary for files and Directories

#du -a

 

11.To see the disk usage for a particular file

#du -h filename

[root@satish ~]# du -h eclipse-SDK-4.2.1-linux-gtk.tar.gz
928K    eclipse-SDK-4.2.1-linux-gtk.tar.gz

12.To see the disk usage of multiple files at a time use

#df -h fle1 file2

[root@satish ~]# du -h index.php install.log
4.0K    index.php
36K     install.log

 

 

[root@satish ~]# du -h index.php install.log iplist link.php file.img
4.0K    index.php
36K     install.log
4.0K    iplist
9.8M    file.img

13. Using "--time" option we can see the disk usage statistics on the basis of modification time.

[root@satish ~]# du --time -ha /etc/httpd/
4.0K    2009-07-15 18:34        /etc/httpd/conf.d/proxy_ajp.conf
4.0K    2009-07-15 18:34        /etc/httpd/conf.d/manual.conf
4.0K    2013-01-09 23:23        /etc/httpd/conf.d/munin.conf
4.0K    2009-05-21 20:56        /etc/httpd/conf.d/squirrelmail.conf

 

 

14 using "--exclude" option we can exclude a particular type of file format from disk usage statistics.

# du -ah --exclude="*.rpm" /var/ftp/Server/

 

[root@satish Server]# du -ah --exclude="*.rpm" /var/ftp/Server/
556K    /var/ftp/Server/TRANS.TBL
2.4M    /var/ftp/Server/repodata/filelists.xml.gz
1008K   /var/ftp/Server/repodata/comps-rhel5-server-core.xml
740K    /var/ftp/Server/repodata/primary.xml.gz
6.4M    /var/ftp/Server/repodata/other.xml.gz
4.0K    /var/ftp/Server/repodata/TRANS.TBL
4.0K    /var/ftp/Server/repodata/repomd.xml
11M     /var/ftp/Server/repodata
12M     /var/ftp/Server/

If you want to see what will be the output if we not use --exclude=".rpm" option here see below(the output is much longer, i have suppressed it).

[root@satish Server]# du -ah /var/ftp/Server/
212K    /var/ftp/Server/psgml-1.2.5-4.3.noarch.rpm
108K    /var/ftp/Server/zlib-devel-1.2.3-3.i386.rpm
28K     /var/ftp/Server/libibmad-static-1.2.3-1.el5.i386.rpm
60K     /var/ftp/Server/ftp-0.17-35.el5.i386.rpm
1.6M    /var/ftp/Server/openmotif-2.3.1-2.el5.i386.rpm
612K    /var/ftp/Server/setools-gui-3.0-3.el5.i386.rpm
180K    /var/ftp/Server/netpbm-devel-10.35.58-8.el5.i386.rpm
16K     /var/ftp/Server/xorg-x11-drv-keyboard-1.1.0-3.i386.rpm

 

15.Find out top 20 largest File or directory.

[root@satish ~]# du -sk /var/log/* | sort -r -n | head -20
1108    /var/log/messages.3
996     /var/log/maillog.3
536     /var/log/wtmp
404     /var/log/anaconda.log
400     /var/log/audit
280     /var/log/lynis.log
224     /var/log/maillog
156     /var/log/maillog.1
152     /var/log/maillog.2
144     /var/log/httpd
92      /var/log/sa
88      /var/log/munin
80      /var/log/cups
68      /var/log/scrollkeeper.log
56      /var/log/lynis-report.dat
48      /var/log/prelink
48      /var/log/messages.2
48      /var/log/lastlog
44      /var/log/messages.1
40      /var/log/Xorg.0.log.old

 

Rate this article: 
Average: 3.5 (27 votes)

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.