Linux 为什么 `du` 的输出通常与 `du -b` 的输出如此不同
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5694741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why is the output of `du` often so different from `du -b`
提问by knittl
why is the output of du
often so different from du -b
? -b
is shorthand for --apparent-size --block-size=1
. only using --apparent-size
gives me the same result most of the time, but --block-size=1
seems to do the trick. i wonder if the output is then correct even, and which numbers are the ones i want? (i.e. actual filesize, if copied to another storage device)
为什么与 的输出du
经常如此不同du -b
?-b
是 的简写--apparent-size --block-size=1
。--apparent-size
大多数时候只使用会给我相同的结果,但--block-size=1
似乎可以解决问题。我想知道输出是否正确,哪些数字是我想要的?(即实际文件大小,如果复制到另一个存储设备)
采纳答案by Ken Bloom
Apparent sizeis the number of bytes your applications think are in the file. It's the amount of data that would be transferred over the network (not counting protocol headers) if you decided to send the file over FTP or HTTP. It's also the result of cat theFile | wc -c
, and the amount of address space that the file would take up if you loaded the whole thing using mmap
.
表观大小是您的应用程序认为文件中的字节数。如果您决定通过 FTP 或 HTTP 发送文件,它是将通过网络传输的数据量(不包括协议标头)。这也是 的结果cat theFile | wc -c
,以及如果您使用mmap
.
Disk usageis the amount of space that can't be used for something else because your file is occupying that space.
磁盘使用量是不能用于其他用途的空间量,因为您的文件正在占用该空间。
In most cases, the apparent size is smaller than the disk usage because the disk usage counts the full size of the last (partial) block of the file, and apparent size only counts the data that's in that last block. However, apparent size is larger when you have a sparse file (sparse files are created when you seek somewhere past the end of the file, and then write something there -- the OS doesn't bother to create lots of blocks filled with zeros -- it only creates a block for the part of the file you decided to write to).
在大多数情况下,表观大小小于磁盘使用量,因为磁盘使用量计算文件的最后(部分)块的完整大小,而表观大小仅计算最后一个块中的数据。然而,当你有一个稀疏文件时,表观大小会更大(当你在文件末尾寻找某处然后在那里写一些东西时会创建稀疏文件——操作系统不会费心创建大量填充零的块—— - 它只为您决定写入的文件部分创建一个块)。
回答by Brian Carlton
Because by default du gives disk usage, which is the same or larger than the file size. As said under --apparent-size
因为默认 du 给出了磁盘使用量,它等于或大于文件大小。正如在 --apparent-size 下所说
print apparent sizes, rather than disk usage; although the apparent size is usually smaller, it may be
larger due to holes in (`sparse') files, internal fragmentation, indirect blocks, and the like
回答by Ruud H.G. van Tol
Compare (for example) du -bm
to du -m
.
比较(例如)du -bm
与du -m
.
The -b
sets --apparent-size --block-size=1
,
but then the m
overrides the block-size to be 1M
.
的-b
套--apparent-size --block-size=1
,但随后的m
替换的块大小为1M
。
Similar for -bh
versus -h
:
the -bh
means --apparent-size --block-size=1 --human-readable
, and again the h
overrides that block-size.
与-bh
vs类似-h
:-bh
mean --apparent-size --block-size=1 --human-readable
,再次h
覆盖块大小。
回答by hukko
Files and folders have their real size and the size on disk
文件和文件夹有它们的实际大小和磁盘上的大小
--apparent-size is file or folder real size
size on disk is the amount of bytes the file or folder takes on disk. Same thing when using just du
--apparent-size 是文件或文件夹的实际大小
size on disk 是文件或文件夹在磁盘上占用的字节数。仅使用 du 时相同
If you encounter that apparent-size is almost always several magnitudes higher than disk usage then it means that you have a lot of (`sparse') files of files with internal fragmentation or indirect blocks.
如果您遇到表观大小几乎总是比磁盘使用量高几个数量级,那么这意味着您有很多(“稀疏”)文件,其中包含内部碎片或间接块。