如何找到以“.html”结尾但文件名中没有“.bin”的文件?

How can I find files that end in ".html", but don't have ".bin" anywhere in the filename?

我有以下几种类型的文件名:

  1. 结尾为.html:

    l_scheduling_suite.temp.html
    
  2. 另一种类型以 .html 结尾,但其名称中包含 .bin

    l_scheduling_suite.temp.bin.html
    
  3. 第三个以.bin结尾:

    l_scheduling_suite.temp.bin
    

文件名随意。它不一定总是在 .html.bin 之前有一个 temp。我需要找到所有只符合第一种格式的文件。我正在使用以下正则表达式通过 grep 来查找文件,但我无法使其工作:

"(?=(\.html)$) (?=(?!\.bin))"

我应该如何使用 grep 或 find 来获取正确的文件列表?

试试这个:

find -type f | grep -P '^.*(?<!\.bin)\.html$'

这使用了负面回顾。基本上这意味着,获取所有以 .html 结尾的名称,但只需确保 .bin 不会出现在它之前。

使用简单的 Glob 模式

你把问题复杂化了。您只需要(基于您发布的语料库)是:

find . -name \*.temp.html

这将找到 .temp.html 结束 的所有文件。您的其他示例不匹配,因为 *.bin.html*.temp.bin 与此 glob 模式没有重叠。

使用否定 Glob

如果您的语料库选择不当,而您实际上是在尝试匹配 end in .html 不匹配的所有文件 在名称中的任何位置包含 .bin,然后您可以只使用带有否定 glob 的 find 实用程序,而无需求助于正则表达式、管道、扩展的 shell glob 或其他扭曲。例如:

find . -name '*.html' -not -name '*.bin*'