日常工作中,会遇到一堆目录,如何查看各目录大小呢,大家都知道du命令

先列出前10条

root@local /scripts]#du -sh * | head120K	2017031510248K	201703151423M	2017031515664K	2017031516656K	20170315172.0M	201703151868K	2017031519884K	2017031520580K	201703152160K	2017031609

再排序

root@local /scripts]#du -sh * | sort -nr | head1020K	20170621201008K	20170330161000K	2017032221988K	2017052420988K	2017033118988K	201703191298K	201703281080K	201705111476M	201705091868K	2017052010

是不是乱了?这是du命令中-h导致的,先计算了大小,但是在排序的时候,只针对数字,不会识别后面的K 和 M ,优化下

root@local /scripts]#du -s * | sort -nr | head | awk '{printf"%sM\n",$1/1024 }'125.906M66.4023M65.8516M63.5117M53.5859M53.125M51.4805M47.8945M46.8867M44.25M

OK了