LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-06-2015, 11:00 PM   #1
hardikgohil1988
Member
 
Registered: Sep 2014
Posts: 61

Rep: Reputation: Disabled
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.
 
Old 07-07-2015, 12:35 AM   #2
ilesterg
Member
 
Registered: Jul 2012
Distribution: CentOS, SLES, RHEL, HP-UX, OpenSUSE Tumbleweed KDE
Posts: 504

Rep: Reputation: 54
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.
 
Old 07-07-2015, 01:18 AM   #3
eklavya
Member
 
Registered: Mar 2013
Posts: 619

Rep: Reputation: 136Reputation: 136
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.
 
Old 07-07-2015, 03:54 AM   #4
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 806

Rep: Reputation: 186Reputation: 186
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
 
Old 07-07-2015, 05:39 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Have you looked into the std tool logrotate (see /etc/logrotate.d dir on most Linux)
 
Old 07-07-2015, 09:10 PM   #6
hardikgohil1988
Member
 
Registered: Sep 2014
Posts: 61

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by AnanthaP View Post
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
 
Old 07-08-2015, 03:10 AM   #7
hardikgohil1988
Member
 
Registered: Sep 2014
Posts: 61

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by eklavya View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] shell script string manipulation problem :( rohaanembedded Programming 6 07-15-2013 08:28 AM
[SOLVED] string manipulation in a script file mahmoodn Linux - General 12 06-02-2010 02:39 PM
.csv file upoload, manipulation, download script help donv2 Programming 6 12-19-2007 04:20 PM
script for file manipulation yongitz Programming 3 08-17-2006 12:22 PM
shell script: File Manipulation simon_qwl Programming 2 10-29-2005 04:40 PM


All times are GMT -5. The time now is 05:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration