我如何递归列出的一个位置,广度优先的所有目录? [英] How do I recursively list all directories at a location, breadth-first?

查看:143
本文介绍了我如何递归列出的一个位置,广度优先的所有目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

广度优先名单是很重要的,在这里。此外,限制搜索的深度将是很好的。

  $找到。 ð型
/富
/富/ subfoo
/富/ subfoo / subsub
/富/ subfoo / subsub / subsubsub
/酒吧
/酒吧/ subbar$找到。 ð型-depth
/富/ subfoo / subsub / subsubsub
/富/ subfoo / subsub
/富/ subfoo
/富
/酒吧/ subbar
/酒吧$<发生的事情吗? >
/富
/酒吧
/富/ subfoo
/酒吧/ subbar
/富/ subfoo / subsub
/富/ subfoo / subsub / subsubsub

我想如果可能的话,要做到这一点使用一个bash一行程序。如果有一个JavaScript的外壳,我想像类似

 庆典(发现型Ð)排序(函数(X)x.findall(/ \\ // G)。长度)。


解决方案

找到命令支持 -printf 选项承认有很多的占位符。

这样的一个占位符是%d个这使得定路径的深入,相对于其中找到开始。

因此​​,您可以用以下简单的一行:

 找到型ð-printf%d个\\ t%P \\ N'|排序-r -nk1 |切-f2-

这是很简单的,不依赖于重型工具如 perl的

它是如何工作:


  • 它内部产生的文件的列表,每个呈现为两个场行

  • 所述第一字段包含深度,其用于(反向)的数值的排序,然后切去

  • 结果是简单的文件列表,每行一个文件,在最深的优先顺序

Breadth-first list is important, here. Also, limiting the depth searched would be nice.

$ 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

I'd like to do this using a bash one-liner, if possible. If there were a javascript-shell, I'd imagine something like

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

解决方案

The find command supports -printf option which recognizes a lot of placeholders.

One such placeholder is %d which renders the depth of given path, relative to where find started.

Therefore you can use following simple one-liner:

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

It is quite straightforward, and does not depend on heavy tooling like perl.

How it works:

  • it internally generates list of files, each rendered as a two-field line
  • the first field contains the depth, which is used for (reverse) numerical sorting, and then cut away
  • resulting is simple file listing, one file per line, in the deepest-first order

这篇关于我如何递归列出的一个位置,广度优先的所有目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆