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 10:14] – [Test inotifywait] 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: | ||
- | |||
- | < | ||
- | Setting up watches. | ||
- | Watches established. | ||
- | </ | ||
- | |||
- | <WRAP info> | ||
- | **NOTE: | ||
- | |||
- | * **< | ||
- | * Symbolic links are not traversed. | ||
- | |||
- | |||
- | See http:// | ||
- | </ | ||
- | |||
- | |||
- | ---- | ||
- | |||
- | In a 2nd shell, go to that directory being watched. | ||
- | |||
- | <code bash> | ||
- | cd thedirtowatch/ | ||
- | touch test1 | ||
- | </ | ||
- | |||
- | <WRAP info> | ||
- | **NOTE: | ||
- | |||
- | <code bash> | ||
- | RESULT=test1 | ||
- | </ | ||
- | </ | ||
- | |||
- | |||
- | ---- | ||
- | |||
- | ===== Only listen for certain events ===== | ||
- | |||
- | Change the inotifywait to the following: | ||
- | |||
- | <code bash> | ||
- | while true; do | ||
- | res=`inotifywait -q -e create, open, close, modify, moved_to, moved_from -r thedirtowatch/ | ||
- | echo " | ||
- | done | ||
- | </ | ||
- | |||
- | |||
- | <WRAP info> | ||
- | **NOTE: | ||
- | |||
- | Events include: | ||
- | |||
- | * **access**: A watched file or a file within a watched directory was read from. | ||
- | * **modify**: A watched file or a file within a watched directory was written to. | ||
- | * **attrib**: The metadata | ||
- | * This includes timestamps, file permissions, | ||
- | * **close_write**: | ||
- | * This does not necessarily imply the file was written to. | ||
- | * **close_nowrite**: | ||
- | * **close**: | ||
- | * Note that this is actually implemented simply by listening for both **close_write** and **close_nowrite**, | ||
- | * **open**: | ||
- | * **moved_to**: | ||
- | * This event occurs even if the file is simply moved from and to the same directory. | ||
- | * **moved_from**: | ||
- | * This event occurs even if the file is simply moved from and to the same directory. | ||
- | * **move**: | ||
- | * Note that this is actually implemented simply by listening for both **moved_to** and **moved_from**, | ||
- | * **move_self**: | ||
- | * After this event, the file or directory is no longer being watched. | ||
- | * **create**: | ||
- | * **delete**: | ||
- | * **delete_self**: | ||
- | * After this event the file or directory is no longer being watched. | ||
- | * Note that this event can occur even if it is not explicitly being listened for. | ||
- | * **unmount**: | ||
- | * After this event the file or directory is no longer being watched. | ||
- | * Note that this event can occur even if it is not explicitly being listened to. | ||
- | |||
- | The **< | ||
- | |||
- | The **-q** option is to not show too much output. | ||
- | |||
- | See http:// | ||
- | |||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== 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.1611656099.txt.gz · Last modified: 2021/01/26 10:14 by peter