Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inotifywait - exclude regex pattern formatting

I am trying to use inotifywait to watch all .js files under my ~/js directory; how do I format my regex inside the following command?

$ inotifywait -m -r --exclude [REGEX HERE] ~/js

The regex - according to the man page, should be of POSIX extended regular expression - needs to match "all files except those that ends in .js", so these files can in turn be excluded by the --exclude option.

I've tried the (?!) lookaround thing, but it doesn't seem to work in this case. Any ideas or workarounds? Would much appreciate your help on this issue.

like image 975
gsklee Avatar asked Oct 30 '11 05:10

gsklee


5 Answers

I posted a patch here that adds --include and --includei options that work like negations of --exclude and --excludei:

https://github.com/browndav/inotify-tools/commit/160bc09c7b8e78493e55fc9f071d0c5575496429

Obviously you'd have to rebuild inotifytools, and this is relatively untested, but hopefully it can make it in to mainline or is helpful to someone who comes across this post later.

like image 158
browndav Avatar answered Nov 11 '22 12:11

browndav


As of version 3.20.1, inotifywait does include the --include and --includei options.

To see them, run inotifywait --help. For some reason, they aren't documented in the manpages.

like image 26
erandros Avatar answered Sep 18 '22 19:09

erandros


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.

like image 9
FailedDev Avatar answered Nov 11 '22 11:11

FailedDev


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
like image 8
maikel Avatar answered Nov 11 '22 10:11

maikel


Make sure you are quoting the regex command, if you are using shell-relevant characters (including ()).

While this is working:

inotifywait --exclude \.pyc .

this is not:

inotifywait --exclude (\.pyc|~) .

You have to quote the entire regular expression:

inotifywait --exclude '.*(\.pyc|~)' .
like image 2
sebix Avatar answered Nov 11 '22 12:11

sebix