User Tools

Site Tools


directory:run_a_command_upon_files_or_directories_changes

This is an old revision of the document!


Directory - Run a Command Upon Files or Directories Changes

Use either inotifywait or iwatch.

For example

inotifywait -m -e modify,create -r /yourpath && \
chmod 644 file; && \
chown blah:blah file;

NOTE:

  • -m, –monitor: Instead of exiting after receiving a single event, execute indefinitely. The default behavior is to exit after the first event occurs.
  • -r, –recursive: Watch all sub-directories of any directories passed as arguments. Watches will be set up recursively to an unlimited depth.
    • Symbolic links are not traversed. Newly created sub-directories will also be watched.

Using a BASH Script

<file bash after-change.sh> #!/bin/bash # # Monitor the input directory and if any change occurs run the provided command: # # Usage: # after-change.sh /tmp ls -al %f

if [ $# -lt 3 ]; then

echo "Usage: $0 dir cmd "
exit 1

fi

which inotifywait 2>&1 >/dev/null

if [ “$?” == “1” ]; then

echo "ERROR:  inotifywait not installed."
exit 1

fi

dir=$1 shift cmd=$1 shift files=$*

inotifywait -q -r -m $dir eval $cmd </code>

directory/run_a_command_upon_files_or_directories_changes.1611624591.txt.gz · Last modified: 2021/01/26 01:29 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki