Terminal 101: Using Ditto to Copy Files

Every Monday, we’ll show you how to do something new and simple with Apple’s built-in command line application. You don’t need any fancy software, or a knowledge of coding to do any of these. All you need is a keyboard to type ’em out!

We’ve talked about the cp command in the past, which copies files between locations on your Mac. However, Apple has developed its own (better) implementation of cp. This command, called “ditto,” not only copies the files from the source directory to the destination directory, creating the destination directory if it doesn’t already exist; but will also merge the contents of the source directory with the destination directory if it does exist. Other niceties of this command are that it will follow symbolic links when copying files, and also preserve the file hard links modes and other metadata. Let’s get started copying files with the ditto command.

Before we begin, let’s first take a look at the basic syntax of the command:

ditto sourceFolder destinationFolder

As with other terminal commands, we type the command “ditto” first, then a space, followed by the source folder path (the source folder is the location you’re copying from), then another space, followed by the destination folder path (the destination folder is the location you’re copying to).

So if we wanted to copy a folder and its included files and subdirectories, then we could issue a command like this:

ditto ~/Documents/MyFolder ~/Desktop/MyNewFolder

This would copy the contents of the MyFolder directory in the Documents folder to the “MyNewFolder” directory on the Desktop. Pretty straightforward, right?

After pressing enter, ditto does the rest, ensuring that the contents of the source folder are copied to the destination. If the destination folder doesn’t exist, then it’ll be created for you. If it does exist, then the contents will be merged into the destinatio folder, ensuring that nothing is overwritten accidentally.

What if you want to see what the command is actually doing while it’s running? Well, ditto has a verbose mode that spits out the current operation to the command line for your viewing pleasure. Let’s take a look at how you can add this flag:

ditto -V ~/Documents/MyFolder ~/Desktop/MyNewFolder

As you can see here, we simply add the “-V” flag, and the command will begin printing out the current operation (one line at a time) to the terminal as each file is copied to the destination directory. This is great if you need to monitor the operation as it runs.

Cory Bohon is a freelance technology writer, indie Mac and iOS developer, and amateur photographer. Follow this article’s author on Twitter.