Apr 262011
 

hard-diskAs system administrator , but also as common user on my PC, one of the more common problem is when a file system got filled up at up at 100% of its size.
So, in this article we’ll see 2 commands that can help us in keeping under control or check the space used in every filesystem and in his directory.

df : report file system disk space usage

du: estimate file space usage


DF

Basic usage

df [Option…] [FILE_NAME…]

description

If [FILE_NAME…] is not given df displays the following information for each file system that was activated with mount: total space, used space, free space and use in %.

if it’s given [FILE_NAME…] the same information is given for the filesystem containing the specified files .

Options:

The most used options for df are -k that show the output values in Kilobytes (this is usually teh default), -h that print sizes in human readable format (e.g., 1K 234M 2G) and -i that give information about inodes

Examples:

#df
 
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/root_vg-root_lv
                       4128448    400936   3517800  11% /
/dev/mapper/root_vg-home_lv
                        516040     17568    472260   4% /home
/dev/mapper/root_vg-tmp_lv
                       4128448    164316   3754420   5% /tmp
/dev/mapper/root_vg-usr_lv
                       8256952   1240372   6597152  16% /usr
/dev/mapper/root_vg-var_lv
                       4128448    490632   3428104  13% /var
/dev/mapper/root_vg-var_log_lv
                       8256952    320836   7516740   5% /var/log

Same machine but with the -h option:

#df -h
 
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/root_vg-root_lv
                      4.0G  392M  3.4G  11% /
/dev/mapper/root_vg-home_lv
                      504M   18M  462M   4% /home
/dev/mapper/root_vg-tmp_lv
                      4.0G  161M  3.6G   5% /tmp
/dev/mapper/root_vg-usr_lv
                      7.9G  1.2G  6.3G  16% /usr
/dev/mapper/root_vg-var_lv
                      4.0G  480M  3.3G  13% /var
/dev/mapper/root_vg-var_log_lv
                      7.9G  314M  7.2G   5% /var/log

Same machine but with the -i option:

#df -i
 
Filesystem            Inodes   IUsed   IFree IUse% Mounted on
/dev/mapper/root_vg-root_lv
                        256K    7.0K    250K    3% /
/dev/mapper/root_vg-home_lv
                         32K      55     32K    1% /home
/dev/mapper/root_vg-tmp_lv
                        256K      27    256K    1% /tmp
/dev/mapper/root_vg-usr_lv
                        512K     51K    462K   10% /usr
/dev/mapper/root_vg-var_lv
                        256K    4.0K    253K    2% /var
/dev/mapper/root_vg-var_log_lv
                        512K     473    512K    1% /var/log

An example a bit more interesting, we do a df, then we use the ouput to see when is the next check of the filesystem (it works on ext3 /4):

#df  | awk '{print $1}' | grep dev/ | xargs -i tune2fs -l {}| egrep "mounted|Next"
 
Last mounted on:          /
Next check after:         Sat Oct  1 19:05:57 2011
Last mounted on:          /home
Next check after:         Sat Oct  1 12:33:16 2011
Last mounted on:          /tmp
Next check after:         Sat Oct  1 19:06:00 2011
Last mounted on:          /usr
Next check after:         Sat Oct  1 19:06:04 2011
Last mounted on:          /var
Next check after:         Sat Oct  1 19:06:07 2011
Last mounted on:          /var/log
Next check after:         Sat Oct  1 12:33:22 2011


DU

Basic usage

du [Option…] [FILE_NAME…]

description

The du command serves to return the size of either an entire file system or a folder, file, etc..
du command returns the size of each file and directory that is within that directory. If [FILE_NAME…] is not given du will print the disk usage of every file from the current directory and all subdir.

Options

You can use the option -a , which tells the du command to return the size of individual files and directories, while -s display only a total for each argument.
Another important parameter is -h which makes the output, of the du command, more friendly to the human eye such as 18M instead of displaying 17,532 (the same number shown in bytes without rounding).
Another recommended setting is -c which returns the total size of all files.

Examples
Show the disk usage of /var

# du -hs /var/
481M	/var/

Show the disk usage of every directory in /var and print the total:

# du -hsc /var/*
5.3M	/var/backups
188M	/var/cache
112M	/var/lib
4.0K	/var/local
16K	/var/lock
177M	/var/log
16K	/var/lost+found
12K	/var/mail
4.0K	/var/opt
100K	/var/run
604K	/var/spool
4.0K	/var/tmp
120K	/var/www
481M	total

Get the 10 biggest files/folders for the /var/ directory

# du -s /var/* | sort -nr |head
191676	/var/cache
180760	/var/log
113712	/var/lib
5332	/var/backups
604	/var/spool
120	/var/www
100	/var/run
16	/var/lost+found
16	/var/lock
12	/var/mail

Conclusions

With these two simple commands you can control the growth of your file system and quickly see if the space is exausted, or if there are problems on inode and which directory is consuming all of your disk.

Why command df and du reports different output ?

Short answer: this is due to open files and how they are counted by the 2 commands.
Long answer: Check this post

Popular Posts:

Flattr this!

  2 Responses to “Check your disk usage with df and du”

  1. Su gnome è possibile vedere l’utilizzo in lettura e scrittura momentaneo del disco tramite applet System Monitor. Quale comando posso usare per avere tale informazione tramite linea di comando?

    Grazie

    • Ciao Stefano,
      Puoi usare il comando iostat, normalmente fornsice in output il numero di blocchi scritti/letti al secondo ma puoi usare -k (kilobytes) o -m (megabytes).

      Come parametro prende l’intervallo in secondi di campionamento, quindi un:

      iostat 2 -m

      Ti da un campionamento a 2 secondi e stampa i valori in MB.

      Ciao

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

*