Last active
December 21, 2024 15:04
-
-
Save ScottJWalter/a20b7d7a8eba5e942ea8cb07009ea1e8 to your computer and use it in GitHub Desktop.
Bash wrapper for inotify
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# inotify-watcher.sh -- Monitor a folder for file changes | |
# | |
# This is a wrapper around inotify, intended to make it easier to quickly | |
# spin up directory watchers. To activate watching for a particular event, | |
# comment and fill out the appropriate `inotify_XXXX` function. | |
# | |
inotify_close_write() { | |
# File closed after writing ("saved") and ready to be processed. | |
# | |
# initiate_transcription_flow $1 $2 | |
} | |
#inotify_create() {} | |
#inotify_access() {} | |
#inotify_ignored() {} | |
#inotify_open() {} | |
#inotify_attrib() {} | |
#inotify_modify() {} | |
#inotify_close_nowrite() {} | |
#inotify_moved_from() {} | |
#inotify_moved_to() {} | |
#inotify_delete() {} | |
#inotify_delete_self() {} | |
# NO EDITING NEEDED BELOW THIS LINE | |
############################################################################## | |
# MAIN | |
# | |
if [[ $# -eq 0 ]] ; then | |
echo "$0 path/to/directory/to/watch" | |
exit 0 | |
fi | |
# array of possible actions | |
inotify_events_master=( | |
create | |
access | |
ignored | |
open | |
attrib | |
modify | |
close_write | |
close_nowrite | |
moved_from | |
moved_to | |
delete | |
delete_self | |
) | |
# array of active actions | |
inotify_events=() | |
# figure out which actions have functions, and activate those | |
for event in "${inotify_events_master[@]}"; do | |
if declare -F "inotify_${event,,}" > /dev/null; then | |
inotify_events+=("${event,,}") | |
fi | |
done | |
events=$(IFS=,; echo "${inotify_events[*]}") | |
echo Monitoring "$actions" events in "$1" ... | |
# move to the parent of the directory you want to search | |
pushd $(dirname "$1") | |
# main loop, watching $1 for any $inotify_actions[] | |
inotifywait -q -m \ | |
-e "$events" "$1" \ | |
--timefmt '%Y-%m-%dT%H:%M:%S' \ | |
--format '%T %w %f %e' \ | |
| while read datetime dir filename event; do | |
echo "${datetime}: ${dir}${filename} $event" | |
eval "inotify_${event,,} \"${dir}\" \"${filename}\"" | |
done | |
popd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment