remove old date by shell script

the file in the backup as below:

file.zip.270306
file.zip.230306
file.zip.200306
.

i need to create a script where to keep only the latest 2 file date. example from above the script will remove file.zip.200306. and will keep the file.zip.270306 and file.zip.230306

+++++++
Part of my script...the test.zip is in the /folder and backup folder is at /folder/backup. My script purposes is....move the test.zip in folder directory then change the name to test.zip.DDMMYY

#/bin/bash
file=test.zip

ls -t /folder/file.zip.* | sed 1,2d | while read file ; do
         rm $file
done
emlilyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NopiusCommented:
change your extention to file.YYMMDD it will be much easier to sort and to find the latest 2
0
Gabriel OrozcoSolution ArchitectCommented:
hello


it's kind of easy if your files are created on the same date they show.
because you can use the date the file was created to process it.

the follow find command deletes all files more than two days old:

find /folder -type f -mtime +2 -exec rm -fr {} \;

so you can modify the "+2" part if you want to delete files with more than, say; 4 days old:

find /folder -type f -mtime +4 -exec rm -fr {} \;

keep in mind the path should be exactly given, or you will delete all your files!!!!


Hope this help
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NopiusCommented:
Redimido, when user hasn't run rotation script for last two days (suppose system was down),
your script will remove all files and will not leave the latest two.
0
Gabriel OrozcoSolution ArchitectCommented:
Nopius: Yes, and that's good enough if you want to have the last 'n' days instead of the last 'n' logs.

if the system was down, well, then it was down. no further problem.
if the user forgot to run the rotation script, so then it will not run this script neither.

 =)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.