aboutsummaryrefslogtreecommitdiff
path: root/watchdir
diff options
context:
space:
mode:
authorLukasz Kasprzak <lukas@labunix.xyz>2026-05-19 18:38:39 +0200
committerLukasz Kasprzak <lukas@labunix.xyz>2026-05-19 18:38:39 +0200
commite8cf42f2f17151378574ee5b50f29904a1067bee (patch)
tree86b4db80971ecf9818ad769bd8b7fa337d218731 /watchdir
parentc3bfa44d1b6da88c184fc3312944cf47d1dfd7f2 (diff)
downloadbin-main.tar.gz
bin-main.zip
added readmeHEADmain
Diffstat (limited to 'watchdir')
-rwxr-xr-xwatchdir42
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