File name manipulation using shell script
Hello,
I have 3 file in a folder namely backup-20150706-100433-1.tar.gz backup-20150706-145531-2.tar.gz backup-20150706-145734-3.tar.gz once i create a new backup file the new backup file should me renamed from backup-date.tar.gz to backup-date-3.tar.gz backup-date-3.tar.gz to backup-date-2.tar.gz 2-1 and remove 1. help me to write a shell script. |
Nobody's going to write a script for you. You can hire and pay someone to do it for you.
But everyone's willing to help you fix any problem you have, that is, only if you have already tried doing it first. |
We need to see your efforts first. What have you written so far? Paste your code here and we will definitely help you to solve your problem.
If new back-up file is always going to be saved as backup-.*-3.tar.gz, you need to remove first backup-.*-1.tar.gz, then rename backup-.*-2.tar.gz into backup-.*-1.tar.gz and backup-.*-3.tar.gz into backup-.*-2.tar.gz, so your backup directory will have only three files always. First try on test directories and test files. It is just bunch of step by step commands, there is no rocket science. |
Since the generated file name of the back already contains the YYYYMMDD-HHMMSS why bother with a complicated scheme to rename the files? wouldn't it be straight forward to just retain the latest three files (by date) after the backup command (unless your date gets changed often).
OK |
Have you looked into the std tool logrotate (see /etc/logrotate.d dir on most Linux)
|
Quote:
|
Quote:
k=$(ls /syslink/.sram_copy/backup*.tar.gz 2> /dev/null | wc -l) l=`expr $k + 1` myfile=/syslink/.sram_copy/backup-$(date +%Y%m%d-%H%M%S)-$l.tar.gz if [ $k -eq 3 ] then i=1 rm /syslink/.sram_copy/backup*$i.tar.gz rm /syslink/.sram_copy/backup*$i.tar.gz.md5 i=`expr $i + 1` while [ $i -le 3 ] do filename=$(ls /syslink/.sram_copy/backup*$i.tar.gz) filename=${filename##*/} j=`expr $i - 1` filename1=/syslink/.sram_copy/${filename%-*.*}-${j}.tar.gz mv /syslink/.sram_copy/$filename $filename1 md5sum $filename1 > $filename1.md5 rm /syslink/.sram_copy/$filename.md5 i=`expr $i + 1` done tar zcf /syslink/.sram_copy/backup-$(date +%Y%m%d-%H%M%S)-3.tar.gz /sramdisk/ > /dev/null 2>&1 myfile=$(ls /syslink/.sram_copy/*3.tar.gz) md5sum $myfile > $myfile.md5 elif [ $k -eq 2 ] then tar zcf $myfile /sramdisk/ > /dev/null 2>&1 md5sum $myfile > $myfile.md5 elif [ $k -eq 1 ] then tar zcf $myfile /sramdisk/ > /dev/null 2>&1 md5sum $myfile > $myfile.md5 elif [ $k -eq 0 ] then tar zcf $myfile /sramdisk/ > /dev/null 2>&1 md5sum $myfile > $myfile.md5 fi |
All times are GMT -5. The time now is 05:57 PM. |