执行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)
没有-type
像find
. 我试过了,(^/)
但这似乎排除了一切。
帮助表示赞赏。
由于该--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
从匹配.
,并且因此不包括在当前目录为好。