如何按大小对目录中的所有文件进行排序?

use*_*976 52 unix bash shell

如何显示 unix 目录中的文件,按其人类可读的大小排序,从最大到最小?

我试过

du -h | sort -V -k 1 
Run Code Online (Sandbox Code Playgroud)

但它似乎不起作用。

Hel*_*o71 60

ls(1) /sort

-S     sort by file size
Run Code Online (Sandbox Code Playgroud)

  • `-S` 不再是一个有效的排序参数,至少在 ubuntu 上是这样。@alex 的以下答案对我有用。答案链接是 https://superuser.com/a/990437/528836。 (2认同)

kev*_*kev 38

$ ls -lhS
Run Code Online (Sandbox Code Playgroud)
-l     use a long listing format
-h     with -l, print sizes in human readable format (e.g., 1K 234M 2G)
-S     sort by file size
Run Code Online (Sandbox Code Playgroud)


zta*_*013 16

如果你有合适的sort版本,你可以简单地使用:

du -h | sort -rh
Run Code Online (Sandbox Code Playgroud)

我的是

$ sort --version
sort (GNU coreutils) 8.12
Run Code Online (Sandbox Code Playgroud)


And*_*510 6

du -ha | sort -h

du:估计文件磁盘使用情况。

-h : for human
-a : all files
Run Code Online (Sandbox Code Playgroud)

sort:对文本行进行排序。

-h : for human
Run Code Online (Sandbox Code Playgroud)

man du; man sort了解更多。它适用于我的 ubuntu v15。