Link to home
Create AccountLog in
Avatar of emlily
emlily

asked on

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
Avatar of Arty K
Arty K
Flag of Kazakhstan image

change your extention to file.YYMMDD it will be much easier to sort and to find the latest 2
ASKER CERTIFIED SOLUTION
Avatar of Gabriel Orozco
Gabriel Orozco
Flag of Mexico image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
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.
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.

 =)