-
-
Save sc0ttj/d4c219453a15620474fea019958af0ff to your computer and use it in GitHub Desktop.
watchdo: Linux bash script to monitor file modifications recursively & execute bash commands as changes occur
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# script: watchdo | |
# author: Mike Smullin <mike@smullindesign.com> | |
# modified: Jared Blalock <mb@molotovbliss.com> | |
# possible use for monitoring .less changes to compile to .css | |
# license: GPLv3 | |
# description: | |
# watches the given path for changes | |
# and executes a given command when changes occur | |
# usage: | |
# watch <path> <cmd...> | |
# | |
path=$1 | |
shift | |
cmd=$* | |
sha=0 | |
echo -en "Generating checksum from $path ...\n\n" | |
update_sha() { | |
# NOTE: If checksum generation is slow try a smaller size directory. | |
sha=`ls -lR --time-style=full-iso $path | sha1sum` | |
#sha=`find $path -type f -exec stat -c '%y %s %n *' '{}' + | sha1sum` | |
} | |
update_sha | |
previous_sha=$sha | |
build() { | |
echo -en "Executing: $cmd ...\n\n" | |
$cmd -s | |
echo -en "Resuming watch: $path " | |
} | |
compare() { | |
update_sha | |
if [[ $sha != $previous_sha ]] ; then | |
echo -n " >> Change detected! << \n\n" | |
build | |
previous_sha=$sha | |
else | |
echo -n . | |
fi | |
} | |
#trap build SIGINT | |
#trap exit SIGQUIT | |
echo -en " Press Ctrl+C to exit.\n" | |
echo -en " ... 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