O'Reilly logo

Mac OS X Hacks by Rael Dornfest, Kevin Hemenway

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mirroring Files and Directories with rsync

With rsync, supplied by default on OS X, quick mirrors and backups are a command line away.

As its name suggests, rsync synchronizes files and folders from one location to another. That location could be another directory on your current machine, or any other rsync-enabled machine that you have access to. Using the rsync protocol, only differences between files are transferred; if you’re transferring a 30MB text file that had only a few spelling corrections, you’ll transfer only those corrections, not the entire 30MB. You won’t believe how much of a time-saver this is until you turn your head back and realize it’s finished.

I’ve always been fond of the learning-by-doing school of thought, so open up a Terminal and enter the following command:

% rsync -vaz ~/Library ~/Backups

Let’s break that down: rsync is the name of our utility and -vaz are some flags we’ve passed to it. By issuing an rsync --help at the command line, we quickly find out what that means:

-v, --verbose  increase verbosity
-a, --archive  archive mode
-z, --compress compress file data

Archive mode is a special rsync configuration that is suitable for mirroring. As the manual suggests (see man rsync), archive mode is a quick way of saying you want recursion and to preserve almost everything. In this case, everything means permissions, ownership, file modification and access times, and so on. The -z flag, for compression, is more useful when you’re handling mirrors or backups ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required