Created
September 14, 2022 17:40
-
-
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.
This file contains 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/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