Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 10-21-2010, 06:18 AM   #1
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, 06:42 AM   #2
LQ Newbie
Registered: Oct 2009
Posts: 8

Rep: Reputation: 2
It sounds like you are attempting some sort of backup procedure. Take a look at this article, which was the basis of SnapBack2 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

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


Last edited by zuikway; 10-21-2010 at 06:47 AM.
Old 10-21-2010, 07:46 AM   #3
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)

Last edited by grandeinter; 10-21-2010 at 07:47 AM.
Old 10-21-2010, 09:20 AM   #4
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
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, 11:44 PM   #5
Jerry Mcguire
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 165

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

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

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/source-file.~xx~  <- second newest copy, ( version ;-1 )

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

Rep: Reputation: 0
use rsync

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


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 03:26 PM
Bash script to transfer folders recursively without overwriting via FTP laurens Linux - Newbie 9 11-13-2009 08:40 AM
How to copy files without overwriting? Stephan_Craft Linux - Newbie 7 02-17-2009 06:28 AM
How to copy without overwriting [KIA]aze Linux - Newbie 4 03-08-2007 08:42 AM
bash script to copy files thtr2k Programming 1 02-08-2007 01:03 AM > Forums > Non-*NIX Forums > Programming

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

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