Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Unix (http://www.go4expert.com/articles/unix/)
-   -   Find Disk Usage & Free Space Using du & df (http://www.go4expert.com/articles/disk-usage-free-space-using-du-df-t28698/)

pradeep 9Jul2012 18:59

Find Disk Usage & Free Space Using du & df
 
du is an abbreviation for (Disk Usage), this command calculates & returns the size of entire directory tree as well as of individual files. df is an abbreviation for (Disk Free), it's job is to calculate and return the amount of free space of a disk/partition.

These two commands report space usage & free space by filesystem block and not the actual size of the file, but the actual space that it uses on the disk.

The du & df commands are generally used by system administrators to monitor & alert storage's volumes to prevent program crashes due to disk space issues.

Disk Usage



All following examples are based on this directory:

Code: bash

[pradeep@castellano]$ ls -l
total 56
-rw-r--r-- 1 pradeep pg1555168 3355 2011-07-22 05:19 1311337262_info.png
-rwxr-xr-x 1 pradeep pg1555168 1227 2011-07-30 01:54 android.pl
-rw-rw-r-- 1 pradeep pg1555168 5076 2011-07-22 07:00 android_apps.html
-rw-r--r-- 1 pradeep pg1555168 3430 2011-07-29 06:34 apps.dat
drwxrwxr-x 2 pradeep pg1555168 4096 2012-07-09 04:58 images
-rw-r--r-- 1 pradeep pg1555168 4599 2011-07-22 04:54 info_icon.png
-rw-r--r-- 1 pradeep pg1555168 3374 2011-07-29 05:14 jquery.lightbox_me.js
-rw-rw-r-- 1 pradeep pg1555168  101 2009-06-25 02:03 llsh.gif
-rw-r--r-- 1 pradeep pg1555168 1742 2011-07-30 01:57 main.tpl.html
-rwxrwxr-x 1 pradeep pg1555168 3741 2012-04-30 03:31 ssearch.pl
-rw-rw-r-- 1 pradeep pg1555168 1830 2011-07-29 07:03 style.css
-rw-rw-r-- 1 pradeep pg1555168  117 2009-06-25 02:03 teaser.gif


Now, we'll see the most basic usage of du, we'll use the -a option to report usage on a per-file basis, instead of just displaying the total size of a directory.

Code: bash

[pradeep@castellano]$ du
16      ./images
76      .
[pradeep@castellano]$ du -a
4       ./1311337262_info.png
4       ./android.pl
8       ./android_apps.html
4       ./ssearch.pl
4       ./teaser.gif
4       ./main.tpl.html
4       ./style.css
4       ./llsh.gif
8       ./info_icon.png
4       ./.htaccess
4       ./apps.dat
4       ./images/1311337262_info.png
4       ./images/teaser.gif
4       ./images/llsh.gif
16      ./images
4       ./jquery.lightbox_me.js
76      .


The size is displayed in bytes, but we might want to display the sizes in human readable format.

Code: bash

[pradeep@castellano]$ du -ah
4.0K    ./1311337262_info.png
4.0K    ./android.pl
8.0K    ./android_apps.html
4.0K    ./ssearch.pl
4.0K    ./teaser.gif
4.0K    ./main.tpl.html
4.0K    ./style.css
4.0K    ./llsh.gif
8.0K    ./info_icon.png
4.0K    ./.htaccess
4.0K    ./apps.dat
4.0K    ./images/1311337262_info.png
4.0K    ./images/teaser.gif
4.0K    ./images/llsh.gif
16K     ./images
4.0K    ./jquery.lightbox_me.js
76K     .


There are 2 more important options, s (summarize or suppress) & c options. See the examples below.

Code: bash

[pradeep@castellano]$ du -hs
76K     .
[pradeep@castellano]$ du -ahc *.png
4.0K    1311337262_info.png
8.0K    info_icon.png
12K     total


The s only shows the total size, and the c option show the total in the end.

We might at times want to exclude some files, or some types of files from the calculation.

Code: bash

[pradeep@castellano]$ du -ahc --exclude='*.png'
4.0K    ./android.pl
8.0K    ./android_apps.html
4.0K    ./ssearch.pl
4.0K    ./teaser.gif
4.0K    ./main.tpl.html
4.0K    ./style.css
4.0K    ./llsh.gif
4.0K    ./.htaccess
4.0K    ./apps.dat
4.0K    ./images/teaser.gif
4.0K    ./images/llsh.gif
12K     ./images
4.0K    ./jquery.lightbox_me.js
60K     .
60K     total


Disk Free



The most common use of df is to display summary of all partitions in the system.

Code: bash

[pradeep@castellano]$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              60G   43G   14G  76% /
tmpfs                 7.9G     0  7.9G   0% /lib/init/rw
tmpfs                 7.9G  5.6M  7.9G   1% /dev/shm
/dev/sda2             3.8G  168M  3.6G   5% /tmp
/dev/sda6              92M  5.7M   81M   7% /var/spool/cron/crontabs
/dev/sdb1             3.6T  3.5T   92G  98% /home


You may also also use df checking free space on individual partitions, either by specifying the device or the directory.

Code: bash

[pradeep@castellano]$ df /
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1             62009668  44463052  14396676  76% /
[pradeep@castellano]$ df /tmp/
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda2              3937252    171544   3765708   5% /tmp
[pradeep@castellano]$ df /dev/sdb1
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sdb1            3758537404 3664210776  94326628  98% /home


Caveats



At times it might happen that df & du might report data that is not matching, this usually occurs due to open file descriptors, you can check these using the lsof command.

dearvivekkumar 10Jul2012 12:01

Re: Find Disk Usage & Free Space Using du & df
 
Hi,

Code:

du -ahc --exclude='*.png'
If I want to excludes more than one type (for example *.png and *.c")
Can you please let me know how will I achieve that?

Thanks

pradeep 10Jul2012 14:43

Re: Find Disk Usage & Free Space Using du & df
 
Code:

du -ahc --exclude='*.png' --exclude='*.pl'

dearvivekkumar 10Jul2012 15:01

Re: Find Disk Usage & Free Space Using du & df
 
Thanks Pradeep!


All times are GMT +5.5. The time now is 06:16.