Thursday, May 31, 2007

Disk usage in linux

A quick summary of the used/available space for each partition of the system can be retrieved with the df command:

apapad@Zzzzz:~> df
Filesystem      1K-blocks      Used Available Use% Mounted on
/dev/hda7        15172120   4803284   9598120  34% /
varrun             517208        88    517120   1% /var/run
varlock            517208         0    517208   0% /var/lock
procbususb         517208       120    517088   1% /proc/bus/usb
udev               517208       120    517088   1% /dev
devshm             517208         0    517208   0% /dev/shm
/dev/hda8        12507868   9081380   2791120  77% /home
/dev/hda2        28653984  23529856   5124128  83% /media/windows/D
/dev/hdc          4338192   4338192         0 100% /media/cdrom0
A more comprehensive view can be obtained by using the -h switch (which stands for human readable).
apapad@Zzzzz:~> df -h
Filesystem         Size  Used Avail Use% Mounted on
/dev/hda7           15G  4.6G  9.2G  34% /
varrun             506M   88K  505M   1% /var/run
varlock            506M     0  506M   0% /var/lock
procbususb         506M  120K  505M   1% /proc/bus/usb
udev               506M  120K  505M   1% /dev
devshm             506M     0  506M   0% /dev/shm
/dev/hda8           12G  8.7G  2.7G  77% /home
/dev/hda2           28G   23G  4.9G  83% /media/windows/D
/dev/hdc           4.2G  4.2G     0 100% /media/cdrom0

This still doesn't give enough information. We might wish to know which folders are taking up the most space. This can be done with the du command:

apapad@Zzzzz:~> du -sk /home/apapad/* | sort -rn
2260396 /home/apapad/Desktop
1413532 /home/apapad/torrent
106832  /home/apapad/iexplore
10288   /home/apapad/workspace
6532    /home/apapad/mypics
6300    /home/apapad/mydocs
2236    /home/apapad/bin
880     /home/apapad/archive
428     /home/apapad/wireless
304     /home/apapad/resume
80      /home/apapad/gl2gr
12      /home/apapad/games
8       /home/apapad/dvdrip-data
4       /home/apapad/wine-src
4       /home/apapad/downloads
0       /home/apapad/Examples

The -k option tells du to use 1K-blocks. (Actually this is the default value, so it is not really required). The -s option gives a total for each directory. Piping the output of du to sort gives a nice result: -n tells sort that it should sort numbers rather than strings and -n returns the results in reverse order.

du also has a -h option for human readable results, but this wouldn't work well with sort...

No comments: