Skip to content

Instantly share code, notes, and snippets.

@JarredMack
Last active August 7, 2023 11:45
Show Gist options
  • Save JarredMack/b33900d64c0e448fd5ff1e1bd760789e to your computer and use it in GitHub Desktop.
Save JarredMack/b33900d64c0e448fd5ff1e1bd760789e to your computer and use it in GitHub Desktop.
Bash file watcher
#!/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