bash inotifywait - 排除正则表达式格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7943528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:00:42  来源:igfitidea点击:

inotifywait - exclude regex pattern formatting

regexbashshellinotify

提问by gsklee

I am trying to use inotifywaitto watch all .jsfiles under my ~/jsdirectory; how do I format my regex inside the following command?

我正在尝试使用inotifywait查看我目录下的所有.js文件~/js;如何在以下命令中格式化我的正则表达式?

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

$ 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 --excludeoption.

正则表达式 - 根据手册页,应该是 POSIX 扩展正则表达式 - 需要匹配“除以.js结尾的文件之外的所有文件”,因此这些文件可以依次被--exclude选项排除。

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.

我试过 (?!) 环视的东西,但在这种情况下它似乎不起作用。任何想法或解决方法?非常感谢您在此问题上的帮助。

采纳答案by FailedDev

I've tried the (?!) thing

我已经尝试过(?!)的事情

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

这件事称为负前瞻,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.

\.(txt|xml)等等。

回答by maikel

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

inotifywait没有包含选项并且 POSIX 扩展正则表达式不支持否定。(由FailedDev回答)

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

您可以修补 inotify 工具以获得一个--include选项。但是需要自己编译维护。(由browndav回答)

A quicker workaround is using grep.

一个更快的解决方法是使用grep.

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

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

但是如果您将输出通过管道传输到另一个命令,请注意 grep 的缓冲。添加--line-buffered以使其与while read. 下面是一个例子:

$ 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 findto generate the list of files. It will not watch newly created files.

如果你只想观看已经存在的文件,你也可以使用find生成文件列表。它不会监视新创建的文件。

$ 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.

如果您的所有文件都在一个目录中,您还可以使用ostrokach的建议。在这种情况下,shell 扩展比 find 和 xargs 容易得多。但同样,它不会监视新创建的文件。

$ inotifywait -m ~/js/*.js

回答by browndav

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

我在这里发布了一个补丁,其中添加了 --include 和 --includei 选项,它们的工作方式类似于 --exclude 和 --excludei 的否定:

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

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.

显然你必须重建 inotifytools,这相对未经测试,但希望它可以进入主线,或者对以后遇到这篇文章的人有所帮助。

回答by sebix

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

如果您使用的是与 shell 相关的字符(包括()),请确保您引用了正则表达式命令。

While this is working:

虽然这是有效的:

inotifywait --exclude \.pyc .

this is not:

这不是:

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

You have to quote the entire regular expression:

您必须引用整个正则表达式:

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

回答by Malvineous

You could get most of this with --exclude '\.[^j][^s]'to ignore files unless they contain .jsat some point in the filename or path. If you combine it with -rthen it will work with arbitrary levels of nesting.

您可以通过--exclude '\.[^j][^s]'忽略文件来获得大部分信息,除非它们.js在文件名或路径中的某个点包含。如果你将它与-r它结合起来,它将可以处理任意级别的嵌套。

Only drawback is filenames like test.js.oldwill still be watched and all files inside a directory called example.js/will also be watched, but this is probably somewhat unlikely.

唯一的缺点是像这样的文件名test.js.old仍然会被监视,并且被调用的目录中的所有文件example.js/也会被监视,但这可能有点不太可能。

You could probably extend this regex to fix this but personally I don't think the drawbacks are a big enough of a deal to worry about.

您可能可以扩展此正则表达式来解决此问题,但我个人认为这些缺点不足以令人担忧。

回答by erandros

As of version 3.20.1, inotifywait does include the --includeand --includeioptions.

从 3.20.1 版开始,inotifywait 确实包含--include--includei选项。

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

要查看它们,请运行inotifywait --help。出于某种原因,它们没有记录在联机帮助页中。