#!/bin/bash # # prune_dir - prune directory by deleting files if we are low on space # DIR=/media/usb/ftp/data/domsod/FI9900P_00626E69B672/record CAPACITY_LIMIT=95 if [ "$DIR" == "" ] then echo "ERROR: directory not specified" exit 1 fi if ! cd $DIR then echo "ERROR: unable to chdir to directory '$DIR'" exit 2 fi if [ "$CAPACITY_LIMIT" == "" ] then CAPACITY_LIMIT=95 # default limit fi CAPACITY=$(df -k . | awk '{gsub("%",""); capacity=$5}; END {print capacity}') if [ $CAPACITY -gt $CAPACITY_LIMIT ] then # # Get list of files, oldest first. # Delete the oldest files until # we are below the limit. Just # delete regular files, ignore directories. # ls -rt $DIR | while read FILE do FILE="${DIR}/${FILE}" if [ -f $FILE ] then if rm -f $FILE then now=$(date) echo "${now}: Deleted $FILE" >> /var/log/ftpDeleter.log CAPACITY=$(df -k . | awk '{gsub("%",""); capacity=$5}; END {print capacity}') if [ $CAPACITY -le $CAPACITY_LIMIT ] then # we're below the limit, so stop deleting now=$(date) echo "${now}: Below limit, stopped deleting. Capacity: ${CAPACITY}" >> /var/log/ftpDeleter.log exit fi fi fi done else now=$(date) echo "${now}: Don't need to delete anything, capacity: ${CAPACITY}" fi