使用 -prune 查找留下修剪的目录名称

ocm*_*mob 5 tcsh find

我构建了以下查找命令:

find ./ \( -path "*work*" -o -path "*ncvlog_lib*" \) -prune -o -type f -not
-name "*.wlf" -not -name "*.vcd" -not -name "*.sim" -not -name "*vcs*"
Run Code Online (Sandbox Code Playgroud)

该命令在一行中调用。为了便于阅读,我在这里断了线。

唯一的问题是,尽管提供了 -type f 参数,但它还是打印了修剪后的目录名称。示例输出:

./cdl2verilog_files/test_synth/work
./cdl2verilog_files/test_synth/some_file1.txt
./cdl2verilog_files/test_synth/something_else.txt
./cdl2verilog_files/test_synth/another_file.v
Run Code Online (Sandbox Code Playgroud)

work是一个目录。它的内容不包含在输出中,因此修剪可以根据需要进行。但是,目录本身会被打印出来。我似乎无法找到解决方案。有什么想法吗?

顺便说一句,我正在使用 tcsh。

cas*_*cas 6

-prune排除目录的内容,但不排除目录本身。如果这是命令中的-prune唯一操作find,则会发生这种情况。如果有任何其他操作(例如-exec-print),则不会输出修剪后的目录名称。所以你只需要-print在命令末尾添加一个显式的find。例如:

find ./ \( -path "*work*" -o -path "*ncvlog_lib*" \) -prune -o -type f  \
  -not -name "*.wlf" -not -name "*.vcd" -not -name "*.sim" -not -name "*vcs*" \
  -print
Run Code Online (Sandbox Code Playgroud)

find顺便说一句,您可以通过使用单个-regex谓词而不是多个-name谓词来缩短/简化命令。例如

find ./ \( -path "*work*" -o -path "*ncvlog_lib*" \) -prune -o -type f \
  -regextype awk -not -regex '.*\.(wlf|vcd|sim)$|.*vcs.*' -print
Run Code Online (Sandbox Code Playgroud)

这会产生与上面第一个版本相同的输出。

  • 我花了一分钟才意识到为什么会这样,就像手册页中那样,它有点双重否定。`find` 对*完整表达式*为真的_每个文件_运行`-print`,除非_表达式包含除_`-prune`或`-quit`之外的操作。即,“find”默认为“-print”,除非“在某个地方”你告诉它做其他事情。对于像 `-path "work/*" -prune -o -type f` 这样的东西,表达式中唯一的操作是 `-prune`,因为 `-prune -o` 的目的是短路表达式如果为 true,“find”也会对这些匹配项运行“-print”。 (2认同)