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.
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...
Recent posts
- Thursday, 21. january 2010. Find files larger than X and delete them in Mac OS/Unix terminal
- Tuesday, 15. december 2009. Bananica.com - Finally!
- Monday, 14. december 2009. MySQL Find and Replace with helper tool that generates SQL for you
- Monday, 14. december 2009. Synchronize two folders on a Mac with Automator and Rsync
- Monday, 14. december 2009. Synchronize two folders on a Mac and other Unix Systems with Rsync
- Sunday, 13. december 2009. Add all new files and folders to subversion with one line in terminal
Recent comments
-
Wednesday, 24. february 2010. karol commented on Add all new files and folders to subversion with one line in terminal Great! However, this doesn't work if your file names contain spaces.
I managed to compile several snippets from the net and came up with this:
svn status | grep "^?" | awk '{$1="";$0=substr($0,2)}1' | sed -e "s,[^.],\'&," -e "s,\$,\'," | xargs svn add
Phew! Works in Snow Leopard. - Tuesday, 23. february 2010. eMancu commented on Synchronize two folders on a Mac and other Unix Systems with Rsync Thanks a lot! I was looking for --delete example :)
-
Wednesday, 13. january 2010. annmarie commented on Ambium Ja, meni je moja spletna stran noro dobra. Na svetovnem nivoju, ni kaj.
Thanks a lot! I was looking for --delete example :)
23. Feb 2010 at 4:35 AM