代码之家  ›  专栏  ›  技术社区  ›  Jon Ericson Homunculus Reticulli

有没有办法阻止find递归地挖掘到子目录中?

  •  3
  • Jon Ericson Homunculus Reticulli  · 技术社区  · 16 年前

    当我这样做的时候:

    $ find / 
    

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

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

    5 回复  |  直到 9 年前
        1
  •  4
  •   Rob Wells    16 年前

    G'Day.

    只是想扩大乔恩的建议使用-删减。这不是最容易使用的查找选项,例如只在当前目录中搜索find命令如下所示:

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

    这将停止查找向下进入此目录中的子目录。

    基本上,它说,“删掉任何一个目录,其名称不是“.”,即当前目录。

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

    Hth.

    干杯, 抢劫

        2
  •  9
  •   dmckee --- ex-moderator kitten    16 年前

    考虑:

    -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.
    
        3
  •  3
  •   Jon Ericson Homunculus Reticulli    16 年前

    你最好使用通配符。例如,如果要在当前目录中查找所有ksh脚本:

    $ ls *.ksh
    
        4
  •  0
  •   Jon Ericson Homunculus Reticulli    16 年前

    使用 -修剪 选择权。

        5
  •  0
  •   aib    16 年前

    你甚至可以这样做

    echo /specific/dir/*.jpg
    

    因为是您的shell扩展了通配符。打字

    ls *.jpg
    

    相当于键入

    ls foo.jpg bar.jpg
    

    给定foo.jpg和bar.jpg是当前目录中以“.jpg”结尾的所有文件。