-
-
Save makhnanov/42ce28f1efeda5c5cb9ce799a256b667 to your computer and use it in GitHub Desktop.
Bash file watcher
This file contains hidden or 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
#!/bin/sh | |
############ | |
# Usage | |
# Pass a path to watch, a file filter, and a command to run when those files are updated | |
# | |
# Example: | |
# watch.sh "node_modules/everest-*/src/templates" "*.handlebars" "ynpm compile-templates" | |
############ | |
watch() { | |
WORKING_PATH=$(pwd) | |
DIR=$1 | |
FILTER=$2 | |
COMMAND=$3 | |
chsum1="" | |
while [[ true ]] | |
do | |
chsum2=$(find -L $WORKING_PATH/$DIR -type f -name "$FILTER" -exec md5 {} \;) | |
if [[ $chsum1 != $chsum2 ]] ; then | |
echo "Found a file change, executing $COMMAND..." | |
$COMMAND | |
chsum1=$chsum2 | |
fi | |
sleep 2 | |
done | |
} | |
watch "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment