bash 如何在bash中按字母顺序对find(包括嵌套目录)的结果进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14491020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:21:14  来源:igfitidea点击:

How to sort the results of find (including nested directories) alphabetically in bash

bashsortingfind

提问by Ken

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:

我有一个基于在 bash 中运行“find”命令的结果的目录列表。例如, find 的结果是文件:

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?

有什么方法可以在 find 命令中对结果进行排序,或者通过管道将结果进行排序?

回答by John Kugelman

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

如果你有 GNU 版本的 find,试试这个:

find test -type f -printf '%h
find test -type f -printf '%h
$ find test -type f
test/b/file
test/a/file
test/file
test/z/file
$ find test -type f -printf '%h
find test -type f | while read file; do
    printf '%s
find test -print0 | sort -z
%s
find test -name file |sort -t'/' -k2.2r -k2.1
%s\n' "${file%/*}" "$(tr -dc / <<< "$file")" "$file" done | sort -t '##代码##' | awk -F'##代码##' '{print }'
%d##代码##%p\n' | sort -t '##代码##' -n | awk -F'##代码##' '{print }' test/file test/a/file test/b/file test/z/file
%d##代码##%p\n' | sort -t '##代码##' -n | awk -F '##代码##' '{print }' | while read file; do # use $file done
%d##代码##%p\n' | sort -t '##代码##' -n | awk -F '##代码##' '{print }'

To use these file names in a loop, do

要在循环中使用这些文件名,请执行

##代码##

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 -nto sort test/fileabove test/a/file. Finally we use awkto strip out the first two columns since they were only used for sorting.

find 命令为每个文件打印三件事:(1)它的目录,(2)它在目录树中的深度,以及(3)它的全名。通过在输出中包含深度,我们可以使用上面的sort -n排序。最后我们用来去掉前两列,因为它们只用于排序。test/filetest/a/fileawk

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

使用\0的三个字段之间的分隔符可以让我们在处理与他们的空间和制表符的文件名(但不换行,可惜)。

##代码##

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

如果您无法修改find命令,请尝试以下复杂的替换:

##代码##

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

它做同样的事情,${file%/*}用于获取文件的目录名称和tr用于计算斜杠数量的命令,这相当于文件的“深度”。

(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.)

(我当然希望有一个更简单的答案。你问的似乎并不难,但我正在寻找一个简单的解决方案。)

回答by zeroimpl

If you want to sort alphabetically, the best way is:

如果要按字母顺序排序,最好的方法是:

##代码##

(The example in the original question actually wanted files before directories, which is not the same and requires extra steps)

(原始问题中的示例实际上需要目录之前的文件,这不一样,需要额外的步骤)

回答by WhyteWolf

try this. for reference, it firsts sorts on the second field second char. which only exists on the file, and has a r for reverse meaning it is first, after that it will sort on the first char of the second field. [-t is field deliminator, -k is key]

尝试这个。作为参考,它首先对第二个字段的第二个字符进行排序。它只存在于文件中,并且有 ar 表示它是第一个,然后它会在第二个字段的第一个字符上排序。[-t 是字段分隔符,-k 是键]

##代码##

do a info sortfor more info. there is a ton of different ways to use the -t and -k together to get different results.

做一个info sort了解更多信息。有很多不同的方法可以一起使用 -t 和 -k 来获得不同的结果。