Skip to content

Instantly share code, notes, and snippets.

@shello
Forked from senko/onchange.sh
Last active August 29, 2015 14:02
Show Gist options
  • Save shello/13be1a5fe43f0ce0d9d2 to your computer and use it in GitHub Desktop.
Save shello/13be1a5fe43f0ce0d9d2 to your computer and use it in GitHub Desktop.

Revisions

  1. shello revised this gist Jun 11, 2014. 1 changed file with 19 additions and 15 deletions.
    34 changes: 19 additions & 15 deletions onchange.sh
    Original file line number Diff line number Diff line change
    @@ -1,39 +1,43 @@
    #!/bin/bash
    #
    # Watch current directory (recursively) for file changes, and execute
    # a command when a file or directory is created, modified or deleted.
    # 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 protected]>
    # Modified from the original by: Senko Rasic <[email protected]>
    # https://gist.github.com/senko/1154509
    #
    # Requires Linux, bash and inotifywait (from inotify-tools package).
    # Requires:
    # bash
    # fswatch (https://github.com/alandipert/fswatch)
    #
    # 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.
    # 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:
    # 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
    #
    # onchange.sh rsync -avt . host:/remote/dir
    #
    # Released to Public Domain. Use it as you like.
    #

    EVENTS="CREATE,CLOSE_WRITE,DELETE,MODIFY,MOVED_FROM,MOVED_TO"
    EXCLUDE="/\.git/"
    WAIT_TIME=1

    if [ -z "$1" ]; then
    echo "Usage: $0 cmd ..."
    exit -1;
    fi

    inotifywait -e "$EVENTS" -m -r --format '%:e %f' . | (
    fswatch -e $EXCLUDE -r . | (
    WAITING="";
    while true; do
    LINE="";
    read -t 1 LINE;
    read -t $WAIT_TIME LINE;
    if test -z "$LINE"; then
    if test ! -z "$WAITING"; then
    echo "CHANGE";
    @@ -45,7 +49,7 @@ inotifywait -e "$EVENTS" -m -r --format '%:e %f' . | (
    done) | (
    while true; do
    read TMP;
    echo $@
    $@
    echo \[`date`\] $@
    "$@"
    done
    )
  2. @senko senko created this gist Aug 18, 2011.
    51 changes: 51 additions & 0 deletions onchange.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    #!/bin/bash
    #
    # 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 protected]>
    #
    # 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.
    #
    # Installation:
    # 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.
    #

    EVENTS="CREATE,CLOSE_WRITE,DELETE,MODIFY,MOVED_FROM,MOVED_TO"

    if [ -z "$1" ]; then
    echo "Usage: $0 cmd ..."
    exit -1;
    fi

    inotifywait -e "$EVENTS" -m -r --format '%:e %f' . | (
    WAITING="";
    while true; do
    LINE="";
    read -t 1 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 $@
    $@
    done
    )