如何在bash中按字母顺序排列find(包括嵌套目录)的结果 [英] How to sort the results of find (including nested directories) alphabetically in bash

查看:190
本文介绍了如何在bash中按字母顺序排列find(包括嵌套目录)的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于在bash中运行find命令的结果的目录列表。作为一个例子,查找的结果是文件:

  test / a / file 
test / b / file
test / file
test / z / file

我想排序输出,所以它显示为:

  test / file 
test / a / file
test / b /文件
test / z /文件

命令,或通过管道结果排序?

解决方案

如果您有GNU版本的查找,试试这个:

  find test -type f -printf'%h\0%d\0%p\\\
' sort -t'\0'-n | awk -F'\0'{print $ 3}'

要在a中使用这些文件名循环,做

  find test -type f -printf'%h \ 0%d \ 0%p \\\
'| sort -t'\0'-n | awk -F'\0''{print $ 3}'|同时读取文件;做
#使用$文件
完成

每个文件:(1)其目录,(2)目录树中的深度,以及(3)其全名。通过在输出中包含深度,我们可以使用 sort -n test / file 进行排序>测试/一个/文件。最后我们使用 awk 去掉前两列,因为它们只用于排序。



使用 \ 0 作为三个字段之间的分隔符,使我们能够处理文件名中包含空格和制表符(但不幸的是,不包括换行符)。

  $ find test -type f 
test / b / file
test / a / file
test / file
test / z / file
$ find test -type f -printf'%h\0%d\0%p\\\
'| sort -t'\0'-n | awk -F'\ 0'{print $ 3}'
test / file
test / a / file
test / b / file
test / z / file

如果您无法修改 find 命令,然后尝试这个错综复杂的替换:

  find test -type f |同时读取文件; do 
printf'%s\0%s\0%s\\\
'$ {file%/ *}$(tr -dc / done | sort -t'\0'| awk -F'\0''{print $ 3}'

它也是这样, $ {file%/ *} 用于获取文件的目录名称, tr 命令用于计算(相当于一个文件的深度)。

(我当然希望有一个更简单的答案,你所要求的似乎不是很难,但我在一个简单的解决方案空白。)


I have a list of directories based on the results of running the "find" command in bash. As an example, the result of find are the files:

test/a/file
test/b/file
test/file
test/z/file

I want to sort the output so it appears as:

test/file
test/a/file
test/b/file
test/z/file

Is there any way to sort the results within the find command, or by piping the results into sort?

解决方案

If you have the GNU version of find, try this:

find test -type f -printf '%h\0%d\0%p\n' | sort -t '\0' -n | awk -F '\0' '{print $3}'

To use these file names in a loop, do

find test -type f -printf '%h\0%d\0%p\n' | sort -t '\0' -n | awk -F '\0' '{print $3}' | while read file; do
    # use $file
done

The find command prints three things for each file: (1) its directory, (2) its depth in the directory tree, and (3) its full name. By including the depth in the output we can use sort -n to sort test/file above test/a/file. Finally we use awk to strip out the first two columns since they were only used for sorting.

Using \0 as a separator between the three fields allows us to handle file names with spaces and tabs in them (but not newlines, unfortunately).

$ find test -type f
test/b/file
test/a/file
test/file
test/z/file
$ find test -type f -printf '%h\0%d\0%p\n' | sort -t '\0' -n | awk -F'\0' '{print $3}'
test/file
test/a/file
test/b/file
test/z/file

If you are unable to modify the find command, then try this convoluted replacement:

find test -type f | while read file; do
    printf '%s\0%s\0%s\n' "${file%/*}" "$(tr -dc / <<< "$file")" "$file"
done | sort -t '\0' | awk -F'\0' '{print $3}'

It does the same thing, with ${file%/*} being used to get a file's directory name and the tr command being used to count the number of slashes, which is equivalent to a file's "depth".

(I sure hope there's an easier answer out there. What you're asking doesn't seem that hard, but I am blanking on a simple solution.)

这篇关于如何在bash中按字母顺序排列find(包括嵌套目录)的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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