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

发布于 2024-12-12 19:59:17 字数 400 浏览 0 评论 0 原文

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

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

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

我已经尝试过(?!)lookaround,但在这种情况下似乎不起作用。有什么想法或解决方法吗?非常感谢您在这个问题上的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

怪我鬧 2024-12-19 19:59:17

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

这个东西被称为负向前瞻,POSIX ERE 不支持它。

所以你必须以困难的方式来做这件事,即匹配你想要排除的所有内容。

例如

\.(txt|xml) 等。

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.

神妖 2024-12-19 19:59:17

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

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

更快的解决方法是使用 grep

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

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

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

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

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

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

$ inotifywait -m ~/js/*.js

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


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


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


 --line-buffered |
    while read path events file; do
      echo "$events happened to $file in $path"
    done

如果您只想查看已经存在的文件,您还可以使用 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.

$ 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

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

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:


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.


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


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 with while 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.



    
泼猴你往哪里跑 2024-12-19 19:59:17

我在这里发布了一个补丁,添加了 --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.

甜点 2024-12-19 19:59:17

如果您使用与 shell 相关的字符(包括 ()),请确保引用 regex 命令。

虽然这有效:

inotifywait --exclude \.pyc .

但这不是:

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

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

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

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|~)' .
节枝 2024-12-19 19:59:17

从版本 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.

眸中客 2024-12-19 19:59:17

您可以使用 --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 called example.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文