通过深度排序文件(庆典) [英] sort files by depth (bash)

查看:153
本文介绍了通过深度排序文件(庆典)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有在bash的方式从目录中深度顺序了我的文件进行排序,例如先打印的文件在present目录,然后在子目录中的文件或子目录,等等,总是透视他们的深度。

Is there a way in bash to sort my files from a directory down in depth order, for example first print the files in the present directory, then the files in the sub directory or sub directories and so forth, always in perspective to their depth.

推荐答案

最简单的办法:

$ echo * */* */*/* */*/*/* */*/*/*/* */*/*/*/*/*
a a/b a/b/c a/b/c/d1 a/b/c/d2 a/b/c/d1/e a/b/c/d2/e a/b/c/d1/e/f a/b/c/d2/e/f

或者,在列:

$ echo * */* */*/* */*/*/* */*/*/*/* */*/*/*/*/* |tr ' ' '\n'
a
a/b
a/b/c
a/b/c/d1
a/b/c/d2
a/b/c/d1/e
a/b/c/d2/e
a/b/c/d1/e/f
a/b/c/d2/e/f

树的深度为硬$ C $光盘中的例子中,
但你可以写一个小的脚本,使之更加灵活:

The depth of the tree is hardcoded in the example, but you can write a small script and make it more flexible:

A="*"
while true
do
  B=$(echo $A)
  [ "$B" = "$A" ] && break
  echo $B
  A="$A/*"
done | tr ' ' '\n'

使用示例:

$ A="*"; while true; do B=$(echo $A); [ "$B" = "$A" ] && break; echo $B; A="$A/*"; done | tr ' ' '\n'
a
a/b
a/b/c
a/b/c/d1
a/b/c/d2
a/b/c/d1/e
a/b/c/d2/e
a/b/c/d1/e/f
a/b/c/d2/e/f

提供了用于在树

实施例

Examples are provided for the tree:

$ mkdir -p a/b/c/d{1,2}/e/f
$ tree .
.
└── a
    └── b
        └── c
            ├── d1
            │   └── e
            │       └── f
            └── d2
                └── e
                    └── f

9 directories, 0 files

查找/深度解决方案显然不会,因为找工作将显示子树此起彼伏。在 -depth 键说,在哪个方向子树必须证明。但是,这并不意味着,当然,输出将被深度进行排序。

Find/depth solutions will obviously not work because find will shows subtrees one after another. The -depth key says in which direction subtree must be shown. But that doesn't mean, of course, that the output will be sorted by depth.

$ find .
.
./a
./a/b
./a/b/c
./a/b/c/d2
./a/b/c/d2/e
./a/b/c/d2/e/f
./a/b/c/d1
./a/b/c/d1/e
./a/b/c/d1/e/f

$ find . -depth
./a/b/c/d2/e/f
./a/b/c/d2/e
./a/b/c/d2
./a/b/c/d1/e/f
./a/b/c/d1/e
./a/b/c/d1
./a/b/c
./a/b
./a
.

正如你所看到的答案是不正确在两种情况下(有和没有 -find )。

这篇关于通过深度排序文件(庆典)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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