查找目录,其中缺少具有特定结尾的文件

bam*_*boo 10 linux shell bash

我想显示所有目录,不包含具有特定文件结尾的文件。因此我尝试使用以下代码:

find . -type d \! -exec test -e '{}/*.ENDING' \; -print
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我想显示所有不包含结尾的文件的目录.ENDING,但这不起作用。

我的错误在哪里?

Jen*_*y D 7

这是一个分三步的解决方案:

temeraire:tmp jenny$ find . -type f -name \*ENDING -exec dirname {} \; |sort -u > /tmp/ending.lst
temeraire:tmp jenny$ find . -type d |sort -u > /tmp/dirs.lst
temeraire:tmp jenny$ comm -3 /tmp/dirs.lst /tmp/ending.lst 
Run Code Online (Sandbox Code Playgroud)

  • 如果使用 Bash shell,没有临时文件的单行:`comm -3 <(find . -type f -name \*ENDING -exec dirname {} \; |sort -u) <(find . -type d |sort - u)` (2认同)

Den*_*lte 3

shell 扩展了*,但在您的情况下,不涉及 shell,只涉及find执行的测试命令。因此,测试其存在性的文件实际上被命名为.*.ENDING

相反,你应该使用这样的东西:

find . -type d \! -execdir sh -c 'test -e {}/*.ENDING' \; -print
Run Code Online (Sandbox Code Playgroud)

这将导致执行测试sh扩展。*.ENDING

来源:在 UX.SE 上查找 globbing

  • @bamboo 此时我会放弃 shell 实用程序并寻找不同的解决方案。 (3认同)