How do I safely merge two directories using commands on Solaris (and OSX)?

3

Directory A and B have many subdirectories with nearly identical content. Directory B may have newer files and some subdirectories that directory A does not have. When files in Directory B are newer, I want those to overwrite the ones in A. When B contains subdirectories that A does not have, I want B directories to be merged into the results. So, I guess all I'm saying is I need to merge two directories using commands available on Solaris and OSX. I'm working locally on OSX, but will need to perform the same procedure on a Solaris box. I've found a couple of techniques. The first approach is to use tar:

cd A
tar -cvf content.tar *
cp content.tar ../B
tar -uvf content.tar *
cp content.tar ../
mv A A_backup; mv B B_backup
tar -xvf content.tar

(Wow! that seems messy)

Another approach:

cd B
tar cf - . |(cd ../A; tar xvf -)

(That merges everything into A)

The problem I have with both is perhaps mostly a confidence issue. I'm not familiar with the methods and can't really see what is happening. I can't see what files were overwritten or what folders or files existed in B that did not exist in A.

Is there a better way?

Michael Prescott

Posted 2011-05-31T20:12:18.850

Reputation: 3 351

1If you don't require using same tool on both systems, consider making this two questions. – Daniel Beck – 2011-06-01T14:07:20.133

Answers

2

Turns out rsync can be used for local directories.

rsync -av B A

laslowh

Posted 2011-05-31T20:12:18.850

Reputation: 121

Thanks, that will work well for OSX, but it isn't available to me on the Solaris system. – Michael Prescott – 2011-05-31T21:25:29.377

1

Mac OS X has ditto:

ditto [-v] [-V] [-X] [<options>] src ... dst_directory

In its first form, ditto copies one or more source files or directories to a destination directory. If the destination directory does not exist it will be created before the first source is copied. If the destination directory already exists then the source directories are merged with the previous contents of the destination.

Daniel Beck

Posted 2011-05-31T20:12:18.850

Reputation: 98 421