【问题标题】:Is there any way to prevent find from digging down recursively into subdirectories?有什么方法可以防止 find 递归地挖掘到子目录?
【发布时间】:2010-09-06 19:48:21
【问题描述】:

当我这样做时:

$ find / 

它搜索整个系统。
我该如何预防?

(这个问题来自一个“answer”到另一个问题。)

【问题讨论】:

    标签: bash unix shell ksh


    【解决方案1】:

    考虑:

    -maxdepth n
                 True if the depth of the current file into the tree is less than
                 or equal to n.
    
    -mindepth n
                 True if the depth of the current file into the tree is greater
                 than or equal to n.
    

    【讨论】:

    • 请注意 -maxdepth 和 -mindepth 不可移植。 GNU find 有这些选项,但是可以根据需要安装。
    【解决方案2】:

    生日,

    只是想扩展 Jon 使用 -prune 的建议。它不是最容易使用的查找选项,例如仅在当前目录中搜索查找命令如下所示:

    find . \( -type d ! -name . -prune \) -o \( <the bit you want to look for> \)
    

    这将阻止 find 下降到该目录中的子目录。

    基本上,它说,“修剪任何名称不是“.”的目录,即当前目录。”

    find 命令对当前目录中找到的每个项目从左到右进行评估,因此在完成第一个元素(即修剪段)后,它将继续使用第二个 -o(OR'd)中的匹配项目表达。

    HTH。

    干杯, 抢

    【讨论】:

      【解决方案3】:

      使用通配符可能会更好。例如,如果您想在当前目录中查找所有 ksh 脚本:

      $ ls *.ksh
      

      【讨论】:

        【解决方案4】:

        使用 -prune 选项。

        【讨论】:

          【解决方案5】:

          你甚至可以这样做

          echo /specific/dir/*.jpg
          

          因为扩展通配符的是您的外壳。打字

          ls *.jpg
          

          相当于打字

          ls foo.jpg bar.jpg
          

          假设 foo.jpg 和 bar.jpg 是当前目录下所有以“.jpg”结尾的文件。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-04-29
            • 2017-04-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-04-24
            • 2018-01-14
            • 2016-04-06
            相关资源
            最近更新 更多