Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want a script to sleep unless a certain file is modifed/deleted (or a file created in a certain directory, or ...). Can this be achieved in some elegant way? The simplest thing that comes to my mind is a loop that sleeps for some time before checking the status again, but maybe there is a more elegant way?

share|improve this question
up vote 33 down vote accepted

On linux, you can use the kernel's inotify feature. Tools for scripting can be found there: inotify-tools.

Example use from wiki:


EVENT=$(inotifywait --format '%e' ~/file1) # blocking without looping
[ $? != 0 ] && exit
[ "$EVENT" = "MODIFY" ] && echo 'file modified!'
[ "$EVENT" = "DELETE_SELF" ] && echo 'file deleted!'
# etc...
share|improve this answer
Most unices have a similar feature. Unfortunately each has its own interface, and many have no shell API. – Gilles Jan 7 '11 at 18:40

There is an API called inotify for C programmers.

There are some tools that use it, e.g. incron and inotify-tools.

share|improve this answer
Tremendous, that’s the stuff. Cheers Mikel! – Paul D. Waite Apr 6 '11 at 8:52

Indeed there is: entr(1) will run arbitrary commands when files change, and also provides an auto-reload option for restarting application servers.

edit: some examples

Rebuild if sources files change

$ find *.c | entr make

Launch and auto-reload test server if files change

$ ls *.py | entr -r python main.py

Providing a the agument +/path/to/fifo allows more complex scripting by instructing entr to write the name of each file that changes to a named pipe. The following will convert Markdown files in the current directory to HTML as they're edited

$ ls *.md | entr +/tmp/notify &
$ while read F
> do
>   markdown2html $F
> done < /tmp/notify
share|improve this answer
Intriguing, but weird. So ... it reads a bunch of file names on standard input, and runs the specified command when one of them changes? – tripleee Jul 4 '13 at 5:36
Looks interesting, thanks! Is there any way to have entr pass on the name of the file that has changed as well? – Tobias Kienzler Jul 4 '13 at 7:00
As of the 2.7 release, the special /_ argument (somewhat analogous to $_ in Perl) is replaced with the name of the first file that changed – eradman Dec 6 '14 at 11:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.