Bash code to check free disk space

December 05, 2010

I have a script on a Linux server that downloads a bunch of very large files, and I want it to stop running if the disk is low on space. The way I found to do it is very verbose. Breaking it out line by line:

FREE=`df -H   # free space for all volumes
  | grep -E '^/dev/xvda3'    # the main disk
  | awk '{ print $4 }'    # the free space column
  | cut -d'G' -f1        # strip the G (for gigabytes)
  | awk -F '.' '{ print $1 }'`    # strip decimal parts
if [[ $FREE -lt 7 ]]; then
  echo "Less than 7GB free space left, stopping."
  exit 1

There’s gotta be a simpler way to do that… anyone know what that is?