Skip to content

Instantly share code, notes, and snippets.

@igemnace
Last active September 9, 2020 17:44
Show Gist options
  • Save igemnace/87508d54999bcd3f0cbbd0a9ce8612df to your computer and use it in GitHub Desktop.
Save igemnace/87508d54999bcd3f0cbbd0a9ce8612df to your computer and use it in GitHub Desktop.
Watch file modifications with inotifywait and run arbitrary commands on those files
#!/usr/bin/env bash
# Depends on inotifywait, from inotify-tools
# Usage: watchrun dir... -- command...
# e.g. watchrun src -- ctags -a
dirs=()
until [[ $1 == -- ]]; do
dirs+=("$1")
shift
done
shift
rest=("$@")
inotifywait -rme modify --format '%w%f' "${dirs[@]}" | while read -r filename; do
<&2 echo "Running for ${filename##*/}..."
"${rest[@]}" "$filename"
<&2 echo "Done."
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment