Use ditto to Copy Files & Directories from the Mac Terminal

Most command line users have confidence in the cp command to copy files and directories, however mac OS X offers another resolution with the ‘ditto’ command. Ditto is slightly more advanced however may be advantageous to ‘cp’ for many reasons, because it not only preserves attributes and permissions but additionally file resources and file and folder information, primarily insuring that the file and/or folders are copied specifically. Additionally, ditto will be used to copy a file or folder to a source directory, however if that source doesn’t yet exist, ditto can automatically create it.  Also, if the destination folder will exist, the copied contents are merged along with destination directory files / folders. Finally, ditto additionally follows symbolic links, making it significantly handy if you’re a heavy user of the “ln” (The ln command is used to create either a “hard link” or a “symbolic link” between files and/or directories) command.

To better understand the ditto command, let’s run through some examples:

Using Ditto to copy Files / Folders

at its simplest type, ditto works just like the cp command, with basic syntax as follows:

ditto source destination

I.E. if you wanted to copy ~/Desktop/mystuff to /Volumes/mystuff/ you would just type the following:

ditto ~/Desktop/mystuff /Volumes/ mystuff/

Again, this can retain all ownership and resource data details of the files copied, which can be significantly vital if you’re copying files from one user directory to a different, or if you wish to preserve something like modification times of files. If you’re unsure concerning the source and destination contents, you’ll be able to always compare the 2 with the “comm command” or the “diff command” before continuing with the ditto command.

[Compare Two Directories Contents on a Mac Using diff

If you would like to check the difference between 2 folders on a mac, launch the Terminal and read on, because we’ll show you the way to match 2 directories and also the contents of these directories by exploitation the Terminal to output a file containing the precise variations shown between 2 target folders.
To achieve this comparison, we’ll use the command line tool ‘diff’, it’ll simply compare the contents of any 2directories by using the next command:

diff -rq directory1 directory2 >> differences.txt

This executes the diff command comparing directory1 and directory2 (if you’ve got a folder with a space within the file name, simply put it in quotes like so: “folder one”), then redirects the output of that command to a file nameddifferences.txt. Here’s an example and the way the particular output can look:

diff -rq “old music” “new music” >> musicfolders.txt

Now look within the present  directory for the file you just created completing the diff command, during this case the file is musicfolders.txt and also the contents is viewed in any text editor, command line or otherwise. Opening the document you’ll see something like this:

Only in old music: song1.mp3
Only in old music: song2.mp3
Only in old music: song3.mp3
Only in new music: instrumental1.mp3
Only in new music: instrumental1.mp3

If you wish to look at the file from the command line, try:
more musicfolders.txt
Otherwise simply navigate to the containing directory and open it in your favorite text editor. If you’d prefer to not produce a text file with the changes, simply leave off the output redirection of the command. You may wish to pipe the output to one thing like ‘more’ to make it easier to scan though:

diff -rq “old music” “new music” | more

It’s worth mentioning that this command can work in mac OS X and in most UNIX based OS’s.]

Using Ditto to Merge Directories & Folder Contents

Remember, ditto can check to examine if the destination already exists, and if it does, it’ll merge the directories of the source to the destination. This can be vital and very helpful, creating it one among the best ways to merge the directories from the command line in OS X (though it’s easy currently in the Finder too).

ditto ~/Pictures/spring2015/ /Volumes/PhotoBackup/2015/

This would take all photos 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 happens once the destination already exists, if the destination doesn’t exist it will be created as specified, or it will be taken from source name. If you’re using ditto to copy information from directories with symbolic links, using the -V (verbose all) flag is valuable as a result of it’ll show each file and symbolic link that has been copied. Note -V is totally different than -v, which can solely 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, 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, however it may be helpful for a few cases.

You can learn way more concerning the wonderful ditto command by reading its manual page, accessible in OS X by typing:

man ditto

ditto-man

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

 Before you rely on ditto heavily, make sure to do it a couple of times with unnecessary file / folder moves and directory merges to know how it works along with your planned usage.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Up ↑

%d bloggers like this: