Skip to content

Instantly share code, notes, and snippets.

@nkpart
Forked from markhibberd/dirwatch
Created February 9, 2011 06:52
Show Gist options
  • Save nkpart/818059 to your computer and use it in GitHub Desktop.
Save nkpart/818059 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# NAME
# dirwatch - run command on file modification
#
# SYNOPSIS
# dirwatch [-m marker] command dir ...
#
# DESCRIPTION
# dirwatch uses a touch file to track whether files
# in a list of directories are modified. In the case
# where there are no modified files, it will exit. In
# the case where there are modified files it will
# execute the specified command.
#
# Executing dirwatch will execute the check once only,
# combine with the `watch` command to check repeatedly.
#
# LIMITATIONS
# dirwatch currently has very primitive command line
# argument handling, so if the command has any arguments
# they entire command line (including arguments) must be
# quoted.
#
# EXAMPLES
# Saying hello world if a file has been modified:
# $ dirwatch "echo hello world" some/directory
#
# Running make when a file is modified, using watch
# to repeat every 2 seconds:
# $ watch dirwatch make some/directory
#
#
if [ "$1" = "-m" ]; then
marker="$2"; shift; shift
else
marker=.marker
fi
if [ $# -lt 2 ]; then
echo "usage: `basename $0` [-m marker] command dir ..." >&2
exit 1
fi
command="$1"; shift
if [ -f "$marker" ]; then
changed=`find "$@" -newer $marker | wc -l`
if [ "$changed" -ne 0 ]; then
echo "[$changed] files have been updated - executing command."
touch "$marker"
exec $command
else
echo "[0] files have been updated."
fi
else
echo "no marker [$marker] - executing command."
touch "$marker"
exec $command
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment