lørdag 20. mars 2010

Simple bash script to recursively delete files and folders based on date and freespace

This simple script will automatically free diskspace if the diskspace in the targeted area (specified by $FREEDIR) exceeds the specified limit ($LIMIT). It  will then proceed with recursively removing the oldest file/folder until the used diskpace no longer exceeds the limit.

#!/bin/bash

#CONFIG####################################################

#the dir u wish to watch
FREEDIR="/home/rtorrent/download"

#the limit in percent
LIMIT=90

###########################################################

while [ true ]; do
  USAGE=`df $FREEDIR | tail -1 | awk {'print $4'}`
  USAGE=${USAGE:0:${#USAGE}-1}

  if [ $USAGE -gt $LIMIT ]; then
    TODELETE=$FREEDIR/`ls -tA $FREEDIR | tail -1`
    echo "Removing: $TODELETE"
    rm -rf "$TODELETE"
  else
    break
  fi
done

exit 0

1 kommentar: