I found this command to copy the content of source to a destination folder.
rsync -r source/* destination
What I would really like is to have this command run automatically each time a file is added to my source folder.
Is this possible?
Ask Different is a question and answer site for power users of Apple hardware and software. It only takes a minute to sign up.Sign up to join this community
You could use, Automator and Folder actions to watch the folder and run the script.
When launching Automator and choosing New, one of the options given is to make a 'Folder Action'. At the top of the screen after selecting Folder Action you are given the option to choose a folder to 'watch'.
What I think you would want to do in your case is to then select 'Utilities' on the leftmost column, and then drag 'Run Shell Script' from the second column into the workflow window.
Here is where you would write whatever script you would like to run when changes are made to the folder. In this case your rsync script.
One quick and dirty solution to this question that is portable across UNIX platforms (not specific to OSX, but may be used on HP-UX, Solaris, Linux, etc.) is to call rsync from the user's crontab, and let rsync's folder synchronization logic itself determine if the folder has been changed or not. One refinement to using this portable solution is to wrap the call to rsync with a date test between a touched file (actually its modification time) and the modification time of the folder that is to be synced, but this is not necessary unless the folders to be synced are very large (deep and/or wide). A single folder with several subfolders is ideal for this solution.
An entry for the crontab that will perform the "quick and dirty" case at a minute polling interval is:
# run sync every minute: * * * * * /usr/bin/rsync -avz $HOME/source_path $HOME/target/path 1>> $HOME/.my_sync.log 2>&1
That should do it for the quick and dirty case - in other words, the folders will be kept up-to-date within a minute of making a change. Check the ~/.my_sync.log file for execution statuses - this file will grow without bounds over time, so once correct operation has been determined, change ">>" to ">" to at least log the most recent rsync status.
Another pointer is that the -z switch to rsync should be used when syncing over a network. The -z switch will cause compression to be used for the network file transfers. When using rsync to sync between folders on filesystems local to a server, the -z switch may usually be omitted.
Note about using the crontab:
If you cannot edit your crontab with:
$ crontab -e
Then you'll have to give yourself permission to have a crontab. See the man page for crontab. In OSX, this is what needs to be done - the change is slightly different on linux systems: In a nutshell, make sure your user id is in /usr/lib/cron/cron.allow or that /usr/lib/cron/cron.allow does not exist. Also, make sure your user id is not in /usr/lib/cron/cron.deny.
The tool I would use to monitor your folders is Hazel. It would be excellent to kick off the script and you might end up using it for many more folders over time as you think of things you could automate.