Pages

03 February 2011

Using awk to show nice totals for 'df' command

The following snippet of code is quite a handy way to show how much space is used by specific filesystems. For example I have three filesystems of various mp3s, and I want to see how much space in total is used by my mp3s across all three filesystems.

Save the following code in it's own file, I called mine filesystem.awk and kept it in my home directory.
{
   if ( $1 ~ /.*Mp3s$/ ) {
      TOTAL += $2
      USED  += $3
      FREE  += $4
      printf "%-45s %11s %10s %10s %8s %s\n",$1,$2,$3,$4,$5,$6
   }
   if ( $1 ~ /^Filesystem$/ ) {
      printf "%-45s %11s %10s %10s %8s %s\n",$1,$2,$3,$4,$5,$6
   }
}
END {
  TOTAL = TOTAL / 1024 / 1024
  USED  = USED  / 1024 / 1024
  FREE  = FREE  / 1024 / 1024
  USEDP = (USED / TOTAL) * 100
  FREEP = (FREE / TOTAL) * 100
  printf "\n%12s %18s %18s\n", "Total Gb", "Used", "Free"
  printf "%12s %18s %18s\n", "---------", "-----", "-----"
  printf "%12.2f %12.2f (%02d%%) %12.2f (%02d%%)\n", TOTAL, USED, USEDP, FREE, FREEP
}


Now on the command line I run the df command using -kP and pass it's output on to awk and use the filesystem.awk file to control what I want to see and do some arithmetic. Here is an example of what is produced before and after using awk.

Before:
clive@dogmatix > df -kP
Filesystem         1024-blocks      Used Available Capacity Mounted on

/dev/mapper/vgSystem-lvSystem  96118540   8067724  83168180       9% /
none                   1973140       340   1972800       1% /dev
none                   1978060      1344   1976716       1% /dev/shm
none                   1978060       564   1977496       1% /var/run
none                   1978060         0   1978060       0% /var/lock
none                   1978060         0   1978060       0% /lib/init/rw
none                  96118540   8067724  83168180       9% /var/lib/ureadahead/debugfs
/dev/mapper/vgDataVol01-lvCheckMp3s 258030980  39472592 218558388      16% /data/check_mp3s
/dev/mapper/vgDataVol01-lvDocuments 206424760 104589812 101834948      51% /data/documents
/dev/mapper/vgDataVol01-lvGoodMp3s 258030980 143166632 114864348      56% /data/good_mp3s
/dev/mapper/vgDataVol01-lvReorganisedMp3s 206424760  98623828 107800932      48% /data/reorganised_mp3s
/dev/mapper/vgDataVol01-lvSoftware 361243236 259153696 102089540      72% /data/software
/dev/sda1               186663     88645     96091      48% /boot


After:
clive@dogmatix > df -kP | awk -f filesystem.awk 
Filesystem                                    1024-blocks       Used  Available Capacity Mounted
/dev/mapper/vgDataVol01-lvCheckMp3s             258030980   39472592  218558388      16% /data/check_mp3s
/dev/mapper/vgDataVol01-lvGoodMp3s              258030980  143166632  114864348      56% /data/good_mp3s
/dev/mapper/vgDataVol01-lvReorganisedMp3s       206424760   98623828  107800932      48% /data/reorganised_mp3s

    Total Gb               Used               Free
   ---------              -----              -----
      689.02       268.23 (38%)       420.78 (61%)



If anyone wants an explanation of what each line/section of the filesystem.awk file does please let me know and I will edit this post to explain it in more depth.

No comments:

Post a Comment