Merge two folders on Mac using Terminal and tar

I 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 :)

6 Responses to “Merge two folders on Mac using Terminal and tar”

  1. Terrell Says:

    Very nice.

    I've been manually moving files around (or svn rm and svn add'ing) manually.

    This is obviously much better.

  2. Curtis Chambers Says:

    I'd also add that at the end I'd do an "svn add * –force" to make sure that any new files that have been added are included in SVN. Sometimes there are little files many folders deep that have been added that you have no idea about since the default svn add skips folders that it already knows about.

  3. Bob Says:

    Have a look at ECMerge. It's a diff/merge tool running on Windows & MacOS, but also Linux and Solaris. It does side by side and 3-way merge for files and folders. You can use it to merge SVN branches or any other directory.

  4. rtc Says:

    I came across this problem today.

    Officially I hate Mac's I've decided. This is just another in a series of just strange things that Mac does not do. It's mind boggling.

  5. Jean-Denis Says:

    rtc, this is the *only* feature from Windows that might be considered worth adding to MacOS X. And even then, that's debatable. The semantics of moving something over to a place where another item is named identically is to replace it. Overloading a merge feature on it is unnecessary cognitive load, and thus potentially confusing and bad.

    I probably curse Windows more often for merging two directories when I wanted to replace, than MacOS when wanting to merge.

    That being said, merging two folders is a useful thing once in a while. And this is working solution. I personally tend to use ditto for it. I also made an automator action that invokes ditto, so I don't even have to launch the terminal.

    Oh BTW, how is Automator named on Windows?

    And maybe you can enlight me: can you change the current directory in a Windows terminal by a simple drag and drop from a regular window?

    And a gazillion other minor nitpicks that pile up higher than mount Everest.

    (no need to respond, I know the answer).

  6. gstein Says:

    I'm running Leopard and trying to run security updates on Drupal site while preserving some profiles in another version–your tar technique worked great! Thanks so much!

Leave a Reply