Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
Watch Filesystem in Real Time on OS X and Ubuntu

I'm looking for a efficient method to monitor a local directory in OSX and if any files have been changed in that directory run a bash script to commit the files to github.

Any recommend tools for monitoring a directory for file changes, then triggering an action, i.e. a bash script?

share|improve this question

marked as duplicate by slhck, Nifle, Daniel Beck, 8088, Randolph West Jul 22 '12 at 2:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

This question as-is is borderline a software recommendation, which aren't generally allowed on Super User. Try rephrasing it to ask your root question, "How can I automatically commit changes and push from a git repo whenever files are added/deleted/changed?" – Darth Android Jul 6 '12 at 21:47
@DarthAndroid, accd to the FAQ, this question is fine as it stands. The OP is not asking for shopping or buying advice. – ephsmith Jul 6 '12 at 23:26
@eph The OP is asking for recommendations for something, which is rather off topic. The main problem though is that they're not explaining their problem and only ask for their attempted solution. – slhck Jul 6 '12 at 23:57
The problem is simple. Monitor a directory for changes and commit those changes to a repository. It's all in the OP's question. The link you provided has the answer. Good call. – ephsmith Jul 7 '12 at 2:22
up vote 11 down vote accepted

Using fswatch from your repository:

fswatch . 'git commit -avm "snapshot at ${date}"'

This simple example would only catch changes to files already in the repository.

share|improve this answer
This can be done with folder actions which are built in to OS X. See the answer via the link that slhk provided above. – ephsmith Jul 7 '12 at 2:20
You could expand your answer by adding an example explaining what the OP wants to achieve. Just posting a link is not encouraged. – slhck Jul 7 '12 at 8:18
This looks encouraging. I thinking I could run ./fswatch /some/dir ./some/dir/ – Tegan Snyder Jul 9 '12 at 2:08
+1 - I tried fswatch after reading your answer. Excellent utility - very similar to inotify for linux and it makes use of OS X's native fsevent – cwd Aug 28 '12 at 19:10

One option would be to just use launchd. Save a property list like this as ~/Library/LaunchAgents/com.superuser.445907.plist, and load it with launchctl load ~/Library/LaunchAgents/com.superuser.445907.plist or by logging out and back in.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN>
<plist version="1.0">
    <string>/Users/username/script</string> <!-- ~/ doesn't work -->
    <integer>0</integer> <!-- run at most every 0 seconds, by default 10 -->

Launchd only registers changes to files when they are saved atomically, or deleted and recreated every time they are saved. Most OS X applications perform atomic saves by default, but for example TextMate and vim don't. Changes in subfolders of watched folders aren't detected.

launchctl unload $path && launchctl load $path applies changes to a plist.

See man launchd and man launchd.plist for more information.

share|improve this answer
Interesting approach. I will take a look at it and get back to you. Thanks for the feedback. – Tegan Snyder Jul 9 '12 at 2:08
This method definitely has its limitations. You can't detect changes in subfolders of watched folders. This technique also doesn't tell you which file had an event (new file, modification, deletion, etc.) -- just that the path had an event. – Volomike Mar 23 at 18:04

Not the answer you're looking for? Browse other questions tagged .