« Back to Index

[Simple Bash File Watcher]

View original Gist on GitHub

Tags: #tags: bash, watcher, polling

Simple Bash File Watcher.sh

if [ "$#" -ne 1 ]; then
  echo "ERROR: directory is required."
  exit 1
fi

DIRECTORY=$1

checkSum1=""

while [[ true ]]
do
  checkSum2=`find $DIRECTORY -type f \( -iname "*.go" \) -exec md5 {} \;`
  
  if [[ $checkSum1 != $checkSum2 ]] ; then
	# do something like run a test suite
    
    # update last seen checksum
    checkSum1=$checkSum2
  fi
  
  # don't overload the system
  sleep 2
done