disk Usage 按大小排序

测试目录结构如下:

1
2
3
4
5
6
\_ LiuSha丶$ tree -L 1
.
├── Go
├── python
├── the
└─穹顶之下

直接按mb的方式显示排序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#: du
du -cms ./*|sort -rn
du -sh ./* | sort -hr #: require GNU coreutils version >= 7.5

#: awk
du -hsk ./* | sort -nr | awk '
BEGIN {
split("KB,MB,GB,TB", Units, ",");
}
{
u = 1;
while ($1 >= 1024) {
$1 = $1 / 1024;
u += 1
}
$1 = sprintf("%.1f %s", $1, Units[u]);
print $0;
}
'

#: shell
for i in G M K; do du -hsx ./* | grep [0-9]$i | sort -nr -k 1; done

返回结果:

1
2
3
4
 14G    ./穹顶之下
5.5G ./Go
5.4G ./the
396M ./python