11

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?

Tegan Snyder
  • 213
  • 1
  • 2
  • 6
  • 1
    This question as-is is borderline a software recommendation, which aren't generally allowed on [su]. 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 06 '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 06 '12 at 23:26
  • 1
    @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 06 '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 07 '12 at 02:22

2 Answers2

22

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.

Pierre Carrier
  • 986
  • 8
  • 9
  • 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 07 '12 at 02:20
  • 1
    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 07 '12 at 08:18
  • This looks encouraging. I thinking I could run ./fswatch /some/dir ./some/dir/bash_script.sh – Tegan Snyder Jul 09 '12 at 02: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
2

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
http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.superuser.445907</string>
    <key>Program</key>
    <string>/Users/username/script</string> <!-- ~/ doesn't work -->
    <key>WatchPaths</key>
    <array>
        <string>/Users/username/Folder/</string>
    </array>
    <key>ThrotteInterval</key>
    <integer>0</integer> <!-- run at most every 0 seconds, by default 10 -->
</dict>
</plist>

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.

Lri
  • 40,894
  • 7
  • 119
  • 157
  • Interesting approach. I will take a look at it and get back to you. Thanks for the feedback. – Tegan Snyder Jul 09 '12 at 02: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 '16 at 18:04