How to Merge Directories in Mac OS X with ditto

Aug 12, 2010 - 6 Comments

terminal

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. Let’s learn how to merge directories with the ditto command on Mac.

How to Merge Folders with ditto

To use ditto for the purpose of merging directories together, launch the Terminal application.

You’ll want to 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).

Then hit return to execute the command.

For 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"

Hitting return will then merge those two directories.

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.

Another option is to use the cp command, which is will behave similar to ditto. The syntax is a bit more complex though.

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.

Do you know of another method of merging directories from the command line? Share with us your tips and tricks for merging folders on the Mac!

.

Related articles:

Posted by: Manish Patel in Command Line, Mac OS, Tips & Tricks

6 Comments

» Comments RSS Feed

  1. M. Kirby says:

    What I love is how ditto gives you no feedback whatsoever, so if you have a large directory you sit there wondering if it’s working or how long it’s going to take or if it’s frozen or what.

  2. Roger says:

    really? REALLY???
    Mac users usually aren’t a tech savvy as Windows users and definitely not as Linux users, both of which (OSs) do this SIMPLY. Now I have to explain to a customer they must use the terminal (which they didn’t know existed) and this long syntax for the directories to do this. Why this is not possible from Apples amazing OS GUI interface is beyond me. It is one thing to not do something as default, but for it to not do it all…? Really?

    • Matt says:

      You can just drag the items over from the GUI, and in OS X Lion you have more options too.

      • David says:

        You can’t just drag the items using the GUI, this will do a replace. Even using the Option key will only merge if files of the same name do not exist in the source and target folder. So Beware! Bottom line is that Apple is miles behind Microsoft on this one. Sad but true. Apple really need to fix this.

        • steve says:

          you missed the point.

          I believe Matt meant you can drag and drop two directories into the terminal instead of typing out ‘directory 1 directory 2″. so one would open a terminal. $ [type] ditto [drag and drop directory 1] space [drag and drop directory 2]

          • Matthew says:

            Ditto what Steve said.

            1) Open terminal and type: ditto

            2) Drag the source folder from finder into the terminal window.

            3) Next, drag the destination folder from finder into the terminal window.

            4) Press enter.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site