watchfile.sh: Perhaps the best thus far

It’s been a while since fsniper, and even longer since entr. Here’s watchfile.sh, which also watches files for changes, and then executes a command.

It’s a little difficult to show, so you might have to use your imagination. There are several ways to cue it; in my test runs, this:

./watchfile.sh test.txt cat test.txt

was enough to refresh the screen with my edits, when I saved test.txt back to disk from another terminal window.

That’s not the only way to use watchfile.sh, and the help flags will give you ideas on other methods. For example, you can monitor the output of a second command, and when that changes, execute a third command.

I like watchfile.sh a lot better than either fsniper or entr. fsniper was a bit more complex, and in the end, didn’t quite work. entr, on the other hand, worked fine, but expected to receive updates through a pipe, which is a tiny bit clunky.

But watchfile.sh is very straightforward and very flexible, and does what it promises. And I like that the task is reduced to an easily dissected script, rather than complex code.

So my advice, if you have to pick one of the three, is to go with watchfile.sh. Unless of course, you have another method you prefer. 😉

3 thoughts on “watchfile.sh: Perhaps the best thus far

  1. valadil

    It’s a little clunky but I prefer inotifywait/inotifywatch, which watches a file or directory and responds to changes you’ve specified.

    My general workflow for watching a changing file is
    while : ; do
    inotifywait /path/to/file -e MODIFY
    do stuff
    done

    inotifywait pauses until a flie is modified. Then stuff can happen. If you want stuff to be more interesting, capture the output of inotifywait and parse out the file name.

  2. Pingback: wendy: Under wendy’s watchful eye | Inconsolation

  3. Pingback: inotifywait, inotifywatch and incron: A package deal | Inconsolation

Comments are closed.