How to run a shell script when a file or directory changes?

Drew LeSueur picture Drew LeSueur · Oct 30, 2010 · Viewed 82.8k times · Source

I want to run a shell script when a specific file or directory changes.

How can I easily do that?

Answer

kenorb picture kenorb · Jul 6, 2016

You may try entr tool to run arbitrary commands when files change. Example for files:

$ ls -d * | entr sh -c 'make && make test'

or:

$ ls *.css *.html | entr reload-browser Firefox

or print Changed! when file file.txt is saved:

$ echo file.txt | entr echo Changed!

For directories use -d, but you've to use it in the loop, e.g.:

while true; do find path/ | entr -d echo Changed; done

or:

while true; do ls path/* | entr -pd echo Changed; done