Keep your files in sync using a simple command line utility.
Macs have long been equipped with a file syncing utility unknown by most users. Remote sync, or rsync, is a way to synchronize files and directories through the command line interface on Unix-based machines. This easy-to-use utility is commonly used for backing up your data, but can synchronize files for any other purpose you choose to use it for. Remote sync can be better than other backup methods because of its speed, and because it doesn’t require any special permissions to execute an rsync command. With just a small knowledge of the command line, you can be backing up in no time with rsync.
Difficulty Level: Medium
What You Need:
>> Mac OS X
>> Files to synchronize
>> Some Command Line Knowledge
1. Simple File and Directory Synchronization
One of the simplest things you can do with rsync is to backup files and directory structure. In order to use rsync, you must use the following command format when specifying information in Terminal:
rsync –options SourceDirectory DestinationDirectory
There are many different options that will cause rsync to perform different operations during the file synchronization process. The SourceDirectory text will be replaced with the directory path to the files and directories that will be used during the file sync process, and the DestinationDirectory text will be replaced with the directory that the source files and directories will be copied to.
To begin using rsync, open Terminal (located in /Applications/Utilities), and type in the following command:
Next, open a Finder window and drag and drop the folder of the source location (the folder containing files you wish to backup) in the Terminal window. You will notice that the directory location will be inputted to the Terminal window.
Your rsync command will look something like this when you are done inputting all of the options.
Finally, locate the destination location in the Finder (the folder where you wish the source files to be synchronized to) and drag and drop it in the Terminal window. That directory location will also be inputted in the Terminal window, making your rsync command look something like this:
rsync –r /Users/Cory/Documents /Users/Cory/Desktop/Backup
Press enter in the Terminal window to have the source directory contents copied exactly to the destination directory. In this case, our Documents folder will be copied into a Backup folder on our Desktop.
2. Preserving Timestamps During Synchronization
When you use rsync, the files that get copied will have a modification date of the same date that the rsync command was run. To overcome this, there is another option that you can specify in the rsync command that will preserve the timestamps during the synchronization process.
Without preserving the timestamp, the files will display the modification date and time as the time that the rsync command was run.
To do this, use the –a option instead of –r, like we used in the command above. The –a option will use recursive mode, preserve any symbolic links, preserves file and directory permissions, preserves the timestamp, and preserve the owner and group.
To do this, type the following command, and then drag and drop the folders in the appropriate order as we did above.
rsync –a SourceDirectory DestinationDirectory
3. Synchronize Only One File
You may occasionally only wish to sync one file instead of an entire directory and all of the files contained within. Once again, rsync can fulfill this need by simply removing any options, and specifying a file for the source instead of a directory.
Once you press enter, the synchronized file will appear on your Desktop.
Let’s say that we have a text file called “MyDocument.txt” in our Documents directory that we wish to synchronize to my Desktop, we would type the following command into Terminal and press enter:
Notice that we didn’t put any –options, such as –a or –r because we’re not using the features that those options provide.
4. Synchronize Only Directories
If you are working on a project that requires you to replicate the directory structure, but not the files, rsync has the ability to help you here as well. You only need to use the –dr option:
rsync –dr SourceDirectory DestinationDirectory
When you press the enter key, the directories and subdirectories in the source directory will be replicated on the desktop.
So, if you were to replicate the directory structure of a folder called “MyFolder” in your Documents folder on your Desktop, you could specify the following command:
rsync –dr /Users/Cory/Documents/MyFolder
5. View rsync Progress During Synchronization
If you are using rsync with a large group of files, you may wish to view the synchronization progress for each file as it happens. Once again, another option is there to help. Putting rsync in verbose mode will cause the utility to output, line-by-line, the copy progress of files and directories. Simply use the –v option.
Verbose mode will show you exactly which files rsync is working on.
To see the verbose mode output, you can use the following command:
rsync –v –r SourceDirectory DestinationDirectory