diff options
| author | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-05-19 18:38:39 +0200 |
|---|---|---|
| committer | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-05-19 18:38:39 +0200 |
| commit | e8cf42f2f17151378574ee5b50f29904a1067bee (patch) | |
| tree | 86b4db80971ecf9818ad769bd8b7fa337d218731 /watchdir | |
| parent | c3bfa44d1b6da88c184fc3312944cf47d1dfd7f2 (diff) | |
| download | bin-main.tar.gz bin-main.zip | |
Diffstat (limited to 'watchdir')
| -rwxr-xr-x | watchdir | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/watchdir b/watchdir new file mode 100755 index 0000000..3499bf8 --- /dev/null +++ b/watchdir @@ -0,0 +1,42 @@ +#!/bin/sh +# Usage: watchdir [DIR] [LOGFILE] +# Recursively logs file additions/modifications/deletions. + +set -eu + +DIR="${1:-$PWD}" +LOG="${2:-$DIR/changes.log}" + +if [ ! -d "$DIR" ]; then + printf 'Not a directory: %s\n' "$DIR" >&2 + exit 1 +fi + +DIR=$(readlink -f "$DIR") +LOG=$(readlink -f "$LOG") + +# Exclude patterns (POSIX extended regex, OR-joined). +# Append more lines below to extend. +EXCLUDES="${LOG}\$" +EXCLUDES="${EXCLUDES}|/\\.git(/|\$)" +# EXCLUDES="${EXCLUDES}|/node_modules(/|\$)" +# EXCLUDES="${EXCLUDES}|\\.swp\$" +EXCLUDE_RE="($EXCLUDES)" + +printf 'Watching: %s\n' "$DIR" +printf 'Logging to: %s\n' "$LOG" + +inotifywait -mr \ + --exclude "$EXCLUDE_RE" \ + -e close_write,create,delete,moved_to,moved_from \ + --format '%T|%e|%w%f' --timefmt '%F %T' \ + "$DIR" | +while IFS='|' read -r ts ev path; do + case "$ev" in + CREATE*|MOVED_TO*) action=added ;; + CLOSE_WRITE*) action=modified ;; + DELETE*|MOVED_FROM*) action=deleted ;; + *) action="$ev" ;; + esac + printf '%s|%s|%s\n' "$ts" "$action" "$path" >> "$LOG" +done |
