I have of bash script with a loop watching a directory recursively:

while true
    if path=`inotifywait -q -r -e create --format %w%f $watchpath`; then
        #modify file

If I'm not mistaken this comes with this problem:

If many files are created in that directory or the machine is busy with other tasks, a file could be created before inotifywait is reached again - which would mean that it would be ignored.

Is there a way to mitigate that? Perhaps there is way to continuously "watch" and process a stream/feed of modfied files instead?


2 Answers 2


One way is to run inotifywait in monitor mode, e.g:

inotifywait -m -q -r -e create --format '%w%f' "$watchpath" |
    while read -r path; do
        : # do something with path

There still will be a race between processes though, I'm not sure there is a way to avoid race conditions using shell utilities.

Even the man page lists this under caveats.

The inotify API identifies affected files by filename. However, by the time an application processes an inotify event, the filename may already have been deleted or renamed.


Use monitor mode -m so inotifywait does not exit after filesystem change, but outputs to stdout instead. the you can pipe this stream into a readerscript that reads lines from inotofywait and does your processing.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .