Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Say I have two folders with various content, foo and bar. How can I merge bar into foo so that:

  • Files in foo that are not in bar are untouched.
  • Files in bar that are not in foo are now in foo.
  • Files in foo that are also in bar have been replaced by the files from bar.
share|improve this question
up vote 21 down vote accepted

If I'm reading your requirements correctly, there's no files that go from foo to bar. It looks like you can just copy the contents of bar to foo, letting it overwrite files as necessary (its default behavior).

$ cp -R /path/to/bar/* /path/to/foo
share|improve this answer
Yeah, what I was uncertain about was the recursiveness and how to not end up with just having bar inside foo as foo/bar. But this seems to be what I want :) – Svish Feb 7 '11 at 12:57
Right, if you just did cp -R /path/to/bar /path/to/foo it would create a directory bar inside foo. Subtle point. – coneslayer Feb 7 '11 at 13:20

I had a very similar need: merge bar into foo but files which are in bar should not overwrite matching files in foo. In this case:

$ cp -R -n /path/to/bar/* /path/to/foo/
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .