Created
January 3, 2017 14:07
-
-
Save BrandonRomano/f27221f0fd66187a2e57739d953e34cf to your computer and use it in GitHub Desktop.
Watch a directory + execute a command on update
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 | |
# | |
# This script will watch a directory, and execute an arbitrary | |
# command when there is a change to a file within it. | |
# | |
# @param $1: The directory to watch for any changes | |
# @param $2: The command to run when there is a change | |
################################################## | |
current_md5="" | |
previous_md5="" | |
traverse_directory() { | |
for file in $(ls $1); do | |
local file_path="$1/$file" | |
if [[ -f "$file_path" ]]; then | |
current_md5+=$(md5 "$file_path") | |
else | |
traverse_directory "$file_path" | |
fi | |
done | |
} | |
while true; do | |
traverse_directory "$1" | |
if [[ $previous_md5 != "" ]]; then | |
if [[ "$current_md5" != "$previous_md5" ]]; then | |
$2 | |
fi | |
fi | |
previous_md5="$current_md5" | |
current_md5="" | |
sleep .5 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment