Daeblog
Archives ::

Tue, 08 Jun 2010

My good friend Haegin posted an article about finding free space at the commandline. I was asked if it contained anything too zsh-specific; my response was of course to rewrite the whole thing. After a couple of iterations, here's what we ended up with:

#!/bin/sh
2>/dev/null df | awk '
BEGIN {
  total=0; join=0;
}
NF < 3 {
  join=1
}
!join && NF >= 3 {
  total=total+$4;
}
join && NF >= 3 {
  total=total+$3;
  join=0
}
END {
  print int(total / 1024 / 1024) " MB"
}'

The main changes are to accommodate the output format from df better and to move most of the processing into awk. This should make the whole thing a little more portable to other shells in the sh family.

Comments
Add a comment:

Comments are closed for this story.

Trackbacks