Merge two folders on Mac using Terminal and tar
Thursday, December 27th, 2007I have faced a little problem today when I needed to merge two folders' contents, but with a little trick.
Basically, I usually work on projects using Subversion, so I always keep track of what's changed, but sometimes I have to give the files to third party for various - usually design or content - enhancements. The problem is that I usually just don't have time/will/both to explain how Subversion (of version control systems, for that matter) works, and just handle exported files to the third party. This approach removes version control from files, but when I get the modified files back, I want them back into Subversion structure - this way I can see what exactly was changed, as well as have a ground to blame a specific person if some parts which should have not be touched, were actually touched and some part of the system broke in the result.
So, in order to that kind of trick, I need to merge "new" un-versioned directory structure into "old" versioned one, replacing all old files with new ones, but keeping my previous .svn directories.
You can't do folders merging using Mac OS X Finder, as well as I haven't found a good way to do that using FileMerge utility from Developer Tools. But there is actually an easy way to achieve the required result - and the tool for it is good old tar utility.
The way tar works during un-archiving is that is actually merges files and folders, and doesn't replace them. So, if directories on your hard drive and inside the .tar archive have the same name, the directories on your hard drive won't get erased and then replaced with stuff from .tar, but rather have their contents and contents of .tar directories merged. Just what I need.
Here's what you need to do to merge stuff.
We have two directories: project_trunk is a versioned directory, project_changed is the un-versioned directory we received from third-party. These directories are located on the same level so we can go between them with "cd ../project_trunk" or "cd ../project_changed" commands.
Open Terminal application and to the following:
cd /path/to/project_changed
tar cf allfiles.tar *
mv allfiles.tar ../project_trunk
cd ../project_trunk
tar xvf allfiles.tar
rm allfiles.tar
You're done! The un-versioned directory structure was successfully merged into the versioned one.
Now just do svn status on the project_trunk directory and compare changes and commit. Very fast and easy :)




