14 December 2009
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 :)
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.
rsync -varE --progress /folder-source/ /folder-destination/
Syncs with progress, file list, resource forks and everything! rsync rocks!!! Mac's rocks !!!
Just what I was looking for. Worked the first time.
thanks for the excellent share.
rsync -varE --progress /folder-source/ /folder-destination/
very nice that added detailed progres
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.
how can you do this action. can you please add examples?