LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-21-2010, 05:18 AM   #1
grandeinter
LQ Newbie
 
Registered: Oct 2010
Posts: 2

Rep: Reputation: 0
Bash script to copy without overwriting .. Help


Hi guys

I'm a new person in Unix world , So I need some help please
i have one questions :


"bash copy filename destin_dir"

1- i need a bash script to copy file from one directory to another , and if the destination directory have the same file name, add '_1' to the file name ..
and if there is a file with 'filename_1' , add '_2' to the file name and become 'filename_2' and so on ..


Thanks in advance
 
Old 10-21-2010, 05:42 AM   #2
zuikway
LQ Newbie
 
Registered: Oct 2009
Posts: 2

Rep: Reputation: 0
It sounds like you are attempting some sort of backup procedure. Take a look at this article, which was the basis of SnapBack2 http://www.mikerubel.org/computers/rsync_snapshots/. This article describes a way to create backup directories that are linked (hard links) such that if the file copied has not changed you don't automatically increase storage. You might get some ideas by reviewing these scripts. Instead of renaming files, the whole directory is copied, but in a way that only changed files actually increase storage.
Look also at SnapBack2 http://www.perusion.com/misc/Snapback2/

PS, there are some other links to scripts at the end of the 'rsync snapshot' article.

Wayne

Last edited by zuikway; 10-21-2010 at 05:47 AM.
 
Old 10-21-2010, 06:46 AM   #3
grandeinter
LQ Newbie
 
Registered: Oct 2010
Posts: 2

Original Poster
Rep: Reputation: 0
^
thanks for ur comment

i didn't mean the backup or something like that

i just want how to solve the trick in question by adding (1,2,..) if there is a file have the same name in destination directory .

I want to solve it by the basic command (cp)
HELP

Last edited by grandeinter; 10-21-2010 at 06:47 AM.
 
Old 10-21-2010, 08:20 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,562

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
Pseudo code:

1. Check if filename already exists in location. Note it may be part of the filename, eg. filename_X
2. If it exists check for any ending value and if found increment by one
3. Copy file to location

As this appears like a homework question you will need to act on the process and let us know where you are stuck / getting errors.
 
Old 10-21-2010, 10:44 PM   #5
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 143

Rep: Reputation: 17
Not sure if this is what you want:

Code:
cp --force --backup=numbered source-file dest-dir/
so that

Code:
dest-dir/source-file       <- newest copy, ( version ;  if you are familiar with VMS versioning)
dest-dir/source-file.~1~   <- oldest copy, ( version ;1 )
dest-dir/source-file.~2~   <- second oldest copy, ( version ;2 )
dest-dir/...
dest-dir/source-file.~xx~  <- second newest copy, ( version ;-1 )

Last edited by Jerry Mcguire; 10-21-2010 at 10:51 PM. Reason: more details
 
Old 10-22-2010, 07:13 PM   #6
multivers88888
LQ Newbie
 
Registered: Oct 2010
Posts: 5

Rep: Reputation: 0
use rsync

I would use rsync for this... is has incremental backup
 
  


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] Hoe to copy without overwriting abdoullah Linux - Newbie 5 10-05-2010 02:26 PM
Bash script to transfer folders recursively without overwriting via FTP laurens Linux - Newbie 9 11-13-2009 07:40 AM
How to copy files without overwriting? Stephan_Craft Linux - Newbie 7 02-17-2009 05:28 AM
How to copy without overwriting [KIA]aze Linux - Newbie 4 03-08-2007 07:42 AM
bash script to copy files thtr2k Programming 1 02-08-2007 12:03 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration