-
-
Save shello/13be1a5fe43f0ce0d9d2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
# | |
# Watch current directory (recursively) for file changes, and execute a command | |
# when a file or directory is created, modified or deleted. | |
# | |
# Modified from the original by: Senko Rasic <[email protected]> | |
# https://gist.github.com/senko/1154509 | |
# | |
# Requires: | |
# bash | |
# fswatch (https://github.com/alandipert/fswatch) | |
# | |
# To avoid executing the command multiple times when a sequence of events | |
# happen, the script waits WAIT_TIME seconds after the change - if more changes | |
# happen, the timeout is extended by a second again. | |
# | |
# Installation (at your own risk): | |
# chmod a+rx onchange.sh | |
# sudo cp onchange.sh /usr/local/bin | |
# | |
# Example use - rsync local changes to the remote server: | |
# | |
# onchange.sh rsync -avt . host:/remote/dir | |
# | |
# Released to Public Domain. Use it as you like. | |
# | |
EXCLUDE="/\.git/" | |
WAIT_TIME=1 | |
if [ -z "$1" ]; then | |
echo "Usage: $0 cmd ..." | |
exit -1; | |
fi | |
fswatch -e $EXCLUDE -r . | ( | |
WAITING=""; | |
while true; do | |
LINE=""; | |
read -t $WAIT_TIME LINE; | |
if test -z "$LINE"; then | |
if test ! -z "$WAITING"; then | |
echo "CHANGE"; | |
WAITING=""; | |
fi; | |
else | |
WAITING=1; | |
fi; | |
done) | ( | |
while true; do | |
read TMP; | |
echo \[`date`\] $@ | |
"$@" | |
done | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment