How to Merge Directories in Mac OS X with ditto
If you have two directories you need joined together, you can either drag and drop everything together, use the ‘mv’ command to move files manually, or, as we’ll show here, you can quickly merge any two directories within Mac OS X by using the command line tool ditto or ‘cp’. The command line is generally aimed at advanced users, but ditto is pretty simple to use so almost anyone can use it this way if they’re comfortable with the terminal.
To use ditto for the purpose of merging directories together, launch the Terminal application and use the following syntax:
ditto directory1 directory2
If a directory already exists at the destination (directory2) then the contents of the source (directory1) will be merged with the contents of the destination (destination2).
So as an example, let’s say I want to merge pictures from “August 2010″ into “Summer 2010″ then I will use the following command string to accomplish this:
ditto "August 2010" "Summer 2010"
It’s important to understand how ditto works and how it differs from the mv and cp commands, the manual page is a good place to start if you’re unfamiliar. The man page for ditto describes further:
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.
Note the creation of destination directories, and as we emphasize here, the ability to merge a source and destination directory with the ditto string.
If you’re not familiar with the command line or comfortable using it for more advanced methods of file transfers like this, you may want to just use the Finder GUI to perform this type of action.
Merging Directories with ‘cp’ at Command Line
If you don’t want to use ditto, you can also use the cp command with the -r and -n flags like so :
cp -r -n ~/Desktop/Dir1/* ~/Desktop/Dir2/
This will copy everything from Dir1 into Dir2 but not overwrite any matching files.