Skip to content

Instantly share code, notes, and snippets.

@eduardosilva
Created September 14, 2022 17:40
Show Gist options
  • Save eduardosilva/3b7cab2eb92a778c3615915b9f4ca661 to your computer and use it in GitHub Desktop.
Save eduardosilva/3b7cab2eb92a778c3615915b9f4ca661 to your computer and use it in GitHub Desktop.
A polling based script to watch a directory and execute a command if something change.
#!/bin/bash
# A polling based script to watch a directory and execute a command if something change.
# Reference: https://www.baeldung.com/linux/command-execute-file-dir-change
# Example: ./watcher.sh ~/Documents "echo 'Detected the modification of a file or directory'"
DIR_TO_WATCH=${1}
COMMAND=${2}
trap "echo Exited!; exit;" SIGINT SIGTERM
while [[ 1=1 ]]
do
watch --chgexit -n 1 "ls --all -l --recursive --full-time ${DIR_TO_WATCH} | sha256sum" && ${COMMAND}
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment