Skip to content

Instantly share code, notes, and snippets.

@mpapi
Last active September 29, 2025 15:35
Show Gist options
  • Select an option

  • Save mpapi/4656389 to your computer and use it in GitHub Desktop.

Select an option

Save mpapi/4656389 to your computer and use it in GitHub Desktop.

Revisions

  1. mpapi revised this gist Jan 28, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion whenever.sh
    Original file line number Diff line number Diff line change
    @@ -22,4 +22,4 @@ COMMAND="$2"

    inotifywait -q --format '%f' -m -r -e close_write . \
    | grep --line-buffered $PATTERN \
    | xargs -I{} -r sh -c "echo [$(date -Is)] $COMMAND && $COMMAND"
    | xargs -I{} -r sh -c "echo [\$(date -Is)] $COMMAND && $COMMAND"
  2. mpapi created this gist Jan 28, 2013.
    25 changes: 25 additions & 0 deletions whenever.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    #!/bin/sh

    #
    # Usage: whenever.sh [pattern] [command]
    #
    # Executes a command whenever files matching the pattern are closed in write
    # mode. "{}" in the command is replaced with the matching filename (via xargs).
    # Requires inotifywait from inotify-tools.
    #
    # For example,
    #
    # whenever.sh '\.md$' 'markdown -f $(basename {} .md).html {}'
    #
    # This runs "markdown -f my-document.html my-document.md" whenever
    # my-document.md is saved.
    #

    set -e -u

    PATTERN="$1"
    COMMAND="$2"

    inotifywait -q --format '%f' -m -r -e close_write . \
    | grep --line-buffered $PATTERN \
    | xargs -I{} -r sh -c "echo [$(date -Is)] $COMMAND && $COMMAND"