Skip to content

Instantly share code, notes, and snippets.

@xerp
Forked from mikesmullin/watch.sh
Last active October 12, 2015 08:10
Show Gist options
  • Save xerp/be149568dc900bf4ddbb to your computer and use it in GitHub Desktop.
Save xerp/be149568dc900bf4ddbb to your computer and use it in GitHub Desktop.
watch is a linux bash script to monitor file modification recursively and execute bash commands as changes occur
#!/usr/bin/env bash
# script: watch
# authors: Mike Smullin <mike@smullindesign.com>, Santiago de Pedro <santiago.pedro23@gmail.com>
# license: GPLv3
# description:
# watches the given path for changes
# and executes a given command when changes occur
# usage:
# watch <path> <cmd...>
#
path=$1
if [[ ! $path ]] ; then
path=`pwd`
fi
shift
cmd=$*
if [[ ! $cmd ]] ; then
cmd="NONE"
fi
sha=0
update_sha() {
sha=`ls -lR --time-style=full-iso $path | sha1sum`
}
update_sha
previous_sha=$sha
build() {
if [[ $cmd != "NONE" ]] ; then
echo -en "--> building...\n\n"
$cmd
fi
}
compare() {
update_sha
if [[ $sha != $previous_sha ]] ; then
echo -en "\n--> change DETECTED\n"
build
previous_sha=$sha
else
echo -n .
fi
}
trap build SIGINT
trap exit SIGQUIT
echo -e "--> Press Ctrl+C to force build, Ctrl+\\ to exit."
echo -e "--> command [$cmd]"
echo -e "--> watching [$path]"
while true; do
compare
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment