Bash - 按名称和大小列出和排序文件及其大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9755068/
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 01:48:17  来源:igfitidea点击:

Bash - List and Sort Files and Their Sizes and by Name and Size

bashsortingsize

提问by jao


I am trying to figure out how to sort a list of files by name and size.
How do I sort by file name and size using "du -a" and not show directories?


我想弄清楚如何按名称和大小对文件列表进行排序。
如何使用“ du -a”按文件名和大小排序而不显示目录?

Using "du -a"

使用“ du -a

1   ./locatedFiles
0   ./testDir/j.smith.c
0   ./testDir/j.smith
1   ./testDir/sampleFunc/arrays
2   ./testDir/sampleFunc
0   ./testDir/j.smith.txt
0   ./testDir/testing
0   ./testDir/test2
0   ./testDir/test3
0   ./testDir/test1
0   ./testDir/first/j.smith
0   ./testDir/first/test
1   ./testDir/first
1   ./testDir/second
1   ./testDir/third
6   ./testDir

How can I list all files without directories, add files sizes, and sort by files name first, then by size?

如何列出没有目录的所有文件,添加文件大小,然后按文件名排序,然后按大小排序?

Thanks for your help

谢谢你的帮助

回答by icyrock.com

You can use this:

你可以使用这个:

find -type f -printf "%f  %s %p\n"|sort

Explanation:

解释:

  • -type f to find files only
  • -printf to print the output in specific format:
    • %f to print the file name
    • %s to print the file size
    • %p to print the whole file name (i.e. with leading folders) - you can omit this if you want
  • -type f 只查找文件
  • -printf 以特定格式打印输出:
    • %f 打印文件名
    • %s 打印文件大小
    • %p 打印整个文件名(即带有前导文件夹) - 如果需要,您可以省略它

Then run through sort which sorts in the order given above (i.e. file name, then file size, then file path). The output would be something like this (part of the output shown):

然后运行 ​​sort 按上面给出的顺序排序(即文件名,然后是文件大小,然后是文件路径)。输出将是这样的(显示的输出的一部分):

...
XKBstr.h 18278 ./extensions/XKBstr.h
XlibConf.h 1567 ./XlibConf.h
Xlib.h 99600 ./Xlib.h
Xlibint.h 38897 ./Xlibint.h
Xlocale.h 1643 ./Xlocale.h
xlogo11 219 ./bitmaps/xlogo11
....

Hope this helps

希望这可以帮助

回答by kev

You can use the sortcommand

你可以使用sort命令

$ find -type f -printf $'%s\t%f\n' | sort -k2,2 -k1,1n

sort by second field(name), then first field(size) numerically.

按第二个字段(名称)排序,然后按数字排序第一个字段(大小)。

回答by torek

As the other answers so far say, this is not really a bash problem.

正如迄今为止的其他答案所说,这并不是真正的 bash 问题。

dupretty much insists on telling you about directories: if you point it at a directory, then with or without -ait will tell you about it.

du几乎坚持告诉你目录:如果你将它指向一个目录,那么无论有没有-a它都会告诉你它。

If you have the GNU du, though, you can tell it to read a list of NUL-terminated file names from stdin, so you can use findto produce the list: find ... -print0 | du --files0-from=-(you don't need the -aflag here). (If you don't have the --files0-fromoption you can still invoke durelatively efficiently using xargs; see the xargsdocumentation.)

du但是,如果您有 GNU ,则可以告诉它从 stdin 读取以 NUL 结尾的文件名列表,因此您可以使用它find来生成列表:( 此处find ... -print0 | du --files0-from=-不需要-a标志)。(如果您没有该--files0-from选项,您仍然可以du使用 相对有效地调用xargs;请参阅xargs文档。)

If you have GNU du, though, you probably have GNU find, which has -printfas described by @icyrock.com. Just use that. Then use an explicit sort.

du但是,如果您拥有 GNU ,那么您可能拥有 GNU find-printf正如@icyrock.com 所描述的那样。就用那个。然后使用显式sort.