LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   File name manipulation using shell script (https://www.linuxquestions.org/questions/linux-newbie-8/file-name-manipulation-using-shell-script-4175547388/)

hardikgohil1988 07-06-2015 10:00 PM

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.

ilesterg 07-06-2015 11:35 PM

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.

eklavya 07-07-2015 12:18 AM

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.

AnanthaP 07-07-2015 02:54 AM

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

chrism01 07-07-2015 04:39 AM

Have you looked into the std tool logrotate (see /etc/logrotate.d dir on most Linux)

hardikgohil1988 07-07-2015 08:10 PM

Quote:

Originally Posted by AnanthaP (Post 5388276)
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

Issue is observed when date is changed.That why i need to go for sequence numbers

hardikgohil1988 07-08-2015 02:10 AM

Quote:

Originally Posted by eklavya (Post 5388237)
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.

I have written the code can you help me to modify it


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.