scripts/util/watchdir
2025-12-05 15:46:07 -05:00

35 lines
935 B
Shell
Executable file

#!/usr/bin/env bash
# Directory change notifier
# Requires: inotify-tools (inotifywait) and notify-send (libnotify)
# If the user supplied a directory, use it. Otherwise default to ~/Downloads.
DIR="${1:-$HOME/Downloads}"
# Resolve to an absolute path
DIR="$(realpath "$DIR" 2>/dev/null)"
# Validate directory exists
if [[ ! -d "$DIR" ]]; then
echo "❌ Error: '$DIR' is not a valid directory."
echo "Usage: $0 /path/to/directory"
exit 1
fi
# Check dependency
if ! command -v inotifywait >/dev/null 2>&1; then
echo "❌ Error: inotifywait (from inotify-tools) is required."
exit 1
fi
echo "👀 Watching directory: $DIR"
echo "Press Ctrl+C to stop."
echo
# Watch for changes: created, deleted, modified
inotifywait -m -e create -e delete -e modify "$DIR" --format '%e %f' |
while read -r event file; do
notify-send "📂 Directory Change" "$event: $file"
echo "$(date '+%F %T') $event $file"
done