delete files except most recent

#!/bin/ksh -xvf

for arch_filename in `ls -lrt /u02/oracle/CMDR/archive | awk '{print $9}'`; do
echo "rm -rf /u02/oracle/CMDR/archive/"$arch_filename
rm -rf /u02/oracle/CMDR/archive/$arch_filename
done

I am running the above shell script every 10 minutes. I need to not delete the most recent file, but delete everything else. Each file is 500 mb. How would I do it. Basically when job kicks off, some files are getting deleted as it is writing the archive log and system is getting filled up and I cant see where the files are written to. I have to bounce the box to see the space. This is linux box.

Thanks, ST2000

DIR=/u02/oracle/CMDR/archive

find $DIR -type f -maxdepth 1 -printf "%T@\t%p\n" | #list files with timestamps
sort -k1,1n | #sort oldest -> newest
cut -f2- | #remove timestamps
sed '$d' | #remove the last one (newest file)
tr '\n' '\0' | #delimit with NULs in case spaces in names
xargs -r0 rm -f

Also have a look at my newest script

try ... (in ksh) ...

DIR=/u02/oracle/CMDR/archive

cd $DIR
lastfile=$(ls -rt | tail -1)
for afile in $(ls | grep -v $lastfile)
do
    echo > $afile
    rm -rf $afile
done

Can you explain why you empty the file before deleting it? Is that faster?

i've found that when the filesystem is short on space, rm will fail and/or take longer to complete --- emptying the file lets me get around the space issue ... yes, i think it is faster considering the alternatives ...

Thanks for all the posts.

Each file is about 500mb. If the delete job kicks off and if there is a file written at that time, linux does not know what to do and locks that space off. Say 200 mb is written when delete kicks off, that 200mb is gone from archive filesystem and in a week's time it reduces to nothing and will not last even an hour. I have to bounce db to clear up pending archives. Instead i want a modification to my script to preserve the last file and delete all.

Anyway i modified the script adding a variable which greps for last file and delete using grep -v of that last file. It was just one more line to my existing script.

Thanks again