Joined: 02 Feb 2008
|Posted: Mon Nov 24, 2008 11:22 am Post subject: [Mini-TIP] Copy without overwriting
|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:
|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.
PUSH BUTTON, RECEIVE BAILOUT