Linux du 计算文件大小的硬链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19951883/
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
du counting hardlinks towards filesize?
提问by Dan LaManna
I have a backup system that creates directories named after Unix Timestamps, and then creates incremental backups using a hardlink system (--link-dest in rsync), so typically the first backup is very large, and then later backups are fractions as big.
我有一个备份系统,它创建以 Unix 时间戳命名的目录,然后使用硬链接系统(rsync 中的 --link-dest)创建增量备份,所以通常第一个备份非常大,然后后面的备份是几分之一。
This is my output of my current backups:
这是我当前备份的输出:
root@athos:/media/awesomeness_drive# du -sh lantea_home/*
31G lantea_home/1384197192
17M lantea_home/1384205953
17M lantea_home/1384205979
17M lantea_home/1384206056
17M lantea_home/1384206195
17M lantea_home/1384207349
3.1G lantea_home/1384207678
14M lantea_home/1384208111
14M lantea_home/1384208128
16M lantea_home/1384232401
15G lantea_home/1384275601
43M lantea_home/1384318801
Everything seems correct, however, take for example the last directory, lantea_home/1384318801
:
一切似乎都是正确的,但是,以最后一个目录为例lantea_home/1384318801
:
root@athos:/media/awesomeness_drive# du -sh lantea_home/1384318801/
28G lantea_home/1384318801/
I consistently get this behavior, why is the directory considered 28G by the second du command?
我一直得到这种行为,为什么第二个 du 命令将目录视为 28G?
Note - the output remains the same with the -P and -L flags.
注意 - 输出与 -P 和 -L 标志保持相同。
采纳答案by Alfe
Hardlinks are real references to the same file (represented by its inode). There is no difference between the "original" file and a hard link pointing to it as well. Both files have the same status, bothare then references to this file. Removing one of them lets the other stay intact. Only removing the last hardlink will remove the file at last and free the disk space.
硬链接是对同一文件(由其 inode 表示)的真实引用。“原始”文件和指向它的硬链接之间也没有区别。这两个文件具有相同的地位,双方都那么这个文件的引用。移除其中一个可以让另一个保持完整。只有删除最后一个硬链接才会最后删除文件并释放磁盘空间。
So if you ask du
what it sees in one directory only, it does not care that there are hardlinks elsewhere pointing to the same contents. It simply counts all the files' sizes and sums them up. Only hardlinks withinthe considered directory are not counted more than once. du
is that clever (not all programs necessarily need to be).
所以如果你问du
它只在一个目录中看到了什么,它并不关心其他地方是否有指向相同内容的硬链接。它只是计算所有文件的大小并将它们相加。只有硬链接中所考虑的目录不计入超过一次。 du
是不是很聪明(并非所有程序都必须如此)。
So in effect, directory A might have a du
size of 28G, directory B might have a size of 29G, but together they still only occupy 30G and if you ask du
of the size of A and B, you will get that number.
所以实际上,目录 A 可能有du
28G的大小,目录 B 可能有 29G 的大小,但它们一起仍然只占用 30G,如果你问du
A 和 B 的大小,你会得到那个数字。
回答by Tobias
And with the switch "-l" du counts the hardlinks in every subdir too, so I can see, how big the whole backup is, not only the increment delta.
并且使用开关“-l” du 也计算每个子目录中的硬链接,所以我可以看到整个备份有多大,而不仅仅是增量增量。