|# Watch current directory (recursively) for file changes, and execute|
|# a command when a file or directory is created, modified or deleted.|
|# Written by: Senko Rasic <email@example.com>|
|# Requires Linux, bash and inotifywait (from inotify-tools package).|
|# To avoid executing the command multiple times when a sequence of|
|# events happen, the script waits one second after the change - if|
|# more changes happen, the timeout is extended by a second again.|
|# 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.|
|if [ -z "$1" ]; then|
|echo "Usage: $0 cmd ..."|
|inotifywait -e "$EVENTS" -m -r --format '%:e %f' . | (|
|while true; do|
|read -t 1 LINE;|
|if test -z "$LINE"; then|
|if test ! -z "$WAITING"; then|
|done) | (|
|while true; do|
Feb 21, 2016
Why can't I kill it with ctrl + c?
Jul 29, 2017
Nice script! Thanks
I'm using rsync to synchronize my local dir to a remote server's dir, which is working great, but I have to enter my password on every sync. Any ideas on how I could sync fully automatically (without having to enter the password)?
Unfortunately I can't setup an ssh key and use it to authenticate, since the remote is shared and crappy..
Nov 4, 2017
Cool idea, but how would you pass extra arguments with quotes? say i want
onchange.sh rsync -a --exclude='folder with space' host:/path?
Mar 12, 2018
Thanks for the script! I'm having trouble figuring out where the 1 second is defined, as I'd like to extend it.
Sep 28, 2018
read -t 1 LINE tries to read one line, but with a one second timeout.
read is a
bash builtin, and is documented on the
bash man page.
Jul 15, 2021
Thanks! I modified this script, now it can use entered path, not current directory only =)
Changes there : https://gist.github.com/AVAtarMod/e8bb8ee64cdb009f68d2f70615632b62
Because not everyone runs Mac?