Code: Select all
#!/bin/bash
FILESYSTEM=/dev/sdc1
MAXCAPACITY=90
#deprecated
#DIRTODEL=/videodir/
LOGFILE=/videodir/log
#delete oldest two folders
FILESTODEL=2
#array of all folders to delete old folders from.
FOLDERS=( "/videodir/monitor1/"
"/videodir/monitor2/" )
for folderstodel in "${FOLDERS[@]}"
do
#check if devsdc1 capacity from df is gt / greater than of maxcapacityallowed
if [ $(df -P $folderstodel | awk '{gsub("%",""); capacity=$5}; END { print capacity }') -gt $MAXCAPACITY ]
then
# cd to dir, list in order to time modified, list last 2, send that list to rm (delete newlines)
# added var for tail, and rf for rm, since i am deleting directories
cd $folderstodel && ls -t | tail -n $FILESTODEL | xargs -d '\n' rm -rf ;
echo 'deleted 2 oldest folders at the below time' >> $LOGFILE;
date >> $LOGFILE;
# not passing | to ls but using && as we are not passing return values or stdout
else
echo 'not full yet at the below time' >> $LOGFILE;
date >> $LOGFILE;
fi
done