Over a million developers have joined DZone.

Simple Files Watch Associated To Commands

·
The is the scenario: you want to run a command wherever a file (or files) is modified. 

This is the most basic approach (you will need inotify):


#!/bin/bash
# Usage: onchange.sh "COMMAND" FILE1 ... [FILEN]
set -e
RUNFIRSTLOOP=1

COMMAND=$1
shift
test "$RUNFIRSTLOOP" && bash -c "$COMMAND" || { echo "retcode: $?"; }

while inotifywait "$@"; do
  bash -c "$COMMAND" || { echo "retcode: $?"; }
done
Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}