aboutsummaryrefslogtreecommitdiff
path: root/watchdir
blob: 3499bf849d30ebacb66c06eea851a15036bf1a97 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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