从 inotifywait 中排除目录

Sqe*_*tet 4 regex

执行inotifywait监视目录,并尝试排除所有子目录而不是排除文件。

inotifywait -r -q --exclude <pattern> dir/
Run Code Online (Sandbox Code Playgroud)

要放入什么<pattern>?手册inotifywait指定:

--exclude <pattern>
Do not process any events whose filename matches the specified POSIX extended regular expression, case sensitive.
Run Code Online (Sandbox Code Playgroud)

没有-typefind. 我试过了,(^/)但这似乎排除了一切。

帮助表示赞赏。

mur*_*uru 5

由于该--exclude选项仅作用于文件名,因此没有直接的方法。您可以尝试使用循环方式find打印所有目录的名称:

inotifywait --exclude "echo -n (;$(find . -maxdepth 1 -mindepth 1 -type d -printf '%P|');echo )" .
Run Code Online (Sandbox Code Playgroud)

请注意,我没有指定-r,因为这也会导致新创建的子目录也被监视。

这可能会因某些特殊字符而中断。


你也可以试试:

find . -maxdepth 1 -mindepth 1 -type d -printf '@%P\n' > list_of_directories
inotifywait --fromfile list_of_directories .
Run Code Online (Sandbox Code Playgroud)

inotifywait将排除以list_of_directories开头的任何文件或文件夹@(所有这些文件或文件夹都这样做)。


如果您使用inotifywait递归选项,find通过删除-maxdepth限制(也适用于第一个命令)来列出所有嵌套的子目录:

find . -mindepth 1 -type d -printf '@%P\n' > list_of_directories
inotifywait --fromfile list_of_directories . -r
Run Code Online (Sandbox Code Playgroud)

-mindepth被保持,以防止find从匹配.,并且因此不包括在当前目录为好。


ost*_*ach 5

只需使用--exclude '/\..+'. 这将忽略所有以点开头的文件。

(该.+部分是为了不排除您的基本文件夹)。