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, you can 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
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.