【问题标题】:find, excluding dir, not descending into dir, AND using maxdepth and mindepth查找,不包括 dir,不下降到 dir,并且使用 maxdepth 和 mindepth
【发布时间】:2012-09-10 23:36:36
【问题描述】:

这是 RHEL 5.6 和 GNU 找到 4.2.27。

我正在尝试从查找中排除一个目录,并希望确保该目录没有下降到。我已经看到很多帖子说 -prune 会这样做 - 确实如此。我可以运行这个命令:

find . -type d -name "./.snapshot*" -prune -o -print

它有效。我通过 strace 运行它并验证它没有下降到 .snapshot。

我还想仅在某个级别查找目录。我可以使用 mindepth 和 maxdepth 来做到这一点:

find . -maxdepth 8 -mindepth 8 -type d

它为我提供了 8 级以下的所有目录,包括 .snapshot 中的内容。

如果我结合 prune 和 mindepth 和 maxdepth 选项:

find . -maxdepth 8 -mindepth 8 -type d \( -path "./.snapshot/*" -prune -o -print \)

输出是正确的 - 除了 .snapshot 中的内容之外,我看到所有 dirs 都下降了 8 级,但是如果我通过 strace 运行该查找,我看到 .snapshot 仍在下降到 1 到 8 级。

我尝试了各种不同的组合,移动优先括号,重新排列表达式组件 - 产生正确输出的所有内容仍然下降到 .snapshot。

我在手册页中看到 -prune 不适用于 -depth,但没有说明 mindepth 和 maxdepth。

谁能给点建议?

谢谢... 比尔

【问题讨论】:

    标签: unix sh


    【解决方案1】:

    我想这就是你的答案。 -mindepth 似乎会关闭所有测试直到该级别。有点无赖。

    (来自“man find”)

       -mindepth levels
              **Do not apply any** tests or actions at levels less than levels (a non-negative integer).
              '-mindepth 1' means process all  files  except  the  command line arguments.
    

    【讨论】:

      【解决方案2】:

      我将此添加为另一个答案,因为它对这个问题有不同的看法。你可以试试这样的 find 命令:

      查找 * .[^s][^n]*(其他开关/参数)

      它可以通过使用 shell 而不是 find 开关来避免 .snapshot 目录。当然它并不完美,但也许它已经足够接近你正在做的事情了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-04
        • 2018-08-01
        • 1970-01-01
        • 2015-01-05
        • 2011-05-30
        • 2015-08-16
        • 2020-07-05
        • 1970-01-01
        相关资源
        最近更新 更多