gpt4 book ai didi

linux - 在循环内对查找的输出进行排序

转载 作者:太空宇宙 更新时间:2023-11-04 12:28:19 24 4
gpt4 key购买 nike

这是我在 StackOverflow 上的第一篇文章,所以请保持温和。

我想做的是按大小(从最大到最小)对查找的输出进行排序。

我有一个 file.txt,其中包含:

file1
file2
file3
file4
file5
file6

我的代码是这样的:

for x in $(cat file.txt)
do
find -name $x -printf '%s %p\n'| sort -rn
done

但是输出没有排序。

输出是文件的路径(很棒)及其大小,顺序与文件中的顺序相同。

例如:

 128  /dir1/file1
8 /dir2/file2
0 file3
4 file4
...
and so on

最佳答案

您对 find 的调用会为每个文件打印一行,然后您将其发送给 sort。由于 sort 每次调用它时只得到一行,因此无需排序,文件按照它们在 file.txt 中列出的顺序输出。

相反,将整个循环的输出通过管道传递给 sort:

for x in $(cat file.txt)
do
find -name $x -printf '%s %p\n'
done| sort -rn

关于linux - 在循环内对查找的输出进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43981992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com