#!/usr/bin/env zsh error(){print -- $* >&2; exit 1} (($#<2)) && error "usage: ${0:t} [once] cmd dirs..." ONCE=0 [[ $1 = "once" ]] && { ONCE=1; shift } (($#<2)) && error "usage: ${0:t} [once] cmd dirs..." cmd="$1" shift typeset -a listDir listDir=( $@ ) isRecentlyModified() { local fic="$1" local mtime=0 currenttime=$(date +"%s") case $(uname) in Darwin) mtime=$(stat -f %m $fic);; Linux) mtime=$(stat --printf %Y $fic);; *) mtime=$(stat --printf %Y $fic);; esac (( $currenttime - $mtime < 2 )) } checkfile=".last_watched" ((ONCE == 0)) && \ [[ -e $checkfile ]] && \ isRecentlyModified $checkfile && \ error "Wait at least 2 second if you killed the process" execIfChanged() { isRecentlyModified $1 && { print -- "$cmd $1"; eval "$cmd $1"} } typeset -A listFic while true; do listFic=() for d in $listDir; do if [[ -d $d ]]; then for fic in $d/**/*(.); do execIfChanged $fic done else execIfChanged $d fi done ((ONCE == 1)) && break touch $checkfile sleep 2 done