Linux 带有 -regex 选项的 `find` 命令没有按预期工作

Linux `find` command with a -regex option doesn't work as expected

我在当前目录中有一个文件foo。然后我 运行 以下命令:

  1. find . -regex 'foo' 什么也没找到
  2. find . -regex 'foo.*' 什么也没找到
  3. find . -regex '.*foo' 找到文件

我希望这些命令中的任何一个都能产生肯定的结果。

find 命令版本为 4.4.2

来自手册:

   -regex pattern
          File name matches regular expression pattern.  This is  a  match
          on  the  whole path, not a search.  For example, to match a file
          named `./fubar3', you can use the regular expression `.*bar.' or
          `.*b.*3',  but  not `f.*r3'.  The regular expressions understood
          by find are by default Emacs Regular Expressions, but  this  can
          be changed with the -regextype option.

重要的部分是:这是整个路径的匹配,而不是搜索

所以只有第三个正则表达式产生结果:

./foo

find 实际上尝试匹配字符串 ./foo(不同于 -name 开关)所以如果文件 [=15] 第三个模式 -regex '.*foo' 必须工作=] 没有扩展名。

尝试使用:

``find . -regex '.*foo.*'``

并报告输出。