how to delete the older files other than the recently added 5 files

Number of files will get created in a folder automatically daily.. so i hav to delete the older files other than the recently added 5 files..
Could u help me through this..??

Take a look at:

ls -lt

You will also want to search through the forums.

The above will show you how to see your files in date order. What do you think would be the next step?

ya.. it will list the files in date order.. but i hav to delete the files other than recently added 5 files.. (There should be only 5 files which are been recently added.. it should delete all the other files..)

Hello dude just try the below code

ls -lt | awk 'NR == 1 {next} {print $9}' | sed -ne '6,$p' | xargs rm -rf

You can save one pipe by replacing:

ls -lt | awk 'NR == 1 {next} {print $9}'

by

 ls -t -c1   

... Unless you are trying to learn awk of course...:wink:

Hi,
Try this,

ls -lt | awk '{if(NR > 6){print "rm -f",$9;}}' | sh

ls -lt | awk '{if(NR > 6){print $9;}}' | xargs rm -f 

Cheers,
Ranga:-)