My version does not fork a process. It writes out lines on stdout that provide information about changed files. This is a lot more precise and informative.
This is a small program using the Mac OS X FSEvents API to monitor a directory.
When an event about any change to that directory is received, the specified
shell command is executed by /bin/bash
.
If you're on GNU/Linux,
inotifywatch (part of the
inotify-tools
package on most distributions) provides similar
functionality.
You need to be on Mac OS X 10.5 or higher with Developer Tools
installed. Then, run make
. Copy the resulting fswatch
binary to
a directory on your $PATH
.
./fswatch /some/dir "echo changed"
This would monitor /some/dir
for any change, and run echo changed
when a modification event is received.
In the case you want to watch multiple directories, just separate them with colons like:
./fswatch /some/dir:/some/otherdir "echo changed"
fswatch
can be used with rsync
to keep a remote directory in sync
with a local directory continuously as local files change. The
following example was contributed by
Michael A. Smith:
#!/bin/sh
##
# Keep local path in sync with remote path on server.
# Ignore .git metadata.
#
local=$1
remote=$2
cd "$local" &&
fswatch . "date +%H:%M:%S && rsync -iru --exclude .git --exclude-from=.gitignore --delete . $remote"
This code was adapted from the example program in the FSEvents API documentation.