#!/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