directory:run_a_command_upon_files_or_directories_changes
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| directory:run_a_command_upon_files_or_directories_changes [2021/01/26 09:49] – peter | directory:run_a_command_upon_files_or_directories_changes [2021/01/26 10:53] (current) – removed peter | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Directory - Run a Command Upon Files or Directories Changes ====== | ||
| - | Use either inotifywait or iwatch. | ||
| - | |||
| - | |||
| - | ---- | ||
| - | |||
| - | ===== Using inotifywait ===== | ||
| - | |||
| - | ==== Install inotify-tools ==== | ||
| - | |||
| - | <code bash> | ||
| - | sudo apt install inotify-tools | ||
| - | </ | ||
| - | |||
| - | ---- | ||
| - | |||
| - | ==== Test inotifywait ==== | ||
| - | |||
| - | In a shell enter: | ||
| - | |||
| - | <code bash> | ||
| - | while true; do | ||
| - | res=`inotifywait -r thedirtowatch/ | ||
| - | echo " | ||
| - | done | ||
| - | </ | ||
| - | |||
| - | returns: | ||
| - | |||
| - | <code bash> | ||
| - | Setting up watches. | ||
| - | Watches established. | ||
| - | </ | ||
| - | |||
| - | <WRAP info> | ||
| - | **NOTE: | ||
| - | |||
| - | See http:// | ||
| - | </ | ||
| - | |||
| - | |||
| - | ---- | ||
| - | |||
| - | In a 2nd shell, go to that directory being watched. | ||
| - | |||
| - | <code bash> | ||
| - | touch test1 | ||
| - | </ | ||
| - | |||
| - | **NOTE: | ||
| - | |||
| - | <code bash> | ||
| - | RESULT=test1 | ||
| - | </ | ||
| - | |||
| - | ---- | ||
| - | |||
| - | <WRAP info> | ||
| - | **NOTE: | ||
| - | |||
| - | For example: | ||
| - | |||
| - | <code bash> | ||
| - | while true; do res=`inotifywait -e create, open, close, modify, moved_to, moved_from -r thedirtowatch/ | ||
| - | </ | ||
| - | |||
| - | The **< | ||
| - | |||
| - | See http:// | ||
| - | |||
| - | </ | ||
| - | |||
| - | ---- | ||
| - | |||
| - | ===== More ===== | ||
| - | |||
| - | <code bash> | ||
| - | inotifywait -m -e modify, | ||
| - | chmod 644 file; && \ | ||
| - | chown blah:blah file; | ||
| - | </ | ||
| - | |||
| - | <WRAP info> | ||
| - | **NOTE:** | ||
| - | |||
| - | * **-m, --monitor**: | ||
| - | * **-r, --recursive**: | ||
| - | * Symbolic | ||
| - | </ | ||
| - | |||
| - | ---- | ||
| - | |||
| - | ===== 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: | ||
| - | # | ||
| - | |||
| - | if [ $# -lt 3 ]; then | ||
| - | echo " | ||
| - | exit 1 | ||
| - | fi | ||
| - | |||
| - | which inotifywait 2>&1 >/ | ||
| - | |||
| - | if [ " | ||
| - | echo " | ||
| - | exit 1 | ||
| - | fi | ||
| - | |||
| - | dir=$1 | ||
| - | shift | ||
| - | cmd=$1 | ||
| - | shift | ||
| - | options=$* | ||
| - | |||
| - | inotifywait -q -r -m $dir $options | ||
| - | eval $cmd | ||
| - | </ | ||
| - | |||
| - | ---- | ||
| - | |||
| - | ===== References ===== | ||
| - | |||
| - | http:// | ||
directory/run_a_command_upon_files_or_directories_changes.1611654561.txt.gz · Last modified: 2021/01/26 09:49 by peter
