joseraya
11/26/2012 - 12:31 PM

Watch a directory and execute something whenever a file is changed

Watch a directory and execute something whenever a file is changed

#!/bin/sh

check() {
    chsum1=chsum2

    while [[ true ]]
    do
        chsum2=`find . -type f -exec md5 {} \;`
        if [[ $chsum1 != $chsum2 ]] ; then
            echo "Change detected ..."
            eval $*
            chsum1=`find . -type f -exec md5 {} \;`
        fi
        sleep 2
    done
}

check $*