Unix: How to merge two directories

Solution 1:

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

Solution 2:

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/

Solution 3:

Try this :

$ cp -r-u-v /path/to/foo/* /path/to/bar/

From man cp
-u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing
-R, -r, --recursive copy directories recursively