我可以有效地限制 find 给我的匹配项数量吗?

Can I efficiently limit the number of matches find gives me?

我将 find 应用到包含数万个文件的大型目录树,以查找具有特定名称的所有 csv 文件,作为面向内部用户的应用程序的一部分。

find /path/to/dirs -name filename.ext | head -n 100

这需要相当长的时间(在某些情况下长达一分钟),我怀疑仅找到前 100 个匹配项(如果没有匹配项则更少)和 return 那些。 find 是否可以在不将输出通过管道传输到 head 的情况下实现(这需要找到所有匹配项)?我在 info 页面中没有看到任何引起我注意的选项。如果没有(正如我所怀疑的那样),是否已经有任何其他 unix 工具可以做到这一点?

或者,我如何将此代码从 Rosetta Code 修改为这样的工具?

#include "boost/filesystem.hpp"
#include "boost/regex.hpp"
#include <iostream>

using namespace boost::filesystem;

int main()
{
  path current_dir("."); //
  boost::regex pattern("a.*"); // list all files starting with a
  for (recursive_directory_iterator iter(current_dir), end;
       iter != end;
       ++iter)
  {
    std::string name = iter->path().filename().string();
    if (regex_match(name, pattern))
      std::cout << iter->path() << "\n";
  }
}

我必须承认我不是 C++ 程序员,但我怀疑这段代码可以很容易地被修改成 shell 工具的人修改,该工具采用 path 参数,filename 参数,和一个 max 参数,并打印目录 path.

下最多 max 个名为 filename 的文件的完整文件路径

虽然我不是 C++ 程序员,但我今天确实编写了一个 UNIX 工具(在 Racket 中)来执行此操作。稍后我会尝试 post 一些基准测试,将其与 find.

进行比较

Q. "Is this possible with find without piping the output to head (which requires finding all matches)"

这是错误的。当 head 满足时,它结束,关闭管道。 find 将以 SIGPIPE (13) 退出,因此不需要找到所有匹配项