磁盘使用格式

发布于 2024-12-18 01:04:17 字数 1459 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月静好 2024-12-25 01:04:17

使用 GNU Coreutils >= 7.5

du -hs * | sort -h

在 Mac 上OS X 使用自制程序安装 coreutils 很容易:

sudo brew install coreutils

来自 https://serverfault.com/questions/62411/how-can- i-sort-du-h-output-by-size

Using GNU Coreutils >= 7.5:

du -hs * | sort -h

On Mac OS X it's easy to install coreutils with homebrew:

sudo brew install coreutils

From https://serverfault.com/questions/62411/how-can-i-sort-du-h-output-by-size

不羁少年 2024-12-25 01:04:17

我不太明白这个问题。为什么没有排序?

编辑应该要求相同的单位,例如du -k总是千字节

[jaypal~/Temp]$ du -k ./* | sort -nr
120K    ./GTP
108K    ./scripts
 36K    ./backup
 24K    ./GTP/ParserDump
 20K    ./perl
4.0K    ./outfile
4.0K    ./file1
4.0K    ./file
4.0K    ./colors
4.0K    ./GTP/ParserDump/ParserDump

如果您需要使用“人类友好”单位输出,请在排序后进行转换(也许使用<代码>awk)。

AWK 脚本:这就是我们对文件进行排序的方式..不知道我们是否可以使用-h选项,但这有点像模拟- h 选项。结果更准确;)

[jaypal~/Desktop/Reference]$ du -h ./* | sort -nr
584K    ./scan 1.pdf
544K    ./Personal/Resume
 44K    ./Membership.xlsx
 16M    ./Personal
 16K    ./Membership Transmittal Template.xlsx
 14M    ./Personal/Docs
2.4M    ./Work Docs
1.6M    ./Work Docs/Work
1.5M    ./Personal/Raa

[jaypal~/Desktop/Reference]$ du -k ./* | sort -nr | awk '{split("KB,MB,GB",size,",");}{x = 1;while ($1 >= 1024) {$1 = $1 / 1024;x = x + 1} $1 = sprintf("%-4.2f%s", $1, size[x]); print $0;}'
15.92MB ./Personal
13.82MB ./Personal/Docs
2.35MB ./Work Docs
1.59MB ./Work Docs/Work
1.46MB ./Personal/Raa
584.00KB ./scan 1.pdf
544.00KB ./Personal/Resume
44.00KB ./Membership.xlsx
16.00KB ./Membership Transmittal Template.xlsx

I don't quite understand the question. Why isn't is not sorted?

edit should ask for the same unit, e.g. always kilobytes with du -k

[jaypal~/Temp]$ du -k ./* | sort -nr
120K    ./GTP
108K    ./scripts
 36K    ./backup
 24K    ./GTP/ParserDump
 20K    ./perl
4.0K    ./outfile
4.0K    ./file1
4.0K    ./file
4.0K    ./colors
4.0K    ./GTP/ParserDump/ParserDump

If you need to output using "human friendly" units, make the conversion after sorting (perhaps using awk).

AWK Script: This is how we can sort the files .. don't know if we can using the -h option but this is kinda like simulating the -h option. Results are more accurate ;)

[jaypal~/Desktop/Reference]$ du -h ./* | sort -nr
584K    ./scan 1.pdf
544K    ./Personal/Resume
 44K    ./Membership.xlsx
 16M    ./Personal
 16K    ./Membership Transmittal Template.xlsx
 14M    ./Personal/Docs
2.4M    ./Work Docs
1.6M    ./Work Docs/Work
1.5M    ./Personal/Raa

[jaypal~/Desktop/Reference]$ du -k ./* | sort -nr | awk '{split("KB,MB,GB",size,",");}{x = 1;while ($1 >= 1024) {$1 = $1 / 1024;x = x + 1} $1 = sprintf("%-4.2f%s", $1, size[x]); print $0;}'
15.92MB ./Personal
13.82MB ./Personal/Docs
2.35MB ./Work Docs
1.59MB ./Work Docs/Work
1.46MB ./Personal/Raa
584.00KB ./scan 1.pdf
544.00KB ./Personal/Resume
44.00KB ./Membership.xlsx
16.00KB ./Membership Transmittal Template.xlsx
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文