inotifywait - exclude regex pattern formatting

I've tried the (?!) thing

This thing is called negative lookahead and it is not supported by POSIX ERE.

So you have to do it the hard way, i.e. match everything that you want to exclude.

e.g.

\.(txt|xml) etc.


inotifywait has no include option and POSIX extended regular expressions don't support negation. (Answered by FailedDev)

You can patch the inotify tools to get an --include option. But you need to compile and maintain it yourself. (Answered by browndav)

A quicker workaround is using grep.

$ inotifywait -m -r ~/js | grep '\.js$'

But be aware of grep's buffering if you pipe the output to another commands. Add --line-buffered to make it work with while read. Here is an example:

$ inotifywait -m -r ~/js | grep '\.js$' --line-buffered |
    while read path events file; do
      echo "$events happened to $file in $path"
    done

If you just want to watch already existing files, you can also use find to generate the list of files. It will not watch newly created files.

$ find ~/js -name '*.js' | xargs inotifywait -m

If all your files are in one directory, you can also use ostrokach's suggestion. In that case shell expansion is much easier than find and xargs. But again, it won't watch newly created files.

$ inotifywait -m ~/js/*.js