LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-08-2007, 04:40 AM   #1
[KIA]aze
Member
 
Registered: Jun 2006
Distribution: Debian, Ubuntu, Windows XP
Posts: 146

Rep: Reputation: 16
How to copy without overwriting


How can I copy files without overwriting files in the destination directory AND without having to answer a prompt everytime.
(and also independently of wether the source file is older/newer than the destination file)
 
Old 03-08-2007, 05:11 AM   #2
silent_ion
LQ Newbie
 
Registered: Jul 2006
Distribution: Debian GNU/Linux
Posts: 4

Rep: Reputation: 0
Maybe something like this:
Quote:
false | cp -i dir1/* dir2/ > /dev/null 2>&1
 
Old 03-08-2007, 06:43 AM   #3
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

Most Linux systems have utility rsync installed. This worked for me:
Code:
rsync -av --ignore-existing d1/ d2/
see man rysnc for details.

Another, somewhat more complex solution would be to write a script to compare file names in the two directories and omit the copy for matches ... cheers, makyo
 
Old 03-08-2007, 07:04 AM   #4
[KIA]aze
Member
 
Registered: Jun 2006
Distribution: Debian, Ubuntu, Windows XP
Posts: 146

Original Poster
Rep: Reputation: 16
Thanks it works perfectly.

But could you explain exactly how it works?

I understood how the pipe works, but I don't understand the difference between "2>1" and "2>&1".

Code:
$false | cp -irv toto toto2/ 2>1
`toto/newfile10' -> `toto2/toto/newfile10'
Code:
$false | cp -irv toto toto2/ 2>&1
cp: overwrite `toto2/toto/file'? cp: overwrite `toto2/toto/newfile'? cp: overwrite `toto2/toto/newfile2'? cp: overwrite `toto2/toto/newfile3'? cp: overwrite `toto2/toto/newfile4'? cp: overwrite `toto2/toto/newfile10'? `toto/newfile12' -> `toto2/toto/newfile12'
I know that 0=stdinput, 1=stdoutput, 2=stderr.
But why "&1" instead of "1"?

I also tried redirecting stdin to stdout to output "false", but instead it asked me for confirmation:

Code:
$false | cp -irv toto toto2/ 0>&1
cp: overwrite `toto2/toto/file'?
edit:
OK, I see, I just created some useless 0,1 and 2 files... ^^

Last edited by [KIA]aze; 03-08-2007 at 07:26 AM.
 
Old 03-08-2007, 07:42 AM   #5
[KIA]aze
Member
 
Registered: Jun 2006
Distribution: Debian, Ubuntu, Windows XP
Posts: 146

Original Poster
Rep: Reputation: 16
Something else:

Is there a way to generate a nice error file of files that were not overwritten?
I tried 2>err of course, but the problem is that they all get listed in one line instead of one per line.
 
  


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
Dister with dd overwriting / partition Andrew_OC Mandriva 2 01-26-2007 09:48 AM
Overwriting a linux distro sfzab Linux - Software 1 04-23-2006 02:51 PM
Arrays overwriting variables in c george_mercury Programming 4 12-31-2004 06:20 AM
Avoid overwriting bootloader jwijesundra Linux - General 5 07-30-2004 05:16 AM
Overwriting Redhat9 With Suse9 happyman Linux - Distributions 2 04-08-2004 11:37 AM

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

All times are GMT -5. The time now is 03:09 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
Open Source Consulting | Domain Registration