繁体   English   中英

如何以广度优先的方式递归列出某个位置的所有目录?

[英]How do I recursively list all directories at a location, breadth-first?

在这里,广度优先列表很重要。 另外,限制搜索的深度会很好。

$ find . -type d
/foo
/foo/subfoo
/foo/subfoo/subsub
/foo/subfoo/subsub/subsubsub
/bar
/bar/subbar

$ find . -type d -depth
/foo/subfoo/subsub/subsubsub
/foo/subfoo/subsub
/foo/subfoo
/foo
/bar/subbar
/bar

$ < what goes here? >
/foo
/bar
/foo/subfoo
/bar/subbar
/foo/subfoo/subsub
/foo/subfoo/subsub/subsubsub

如果可能的话,我想使用 bash 单行代码来做到这一点。 如果有一个javascript-shell,我会想象像

bash("find . -type d").sort( function (x) x.findall(/\//g).length; )

find命令支持-printf选项,它可以识别很多占位符。

一个这样的占位符是%d ,它呈现给定路径的深度,相对于find开始的位置。

因此,您可以使用以下简单的一行代码:

find -type d -printf '%d\t%P\n' | sort -r -nk1 | cut -f2-

它非常简单,不依赖于像perl这样的重型工具。

怎么运行的:

  • 它在内部生成文件列表,每个文件呈现为两行
  • 第一个字段包含深度,用于(反向)数值排序,然后切掉
  • 结果是简单的文件列表,每行一个文件,按最深的优先顺序

如果你想使用标准工具来完成它,下面的管道应该可以工作:

find . -type d | perl -lne 'print tr:/::, " $_"' | sort -n | cut -d' ' -f2

那是,

  1. 查找并打印这里的所有目录
  2. 计算每个目录中斜杠的数量并将其添加到路径中
  3. 按深度排序(即斜杠数)
  4. 只提取路径。

要限制找到的深度,请将 -maxdepth 参数添加到查找命令。

如果您希望目录以与 find 输出它们相同的顺序列出,请使用“sort -n -s”而不是“sort -n”; “-s”标志稳定排序(即,保留比较相等的项目之间的输入顺序)。

您可以使用 find 命令,find /path/to/dir -type d 下面是当前目录中目录的示例列表:

find . -type d

我的感觉是,这是比前面提到的更好的解决方案。 它涉及 grep 和循环,但我发现它工作得很好,特别是对于你想要缓冲行而不是缓冲完整查找的情况。

它需要更多的资源,因为:

  • 很多分叉
  • 很多发现
  • 当前深度之前的每个目录被 find 命中的次数与文件结构的总深度一样多(如果您实际上有任何数量的 ram,这应该不是问题......)

这很好,因为:

  • 它使用 bash 和基本的 gnu 工具
  • 它可以随时被打破(就像你看到你正在寻找的东西飞过一样)
  • 它按行工作而不是按查找工作,因此后续命令不必等待查找和排序
  • 它基于实际的文件系统分隔工作,所以如果你有一个目录中有一个斜线,它不会被列得比它更深; 如果您配置了不同的路径分隔符,您仍然可以。
#!/bin/bash 
depth=0

while find -mindepth $depth -maxdepth $depth | grep '.'
do
    depth=$((depth + 1))
done

你也可以很容易地将它放在一条线上(?):

depth=0; while find -mindepth $depth -maxdepth $depth | grep --color=never '.'; do depth=$((depth + 1)); done

但我更喜欢小脚本而不是打字......

我认为您不能使用内置实用程序来完成此操作,因为在遍历目录层次结构时,您几乎总是希望进行深度优先搜索,无论是自上而下还是自下而上。 这是一个 Python 脚本,可以为您提供广度优先搜索:

import os, sys

rootdir = sys.argv[1]
queue = [rootdir]

while queue:
    file = queue.pop(0)
    print(file)
    if os.path.isdir(file):
        queue.extend(os.path.join(file,x) for x in os.listdir(file))

编辑:

  1. 使用os.path -module 而不是os.statstat -module。
  2. 使用list.poplist.extend代替del+=运算符。

我试图找到一种使用find来执行此操作的方法,但它似乎没有-breadth选项之类的东西。 如果没有为它编写补丁,请尝试以下 shell 咒语(用于 bash):

LIST="$(find . -mindepth 1 -maxdepth 1 -type d)";
while test -n "$LIST"; do
    for F in $LIST; do
        echo $F;
        test -d "$F" && NLIST="$NLIST $(find $F -maxdepth 1 -mindepth 1 -type d)";
    done;
    LIST=$NLIST;
    NLIST="";
done

我有点偶然发现了这个,所以我不知道它是否一般有效(我只在你询问的特定目录结构上测试它)

如果你想限制深度,在外循环中放置一个计数器变量,就像这样(我也在为这个添加注释):

# initialize the list of subdirectories being processed
LIST="$(find . -mindepth 1 -maxdepth 1 -type d)";
# initialize the depth counter to 0
let i=0;
# as long as there are more subdirectories to process and we haven't hit the max depth
while test "$i" -lt 2 -a -n "$LIST"; do
    # increment the depth counter
    let i++;
    # for each subdirectory in the current list
    for F in $LIST; do
        # print it
        echo $F;
        # double-check that it is indeed a directory, and if so
        # append its contents to the list for the next level
        test -d "$F" && NLIST="$NLIST $(find $F -maxdepth 1 -mindepth 1 -type d)";
    done;
    # set the current list equal to the next level's list
    LIST=$NLIST;
    # clear the next level's list
    NLIST="";
done

(将-lt 2中的 2 替换为深度)

基本上,这实现了标准的广度优先搜索算法,使用$LIST$NLIST作为目录名称队列。 这是后一种方法,作为易于复制和粘贴的单行方法:

LIST="$(find . -mindepth 1 -maxdepth 1 -type d)"; let i=0; while test "$i" -lt 2 -a -n "$LIST"; do let i++; for F in $LIST; do echo $F; test -d "$F" && NLIST="$NLIST $(find $F -maxdepth 1 -mindepth 1 -type d)"; done; LIST=$NLIST; NLIST=""; done

没有应有的顺序:find -maxdepth -type d

要获得应有的顺序,您必须使用这个小的 shellscript 自己进行递归:

#!/bin/bash
r () 
{
    let level=$3+1
    if [ $level -gt $4 ]; then return 0; fi
    cd "$1"
    for d in *; do
        if [ -d "$d" ]; then
            echo $2/$d
        fi;
    done
    for d in *; do
        if [ -d "$d" ]; then
            (r "$d" "$2/$d" $level $4)
        fi;
    done
}
r "$1" "$1" 0 "$2"

然后你可以用参数基目录和深度调用这个脚本。

这是一种可能的方法,使用 find。 我还没有彻底测试它,所以用户要小心......

depth=0
output=$(find . -mindepth $depth -maxdepth $depth -type d | sort); 
until [[ ${#output} -eq 0 ]]; do 
  echo "$output"
  let depth=$depth+1
  output=$(find . -mindepth $depth -maxdepth $depth -type d | sort)
done

像这样:

find . -type d | 
  perl -lne'push @_, $_;
    print join $/,
      sort { 
        length $a <=> length $b || 
          $a cmp $b 
        } @_ if eof'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM