Shell script delete log files from folder & subfolders on space usage

Hi,
I am trying to write a shell script to delete logs generate by db when space in the folder reaches 70%. i am getting space values from db, find the files at OS and remove them by using a cron job runs every 5minutes.
I have to keep the latest 5 files at any time, my problem is that log files are generated based on today date folder eg:
2009_03_20, 2009_03_21, 2009_03_22 etc. and
a log file is generate at 30 time interval gap or may generate 2 files per minute depends upon db activity.

how to keep only latest 5 files and remove rest of the files
my script looks like this in linux env.
#! bin/bash
ORACLE_SID="$1"
limit=$2

export $ORACLE_SID
fdir=/tmp/log
export $fdir

export ORACLE_HOME=`grep $ORACLE_SID: /etc/oratab |awk -F: '{print $2}'`
sqlplus -s "/ as sysdba" <<EOF >$fdir/fravalue
@$fdir/arclg_db.sql
EOF

fradest=`cat $fdir/fravalue |awk '{print $1}'`
spaceused=`cat $fdir/fravalue |awk '{print $2}'`

if [ $spaceused -gt $limit ]; then
echo "time to delete archivelogs to free up space"
find $fradest/$ORACLE_SID/archivelog -type f -maxdepth 2 -printf "%T@\t%p\n"| sort -k1,1n |cut -f2-|sed '$d'|rm -rf

else
echo "Not required to delete logs.. lots of space"
fi
please advice me, or suggestions to improve my script.

Thanks
Saha

Please put code inside

 tags.



#! bin/bash
ORACLE_SID="$1"
limit=$2

export $ORACLE_SID
fdir=/tmp/log
export $fdir

[/quote]

[indent]
Why are you trying to export these variables? I see nothing in the script that would require it.

If you did need it, those commands would not export the variables.

export ORACLE_SID fdir ## no $

You don't need awk and even if you did, you would certainly not need cat.

read fradest spaceused < $fdir/fravalue

You haven't supplied any filenames to rm; rm does not read from the standard input. Perhaps you meant:

xargs rm -rf

Thanks for the code suggestions, i will try to implement and
need inputs on how to keep latest 5 files all the time while remove all other files from the said folders & sub folders when ever the script runs.

ls -t <list of files> | tail -n +6 | xargs rm -f