关于du和du -h

来源:3-6 目录大小

乃好

2020-02-29

老师你好,在9:35的时候你展示了du命令,并且说单位是字节

http://img.mukewang.com/szimg/5e59fb15096f38af00880034.jpg

这里是83948个字节,也就是83948Bytes

然后又使用了du -h命令,显示的是82M

http://img1.sycdn.imooc.com/szimg/5e59fb9c0990805800000000.jpg,82个MB,可是我觉得换算不太对呀


http://img1.sycdn.imooc.com/szimg/5e59fc0809b38f9014420584.jpg

http://img.mukewang.com/szimg/5e59fc8609105c3213040654.jpg

到底是哪里出了问题?


写回答

2回答

Oscar

2020-03-01

不好意思,视频中我没有很严谨地解释,是有不妥之处的。

du 命令如果你不带任何参数来运行,会显示当前目录的磁盘占用,以 block(块)为单位。

如果你用

man du

来查看 du 命令的手册,可以看到有这么一段关于环境变量的:

ENVIRONMENT
     BLOCKSIZE
     If the environment variable BLOCKSIZE is set, and the -k option is not specified, the block counts will be displayed in units of that size block.  If BLOCKSIZE is not set, and the -k option is not specified, the block counts will be displayed in 512-byte blocks.

就是说如果你没有设置 BLOCKSIZE(表示“块的大小”)这个环境变量并且也没有用 -k 参数(-k 参数的作用手册中也有,是“Display block counts in 1024-byte (1-Kbyte) blocks”,就是说以块大小为 1024 Byte,也就是 1024 字节,也就是每个块大小是 1 KB 这样来显示),那么就会以块大小为 512 Byte(也就是 0.5 KB)来显示。

所以,如果我用 du 命令和 du -h 命令得到的数值是不一样的,前者一般正好是后者的两倍。例如:

//img.mukewang.com/szimg/5e5b267609a7698210140348.jpg

1
0

cn_five

2020-03-08

如果你不在du参数 --block-size= 此处设置,或者环境变量里有设置,Otherwise, units default to 1024 bytes 。默认1024字节。83948Bytes/1024=81.98046875M,差不多82M。

0
0

Linux核心技能与应用

一网打尽Linux必备核心技能,面试、升职必备的“敲门砖”。

2093 学习 · 695 问题

查看课程