inotifywait - 排除正则表达式模式格式
我正在尝试使用 inotifywait
来监视 ~/js
目录下的所有 .js 文件;如何在以下命令中格式化我的正则表达式?
$ inotifywait -m -r --exclude [REGEX HERE] ~/js
正则表达式 - 根据手册页,应该是 POSIX 扩展正则表达式 - 需要匹配“除以结尾的文件之外的所有文件”在 .js 中”,因此这些文件又可以通过 --exclude
选项排除。
我已经尝试过(?!)lookaround,但在这种情况下似乎不起作用。有什么想法或解决方法吗?非常感谢您在这个问题上的帮助。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这个东西被称为负向前瞻,POSIX ERE 不支持它。
所以你必须以困难的方式来做这件事,即匹配你想要排除的所有内容。
例如
\.(txt|xml)
等。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
没有包含选项,并且 POSIX 扩展正则表达式不支持否定。 (由 FailedDev 回答)您可以修补 inotify 工具以获得
--include
选项。但需要你自己编译和维护。 (由 browndav 回答)更快的解决方法是使用
grep
。如果您只想查看已经存在的文件,您还可以使用
find
来生成文件列表。它不会监视新创建的文件。如果您的所有文件都位于一个目录中,您还可以使用 ostrokach建议。在这种情况下,shell 扩展比 find 和 xargs 容易得多。但同样,它不会查看新创建的文件。
但如果将输出通过管道传输到另一个命令,请注意 grep 的缓冲。添加
--line-buffered
以使其与while read
一起使用。这是一个例子:如果您只想查看已经存在的文件,您还可以使用
find
来生成文件列表。它不会监视新创建的文件。如果您的所有文件都位于一个目录中,您还可以使用 ostrokach建议。在这种情况下,shell 扩展比 find 和 xargs 容易得多。但同样,它不会查看新创建的文件。
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
.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.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.
But be aware of grep's buffering if you pipe the output to another commands. Add
--line-buffered
to make it work withwhile read
. Here is an example: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.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.
我在这里发布了一个补丁,添加了 --include 和 --includei 选项,其作用类似于 --exclude 和 --excludei 的否定:
https://github.com/browndav/inotify-tools/commit/160bc09c7b8e78493e55fc9f071d0c5575496429
显然你必须重建inotifytools,这是相对未经测试的,但希望它可以进入主线或对后来看到这篇文章的人有帮助。
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.
如果您使用与 shell 相关的字符(包括
()
),请确保引用 regex 命令。虽然这有效:
但这不是:
您必须引用整个正则表达式:
Make sure you are quoting the regex command, if you are using shell-relevant characters (including
()
).While this is working:
this is not:
You have to quote the entire regular expression:
从版本 3.20.1 开始,inotifywait 确实包含
--include
和--includei
选项。要查看它们,请运行
inotifywait --help
。由于某种原因,它们没有记录在联机帮助页中。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.您可以使用
--exclude '\.[^j][^s]'
来忽略文件,除非它们在文件名中的某个位置包含.js
或路径。如果将其与-r
结合使用,它将适用于任意级别的嵌套。唯一的缺点是像
test.js.old
这样的文件名仍然会被监视,并且名为example.js/
的目录中的所有文件也会被监视,但这可能有点不太可能。您可能可以扩展此正则表达式来解决此问题,但就我个人而言,我认为这些缺点并不值得担心。
You could get most of this with
--exclude '\.[^j][^s]'
to ignore files unless they contain.js
at some point in the filename or path. If you combine it with-r
then it will work with arbitrary levels of nesting.Only drawback is filenames like
test.js.old
will still be watched and all files inside a directory calledexample.js/
will also be watched, but this is probably somewhat unlikely.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.