Synchronize two folders on a Mac and other Unix Systems with Rsync

From terminal

One of the great things with Mac OS X is that you have many of the cool tools from UNIX world. One of them is definitely Rsync. I'll show you how to use it to synchronise files from of two different folders from terminal and using mighty automator.

Make a backup before you try it out on your files!
I suggest you make some dummy folders and fill them with some random files to test it out first.

Here is the simple example of syncing two folders from terminal:

rsync -va --delete ~/Folder1/ ~/Folder2/

What this line does is comparing contents of Folder1 with what you have in Folder2 and copies all new and newer files and folders from Folder1 to Folder2.

This "--delete" option tells it that you want also to delete files and folders from Folder2 that do not exist in Folder1. So in case that you'd like to leave those files/folders in Folder2, you just omit this part.

Option "-va" hides two options actually, "v" stands for Verbose, so it will inform you what's being done.

And "a" stands for archive mode, which means that the operation will be recursive so it looks inside of all subfolders you have in Folder1 and Folder2, and that it will keep the permissions intact and also some other things.

You can always check the manual of Rsync by typing "man rsync" in terminal.

In case you don't want to sync some particular files or folders you can specify it with "--exclude" option. So for example if you don't want to sync file named "leave_me_alone.txt" you just add this "--exclude='leave_me_alone.txt'", so your whole command would look like:

rsync -va --exclude='leave_me_alone.txt' --delete ~/Folder1/ ~/Folder2/

And you can add more files by just adding another "--exclude":

rsync -va --exclude='leave_me_alone.txt' --exclude='not_going_anywhere.doc' --delete ~/Folder1/ ~/Folder2/

But the real power comes from the fact that you can use patterns, so if you have a lot of let's say Microsoft Word files that you'd like to ignore for some reason, you can do this:

rsync -va --exclude='*.doc' --delete ~/Folder1/ ~/Folder2/

And now you can go on and see how can you use all this stuff in Automator and make it work without terminal...

Comments

Thanks a lot! I was looking for --delete example :)

eMancu 23. Feb 2010 at 4:35 AM

Be aware that the version of rsync that comes with OS X (10.6) ignores resource forks and other metadata unless you supply -E or --extended-attributes. That’s not perfect either and I tried to post further details but this site wouldn’t allow me.

Jamie 27. Jun 2010 at 1:11 AM

rsync -varE --progress /folder-source/ /folder-destination/

Syncs with progress, file list, resource forks and everything! rsync rocks!!! Mac's rocks !!!

Moschos 14. Dec 2010 at 10:02 PM

Just what I was looking for. Worked the first time.

Sharon 9. Jan 2011 at 4:48 AM

thanks for the excellent share.

rsync -varE --progress /folder-source/ /folder-destination/

very nice that added detailed progres

ram 29. Jan 2012 at 9:30 PM

You can remove spaces, replace spaces with underscore, Uppercase/lowercase filename, add a prefix/suffix, remove/replace strings and also catalog files by adding an incremental number to the file name.

perez murphy 29. Nov 2016 at 9:42 AM

how can you do this action. can you please add examples?

How? 15. Dec 2016 at 4:17 AM Reply to: perez murphy

Leave a comment

*
Leave your e-mail if you would like to get reply from me. Your e-mail will not be shown on my web page nor will I use it for anything other than replying to your question/comment.
**
Leave your website address if you want an extra link pointing to it ;)