Delete files older than "x" if directory size is greater than "y"

I wrote a script to delete files which are older than "x" days, if the size of the directory is greater than "y"

#!/bin/bash

du -hs $1
while read SIZE ENTRY
do

if [ ${SIZE} -gt 200 ];
then

find $1 -mtime +$2 -exec rm -f {} \;
echo "Files older than $2 days deleted"
else
echo "free Space available"
fi
done

I am not sure if everthing is correct it took me some tome to write it, and the only thing is to trigger it only if directory size is greater than.
pls Help

You have to crontab the script to check direcotry size after some interval.

Im no bash expert... (so use with caution as I cant test for you... based on my sh/ksh knowledge

#!/bin/bash
# you could add some comments on how it should work...
# Its always handy to give some more information and
# with a program that rm, test all parameters are set 
# and are coherent or come out with a short help/syntax
# to display
du -hs $1          # I suppose you mean $1=the input directory?
SIZE=$(du -sk $1)
# where do you get the size form?
while read SIZE ENTRY   # either you echo "enter size..."
                                 # and the read SIZE
do
                                 # or you accpet SIZE as 2nd argument $2
if [ ${SIZE} -gt 200 ];   # which gives you 
                         # if [ $(SIZE) -gt "$2" ] 
then

find $1 -mtime +$2 -exec rm -f {} \;
echo "Files older than $2 days deleted"
else
echo "free Space available"
fi
done

---------- Post updated at 18:17 ---------- Previous update was at 18:14 ----------

Your $2 would become $3...

---------- Post updated at 18:22 ---------- Previous update was at 18:17 ----------

About testing your arguments, this is a start:

#Test  params
if [ $# -eq 0 ];
then
        echo " params missing!"
        echo "Usage : $0 directory size (in KB)"
        exit 1
else
   if  [ $# -gt 3 ];
   then
        echo " too many arguments"
        echo "Usage : $0 directory size (in KB)  days"
        exit 1
   else
      blah blah
      ...
  fi
fi

James what do you get when you type "du -sh" as an output?

you can use "-size" option too when looking for a file as well as "-mtime".

And VBE made all confusing points clear which i stuck with.

"du -sh $1" gives the size of the directory. My idea was to = this size with an argument.

@Krabu

Crontab was the next step.
@Vbe
sry that there were no comments in the scrypt just getting started, i will try to comment more.

and yes I need third Argument $3 you right.

---------- Post updated 02-11-10 at 07:41 AM ---------- Previous update was 02-10-10 at 01:07 PM ----------

#!/bin/bash

# shows size of the directory $1 in kb (-ks), can be cahnged into -mb for #example for Megabytes

size=$(du -ks $1 | awk '{print $1}')

#Compares the Size with your max input size $2

if [ $size -gt $2 ];
then

#if greater deletes all files older than $3 days in directory $1 and it�s #subdirectories

find $1 -mtime +$3 -exec rm -f {} \;
echo "Files older than $3 days deleted"
else
echo "free Space available"
fi

works now for me. thanks