How to delete files, except the recent one

Hi,
I am using this script to delete logs file from my server.
It search all the files in the certain folder and delete them all except the recent one.
this help me to save disk space on my servers.


#!/bin/sh

DIR=/any/dir/you/want

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

Enjoy.


No comments:

Post a Comment