Find Disk Usage & Free Space Using du & df

Discussion in 'Unix' started by pradeep, Jul 9, 2012.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    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:
    [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:
    [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:
    [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:
    [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:
    [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:
    [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:
    [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.
     
  2. dearvivekkumar

    dearvivekkumar New Member

    Joined:
    Feb 21, 2012
    Messages:
    29
    Likes Received:
    5
    Trophy Points:
    0
    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
     
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Code:
    du -ahc --exclude='*.png' --exclude='*.pl'
     
    dearvivekkumar likes this.
  4. dearvivekkumar

    dearvivekkumar New Member

    Joined:
    Feb 21, 2012
    Messages:
    29
    Likes Received:
    5
    Trophy Points:
    0
    Thanks Pradeep!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice