Skip to content

Instantly share code, notes, and snippets.

@jforge
Forked from JarredMack/watch.sh
Created July 28, 2023 18:08
Show Gist options
  • Save jforge/442fabbf84785e59f2cbb98a4031eef6 to your computer and use it in GitHub Desktop.
Save jforge/442fabbf84785e59f2cbb98a4031eef6 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