LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 10:00 PM   #1
hardikgohil1988
Member
 
Registered: Sep 2014
Posts: 63

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-06-2015, 11:35 PM   #2
ilesterg
Member
 
Registered: Jul 2012
Location: München
Distribution: Debian, CentOS/RHEL
Posts: 587

Rep: Reputation: 72
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, 12:18 AM   #3
eklavya
Member
 
Registered: Mar 2013
Posts: 636

Rep: Reputation: 142Reputation: 142
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, 02:54 AM   #4
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
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, 04:39 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

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

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, 02:10 AM   #7
hardikgohil1988
Member
 
Registered: Sep 2014
Posts: 63

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



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 07:28 AM
[SOLVED] string manipulation in a script file mahmoodn Linux - General 12 06-02-2010 01:39 PM
.csv file upoload, manipulation, download script help donv2 Programming 6 12-19-2007 03:20 PM
script for file manipulation yongitz Programming 3 08-17-2006 11:22 AM
shell script: File Manipulation simon_qwl Programming 2 10-29-2005 03:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:02 PM.

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
Open Source Consulting | Domain Registration