Linux 硬链接或软链接会占用文件系统中的任何空间吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8337878/
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
do hard links or soft links take any space in the filesystem?
提问by dig_123
I have gone through a number of docs. which have lot of discrepancies over either hardlinks or softlinks taking any space in the file system.Can anyone clear this out for me?
我已经浏览了许多文档。硬链接或软链接在文件系统中占用任何空间有很多差异。有人可以帮我清除这个吗?
For hardlinks i found out this:
对于硬链接,我发现了这一点:
I had a file c1 in my home directory which i hardlinked with d1 in same directory.both c1 and b1 have 11 byte size.Now when i am doing a "ls -lrt" the total bytes for all files listed(excluding d hidden files ofcourse) is 64 bytes. now when i remove hardlink d1 and again do a ls -lrt it gives me 60 bytes.does that not mean hardlinks occupy space in hard disk, but lot of docs.negate this fact, why?
我的主目录中有一个文件 c1,我在同一目录中与 d1 硬链接。c1 和 b1 都有 11 字节大小。现在,当我执行“ls -lrt”时列出的所有文件的总字节数(不包括 d 隐藏文件)当然)是 64 字节。现在,当我删除硬链接 d1 并再次执行 ls -lrt 时,它给了我 60 个字节。这是否意味着硬链接占用了硬盘空间,但是很多文档。否定这个事实,为什么?
I could have checked same way creating a soft link for the file and then deleting it, but since my soft link has only 2bytes size,i dont think deleting it would have any significant effect on the round off total size listed in the output of ls -lrt.
我可以检查相同的方式为文件创建一个软链接然后删除它,但由于我的软链接只有 2 字节大小,我认为删除它不会对 ls 输出中列出的总大小产生任何重大影响-lrt。
So what is with this?
那么这是怎么回事呢?
回答by dig_123
Yes. They both take space as they both still have directory entries.
是的。它们都占用空间,因为它们仍然有目录条目。
A hardlink entry (really, a "normal entry" that [often] shares an inode) takes space, as does a symlink entry which must store the link path (the text itself) somehow. The actual space required is slightly different due to allocation/layout rules determined by the exact filesystem implementation (e.g. block/tree sizes and how the symlink is stored).
硬链接条目(实际上,[通常] 共享一个 inode 的“正常条目”)占用空间,符号链接条目也必须以某种方式存储链接路径(文本本身)。由于由确切的文件系统实现(例如块/树大小和符号链接的存储方式)确定的分配/布局规则,所需的实际空间略有不同。
However, the amount of space is minimaland can be [almost always] considered inconsequentially in relationship to data in the files themselves.
但是,空间量是最小的,并且[几乎总是] 认为与文件本身中的数据无关。
回答by glglgl
Of course they occupy (a little bit of) space:
当然,它们占据(一点点)空间:
hard linked files have multiple directory entries. Each entry occupies space in the directory itself, but from then on, they share the same structures: inode and data area are shared. But I am not sure how this is accounted; maybe the directory entries always occupy a multiple of a certain size or so.
Symlinks occupy space for the directory entry as well, plus one inode which holds information about the link properties as well as the link target itself.
硬链接文件有多个目录条目。每个条目都占用目录本身的空间,但从那时起,它们共享相同的结构:共享索引节点和数据区。但我不确定这是如何计算的;也许目录条目总是占据一定大小的倍数左右。
符号链接也占用目录条目的空间,外加一个 inode 保存有关链接属性以及链接目标本身的信息。
With that small sizes (11 bytes of data in the file) the overhead counts more than the real data. With bigger files, however, this small overhead of few bytes is negligible.
有了这么小的尺寸(文件中有 11 个字节的数据),开销比实际数据要多。然而,对于更大的文件,这几个字节的小开销可以忽略不计。
回答by Brandon Rush
To be short and simple a hardlink is a reference to the inode within the filesystem. Some utilities will read this incorrectly. It will not take up any disk space.
简而言之,硬链接是对文件系统中的 inode 的引用。某些实用程序会错误地读取此信息。它不会占用任何磁盘空间。
回答by user123444555621
ls
is wrong!
ls
是错的!
It counts hard linked files multiple times. ls -l
will just add up the block numbers of each entry, no matter how many hardlinks it has.
它多次计算硬链接文件。ls -l
只会将每个条目的块号相加,无论它有多少个硬链接。
(Use ls -1si
to show each file's inode number as well as block usage)
(ls -1si
用于显示每个文件的 inode 编号以及块使用情况)
Try du -Ssb .
instead. This will give you the correct disk usage in bytes. Unless you use the -l
switch, which will reproduce ls
's behaviour.
试试吧du -Ssb .
。这将为您提供正确的磁盘使用量(以字节为单位)。除非您使用-l
开关,否则会重现ls
的行为。