Terminal 101: Using rsync locally
Posted 03/11/2013 at 11:29am
| by Cory Bohon
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!
Rsync is a piece of utility software that comes standard on most Unix-based systems, providing the ability to synchronize files both locally and remotely. The difference between rsync and cp (the Unix copy command) is that rsync can use compression and smartly determine which files have changed, allowing the synchronization process to happen much faster than just a standard copy. Whether you need to back up your files or simply keep a copy elsewhere on your computer, rsync can save the day (and some time). Continue reading, and we’ll show you how to put this free tool to use on your Mac.
The Command

To use rsync, you will need two directory locations on your Mac: The destination folder, and the source folder containing the files to sync to the destination.
The basic command to synchronize files is as follows:
rsync -options SourceDirectoryPath DestinationDirectoryPath
You’ll replace options with one of the myriad of options listed below or leave it blank to synchronize only a single file at the Source and Destination paths. Be sure to leave a space between the options, source, and destination paths; otherwise the CLI (command line interface) will give you back an error message.
The Options
There are many options flags that can be used with with the rsync command; we’ll cover the most used and most useful here.
-r
Using the -r flag for in the options section will let you recursively go through a folder and synchronize its contents. Use this option if you are planning on synchronizing files within folders. The command will be specified like this:
rsync -r SourceDirectoryPath DestinationDirectoryPath
-a
The -r command will not preserve source file attributes (like timestamps, symbolic links, file and directory permissions, or owners and groups of files and directories). However, using the -a command will do all of that, and will use recursive mode to compare and synchronize directories of files. Use this command if you have files within folders, and would like to preserve the file and directory attributes in the destination path. This command will be specified like this:
rsync -a SourceDirectoryPath DestinationDirectoryPath
(blank)
Leave the options field blank to only compare and synchronize a single file at the source path. This command would be specified like this:
rsync SourceFilePath DestinationFilePath
-dr
Lets say you’re working on a project that uses the same folder structure as a previous project, and you don’t want to manually recreate all of the directories that you used. Well, you can use the -dr option to only synchronize and replicate the directory structure of your source directory at your destination. This command would be specified like this:
rsync -dr SourceDirectoryPath DestinationDirectoryPath
-v
Using the -v option (for verbose mode), you can see all of the synchronization process printed to the screen while the process is going on (this is used in the example below). Simply specify your options above as you would normally, then add a space, then -v, then another space before adding your source directory/file path, like so:
rsync -options -v SourceDirectoryPath DestinationDirectoryPath
Example

In our example, we want to replicate the directory structure of our Documents folder on our Desktop for an upcoming project. We’ll use the -dr command above along with the -v option to see what’s going on while rsync is doing its magic.
First, we’ll open the Terminal (located in Applications/Utilities), and then type the following command to perform the rsync:
rsync -dr -v ~/Documents ~/Desktop
The -dr will replicate the directory structure of the Documents folder inside of the Desktop folder. The -v (or verbose mode) option will let us see the output while the rsync command is run. Press enter to run the command.
Cory Bohon is a freelance technology writer, indie Mac and iOS developer, and amateur photographer. Follow this article's author on Twitter.