Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

2 Answers 2

up vote 14 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
1  
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.