Use ditto to Copy Files & Directories Intelligently from the Mac Terminal

Jun 11, 2014 - 8 Comments

The ditto command Most longtime command line users rely on the cp command to copy files and directories, but Mac OS X offers another solution with the ‘ditto’ command. Ditto is slightly more advanced but can be advantageous to ‘cp’ for several reasons, as it not only preserves ownership attributes and permissions but also file resource forks and file and folder metadata, essentially insuring that the file and/or folders are copied exactly. Additionally, ditto can be used to copy a file or folder to a source directory, but if that source doesn’t yet exist, ditto will automatically create it. Also, if the destination folder does exist, the copied contents will be merged together in that destination directory. Finally, ditto also follows symbolic links, making it particularly handy if you’re a heavy user of the ln command.

To better understand the ditto command, let’s run through a few examples with real syntax.

Using Ditto to Copy Files / Folders

At it’s most simple form, ditto works much like the cp command, with basic syntax as follows:

ditto source destination

For example, if you wanted to copy ~/Desktop/FluffyBackups to /Volumes/FluffyBackups/ you would just type the following:

ditto ~/Desktop/FluffyBackups /Volumes/FluffyBackups/

Again, this will retain all ownership and resource metadata details of the files copied, which may be particularly important if you’re copying files from one user directory to another, or if you want to preserve something like modification times of files.

If you’re uncertain about the source and destination contents, you can always compare the two with the comm command or the diff command before proceeding with the ditto command.

Using Ditto to Merge Directories & Folder Contents

Remember, ditto will check to see if the destination already exists, and if it does, it will merge the directories of the source to the destination. This is important and extremely useful, making it one of the easiest ways to merge the directories from the command line in OS X (though it’s easy now in the Finder too).

ditto ~/Pictures/Fall2015/ /Volumes/PhotoBackup/2015/

This would take all pictures from “Fall2015″ and copy them into the pre-existing directory “2015″, effectively merging the contents from the source to the destination. Again, the merge behavior occurs when the destination already exists, if the destination doesn’t exist it would be created as specified, or as the source name.

If you’re using ditto to copy data from directories with symbolic links, using the -V (verbose all) flag is valuable because it will display every file and symbolic link that has been copied. Note -V is different than -v, which will only show files as output, and not symbolic links.

Copy Without Metadata Using Ditto

If for some reason you don’t want to copy metadata and resource forks, youc an use the –norsrc flag like so:

ditto -V --norsrc ~/Sample/Folder /Volumes/NoMetadataBackups

Using the –norsrc flag kind of defeats a primary benefit of ditto, but it can be useful for some cases.

You can learn much more about the excellent ditto command by reading its manual page, accessible in OS X by typing:

man ditto

The ditto man page explains how to use it to copy files and directories in an advanced manner

As usual, use the arrow keys to navigate up and down in the manual page.

Before you rely on ditto heavily, be sure to try it out a few times with inconsequential file moves and directory merges to understand how it works with your planned usage.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS X, Tips & Tricks

8 Comments

» Comments RSS Feed

  1. Very good stuff. I’ve found that ditto works great when moving data from a Windows server onto a Mac. It seems to be a lot less error prone.

  2. Ex2bot says:

    I’m sure your recommendation to use ditto instead of cp is a good one, but cp has preserved resource forks for over a decade. I remember Apple first offered a version of cp that ignored resource forks, then offered a version called cp_mac that handled resource forks. It was a long time ago, but it may have been around 10.1. By 10.2 or 3, the regular cp copied resource forks with no problem.

    • TGV says:

      Ditto does that too, the man page tells: “ditto will preserve resource forks and HFS meta-data information when copying unless instructed otherwise using –norsrc .”

  3. TGV says:

    Slightly off-topic: for easy man page reading, install Bwana (http://www.bruji.com/bwana/). It allows you to type, e.g., man:ditto in the address bar of a browser, and the man page will open in your browser. Much easier than in the terminal.

  4. Jay says:

    This is great, but what about using a command that checks if an item exists, and if it does, if it is not newer then do not copy over. That is more “Intelligent”

  5. arob says:

    Thanks, good to know!

    Merge seems to be a misnomer. According to man, “ditto
    overwrites existing files, symbolic links, and devices in the destination when these are copied from a source.” I tend to think of Merge as keep existing, add missing files. So if destination has a newer version of the same file name, ditto would apparently replace the newer version with the older one.

  6. Cliff says:

    Can ditto be used to copy a file form a windows share to a local folder by calling out a smb//:xxx.xxx.xxx.xxx as the source? If not what is the best way to do this form the terminal?

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

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