Shell Script to Synchronize Folder with External HDD

applescriptdata synchronizationterminal

I have a Back-Up HDD (not TimeMachine) with only very important data backed-up there. Every once in a while, I connect the external hard disk and sync the files per hand. As the files I am syncing are always the same, it came to my mind that I could automate the whole process as a shell script.

However, I could not find a helpful command that compares A to B (which will then ask me to sync if sync is necessary). I have found rsync but the documentations did not explain how to synchronise a single directory.

I also haven't figured out how to change directory to an external HDD connected via USB.

Best Answer

This is easy to accomplish using rsync. The easiest route is to open terminal and type rsync -r and that drag the source folder to the window, followed by the destination folder. -r stands for recursive, so it will look inside the folder and its subfolders. I just synced a folder on my desktop and a mounted USB drive with this command:

rsync -r /Users/My-User/Desktop/Test_folder /Volumes/My-USB-Drive/Test_on_USB

It actually creates the Test_folder inside the Test_on_USB folder. I'm not an expert on all the options, but you can find them here: http://everythinglinux.org/rsync/

Let me know if that accomplishes what you're after.