Friday, July 15, 2011

DF and DU in UNIX

Prepared by ABHIJITH B A, ABDUL KAREEM M A
Hi Everyone..!!

In this section we are going to discuss about two important commands of UNIX which is related to the disk space usage statistics. The commands are du and df which are explained with some example.
The du command is used to see the amount of space taken by various files or directories in this system. So whenever you feel that your system runs short of memory, just use du command to see which file taking too much space. Here we have given certain example by using various arguments or options in du command.

Df command which is closely related to du, used to identify the space available in various file system. It also shows about the total space, usage, and mount point etc of file system. In this document df is explained with some interesting example. We also showed how df gives the output when we have additional devices like usb, floppy disk etc attached to the system.

One thing we want to point here is that the outputs of these commands are verified and depends on the system that we were used. We hope this document will help you to understand the df and du command easily. Let’s start..!!

THE DF COMMAND IN UNIX

The df command reports file system disk space usage. df displays the amount of disk space available on the file system containing each file name argument. If no file name is given, the space available on all currently mounted file systems is shown.

If an argument is the file name of a disk device node containing a mounted file system, df shows the space available on that file system rather than on the file system containing the device node (which is always the root file system).

In this section we will see the usage of df command in Linux using its various arguments. In the examples, df is first called with no arguments. This default action is to display used and free file space in blocks.
[kareem@localhost ~]$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda5 10557728 3099888 7350600 30% /
tmpfs 1012 260 1012220 1% /dev/shm

Totally confused with the output..?

Let see what these columns indicates, the first column show the name of the disk partition as it appears in the /dev directory. Subsequent columns show total space, blocks allocated and blocks available. The use column indicates the amount used as a percentage of total file system capacity. The final column shows the mount point of the file system. This is the directory where the file system is mounted within the file system tree. Note that the root partition will always show a mount point of /. Other file systems can be mounted in any directory of a previously mounted file system. so in this output /dev/sda5 is the root partition.

But the output shown by df is not in human understandable format. Let see the usage -h or --human-readable option in df.
[kareem@localhost ~]$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda5 11G 3.0G 7.1G 30% /
tmpfs 989M 260K 989M 1% /dev/shm


Here it shows the output in a format which can be understood easily. It displays the size in G(gigabyte)format. Other format may be kilobyte, megabyte etc. Now you may interested in viewing disk space usage statistics of all file system mounted in the system. Argument -a which shows all the dummy file system in the system. Note: here after all the df command example will have -h argument in order display output in human readable format.
[kareem@localhost ~]$ df -a -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda5 11G 3.0G 7.1G 30% /
proc 0 0 0 - /proc
sysfs 0 0 0 - /sys
devpts 0 0 0 - /dev/pts
tmpfs 989M 1.1M 988M 1% /dev/shm
none 0 0 0 - /proc/sys/fs/binfmt_misc
gvfs-fuse-daemon 0 0 0 - /home/kareem/.gvfs

Now you may be interested to know the total free space available in the root file system. The option --total used for this purpose.

[kareem@localhost ~]$ df --total -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda5 11G 3.0G 7.1G 30% /
tmpfs 989M 1.1M 988M 1% /dev/shm
total 12G 3.0G 8.0G 28%

Here tmpfs is another file system which is mounted on the root file system. Total shows the grand total disk space, usage and available of the root file system.

If you want to know the file system type you can use -T or --print-type argument in df command.
The types of the file system may be ext3, ext4 or NTFS (in windows) etc.
[kareem@localhost ~]$ df -h -T
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda5 ext4 11G 3.0G 7.1G 30% /
tmpfs tmpfs 989M 1.1M 988M 1% /dev/shm


Here root file system of type ext4 and inner file system tmpfs which is said to be temporary file system.
Note : Here i installed a pen drive in to the system. So let see the file system type by using -H options.

[kareem@localhost ~]$ df -h -T
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda5 ext4 11G 3.0G 7.1G 30% /
tmpfs tmpfs 989M 1.1M 988M 1% /dev/shm
/dev/sdc1 vfat 1.8G 1.8G 33M 99% /media/WALKMAN

here the last line shows the usb(WALKMAN) details. We can note that the usb is almost full and its file system type is vfat.



* * * * * * * * * * * * *

THE DU COMMAND IN UNIX


The du command shows the disk space used by the files and directories in a directory. Summarize disk usage of each FILE, recursively for directories.

ADVANTAGE :

This common will help in tracking down "space hogs", SPACE HOGS-directories and files that consume large or excessive amounts of space on a hard disk drive or other storage media.

SYNTAX :

du [OPTION]... [FILE]...

USAGE :

1. du - this command gives us the space usage in the current directory.
[abhijith@localhost shellprog]$ du
8 ./example
28 .

here example is a directory ..n the . next to 28 are files instead shellprog folder.

2. du -h - displays the file usage in human readable form.
[abhijith@localhost shellprog]$ du -h
8.0K ./example
28K

3. du -c - gives the grand total of the space used.
[abhijith@localhost shellprog]$ du -c
8 ./example
28 .
28 total


-h and -c can be combined " du -hc "
[abhijith@localhost shellprog]$ du -hc
8.0K ./example
28K .
28K total

4. du -s - gives only the summary of space used.
[abhijith@localhost shellprog]$ du –s -h
28k

5. du * - this gives list of files in the parent directory.
[abhijith@localhost shellprog]$ du *
4 date
4 evenodd
8 example
4 pictures
0 pictures~
4 testin
0 testin ~


6. du [letter]* -this command is used to display only those items whose names begin with, contain or end with certain characters or sequences of characters.
[abhijith@localhost shellprog]$ du e*
4 evenodd
8 example


7. du -a - display an entry for each file (and not directory) contained in the current directory
[abhijith@localhost shellprog]$ du -a
4 ./date
4 ./example/hello
0 ./example/hello~
8 ./example
4 ./pictures

8. du --time - gives the time and date of creation of each file in the current directory
[abhijith@localhost shellprog]$ du --time
8 2011-06-26 07:54 ./example
28 2011-06-26 07:54

du commands with filter :

du -ah | grep WORD- This displays a list of the names and sizes of directories and files in the current directory that contain the word "WORD"
[abhijith@localhost shellprog]$ du -ah |grep picture
4.0K ./pictures
0 ./pictures~

du -ah | greap M - directories and files in a directory tree that are consuming large amounts of disk space is to use grep to search for all the lines that contain the upper case letter M (i.e., for megabytes) or G (for gigabytes).The only problem with this approach is that it will also select directories and files that contain an upper case M or G in their names even if the file size is not measured in megabytes or gigabytes.
* * * * * * * * * * * * * *

No comments:

Post a Comment