Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[Mini-TIP] Copy without overwriting
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
McLink
Apprentice
Apprentice


Joined: 02 Feb 2008
Posts: 181
Location: /dev/chair

PostPosted: Mon Nov 24, 2008 11:22 am    Post subject: [Mini-TIP] Copy without overwriting Reply with quote

cp, by default, overwrites the destination file. Using the -i option, it prompts you if you for input when it's about to overwrite something, so you can say "n" and hit enter to not overwrite the destination. But what if you want to merge two directories containing thousands of files, without overwriting any destination files? What you want is basically the opposite of the --force option, but such an option does not exist with the default cp.

Well, there's a simple solution: yes. If you want to prevent cp from overwriting any destination files, you can use the following:
Code:
yes n | cp -i srcdir/* destdir

It basically tells cp to prompt for overwriting, and programmatically feeds it "n"'s, asserting no destination files are overwritten.

Trivia: this type of behaviour is exactly what yes was created for in the first place.
_________________
Mc'abit wrote:
Islam isn't the problem, religion is.
Back to top
View user's profile Send private message
michel7
Guru
Guru


Joined: 04 May 2006
Posts: 461
Location: localhost

PostPosted: Mon Nov 24, 2008 11:43 am    Post subject: Reply with quote

nice tip! thanks!
_________________
Software is like sex. It's better when it's free
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum