harrygg
3/11/2017 - 9:53 AM

Runs every day and checks if disk space is less than 5 GB, then deletes some files until free space is bigger than 5 GB

Runs every day and checks if disk space is less than 5 GB, then deletes some files until free space is bigger than 5 GB

#!/bin/sh
# set -x
# Shell script to monitor or watch the disk space


cd /home/camera/FI9831P_00626E654229/record

free=`df -k --output=avail "$PWD" | tail -n1`
treshold=5000000 #5GB Free space needed

if [ $free -lt $treshold ]; then
  echo "less than 10GBs free! Cleaning some files"

  while [ $free -lt $treshold ]
    do
      oldest_file="$(ls -lt | grep -v '^d' | tail -1 | awk '{print $NF}')"
      file_size=`stat --printf="%s" $oldest_file`
      echo Removing $oldest_file with size $file_size bytes.
      rm $oldest_file
      # sleep 1
      free=`df -k --output=avail "$PWD" | tail -n1`
      echo Used space after removal $free
    done
else
  echo $free are in available
fi;