最近我在一次工作面试中被问到这个问题。我诚实地说,我知道符号链接的行为和如何创建一个,但不了解硬链接的使用,以及它与符号链接的区别。
当前回答
通过一个简单的例子可以看出硬链接和符号链接之间的区别。指向文件的硬链接将指向存储文件的位置,或者指向该文件的inode。符号链接将指向实际文件本身。
因此,如果我们有一个名为“a”的文件,并创建一个硬链接“b”和一个符号链接“c”,它们都指向文件“a”:
echo "111" > a
ln a b
ln -s a c
“a”,“b”和“c”的输出将是:
cat a --> 111
cat b --> 111
cat c --> 111
现在让我们删除文件“a”,看看“a”,“b”和“c”的输出会发生什么:
rm a
cat a --> No such file or directory
cat b --> 111
cat c --> No such file or directory
到底发生了什么?
因为文件“c”指向文件“a”本身,如果文件“a”被删除,那么文件“c”将没有什么可指向的,实际上它也被删除了。
但是,文件“b”指向文件“a”的存储位置或inode。因此,如果文件“a”被删除,那么它将不再指向inode,但由于文件“b”被删除,inode将继续存储属于“a”的任何内容,直到不再有硬链接指向它。
其他回答
当原始文件被移动时,硬链接非常有用。例如,将文件从/bin移动到/usr/bin或/usr/local/bin。到/bin中文件的任何符号链接都将被破坏,但是硬链接(直接到文件的inode的链接)不会关心。
硬链接可能占用更少的磁盘空间,因为它们只占用一个目录条目,而符号链接需要自己的inode来存储它所指向的名称。
Hard links also take less time to resolve - symlinks can point to other symlinks that are in symlinked directories. And some of these could be on NFS or other high-latency file systems, and so could result in network traffic to resolve. Hard links, being always on the same file system, are always resolved in a single look-up, and never involve network latency (if it's a hardlink on an NFS filesystem, the NFS server would do the resolution, and it would be invisible to the client system). Sometimes this is important. Not for me, but I can imagine high-performance systems where this might be important.
I also think things like mmap(2) and even open(2) use the same functionality as hardlinks to keep a file's inode active so that even if the file gets unlink(2)ed, the inode remains to allow the process continued access, and only once the process closes it does the file really go away. This allows for much safer temporary files (if you can get the open and unlink to happen atomically, which there may be a POSIX API for that I'm not remembering, then you really have a safe temporary file) where you can read/write your data without anyone being able to access it. Well, that was true before /proc gave everyone the ability to look at your file descriptors, but that's another story.
说到这里,恢复一个在进程a中打开,但在文件系统中未链接的文件需要使用硬链接来重新创建inode链接,这样当打开该文件的进程关闭或离开时,该文件不会消失。
符号链接链接到路径名。它可以在系统文件树中的任何位置,甚至在创建链接时不需要存在。目标路径可以是相对路径,也可以是绝对路径。
硬链接是指向inode的附加指针,这意味着它们只能存在于与目标相同的卷上。到文件的附加硬链接与用于引用文件的“原始”名称难以区分。
硬链接是Unix,它在Unix和Linux中都是旧的,但符号链接在Linux中是新的。
硬链接inode与原始文件inode相同。但是symbolik链接索引节点不同于原始文件索引节点。
硬链接文件的字节大小与原始文件的字节大小相同。但是符号链接文件的字节大小不像原始文件的字节大小。符号链接文件大小小于原始文件大小。
硬链接是原始文件的镜像副本。符号链接或软链接就像窗口中的快捷方式。
如果您删除原始文件,硬链接将保留其文件,您可以看到硬链接文件的内容。在符号链接中,如果删除原始文件,其符号链接将断开,符号链接仍然保留,但不能显示符号链接内容。
符号链接是新的,它有很多特点,但硬链接是旧的,这就是为什么它有较少的特点。
让我们用终端做一些硬的和象征性的链接: Echo“为什么这么严重”> file.txt
硬链接: Ln file.txt file_hard
symbolick链接: Ln -s file.txt file_sym
让我们看看inode的内容: ls李津
通过一个简单的例子可以看出硬链接和符号链接之间的区别。指向文件的硬链接将指向存储文件的位置,或者指向该文件的inode。符号链接将指向实际文件本身。
因此,如果我们有一个名为“a”的文件,并创建一个硬链接“b”和一个符号链接“c”,它们都指向文件“a”:
echo "111" > a
ln a b
ln -s a c
“a”,“b”和“c”的输出将是:
cat a --> 111
cat b --> 111
cat c --> 111
现在让我们删除文件“a”,看看“a”,“b”和“c”的输出会发生什么:
rm a
cat a --> No such file or directory
cat b --> 111
cat c --> No such file or directory
到底发生了什么?
因为文件“c”指向文件“a”本身,如果文件“a”被删除,那么文件“c”将没有什么可指向的,实际上它也被删除了。
但是,文件“b”指向文件“a”的存储位置或inode。因此,如果文件“a”被删除,那么它将不再指向inode,但由于文件“b”被删除,inode将继续存储属于“a”的任何内容,直到不再有硬链接指向它。
一些例子可能会有所帮助。
创建两个包含数据的文件:
$ printf Cat > foo
$ printf Dog > bar
创建一个硬链接和软链接(又名符号链接):
$ ln foo foo-hard
$ ln -s bar bar-soft
通过增加大小以长格式列出目录内容:
ls -lrS
lrwxr-xr-x 1 user staff 3 3 Apr 15:25 bar-soft -> bar
-rw-r--r-- 2 user staff 4 3 Apr 15:25 foo-hard
-rw-r--r-- 2 user staff 4 3 Apr 15:25 foo
-rw-r--r-- 1 user staff 4 3 Apr 15:25 bar
这告诉我们
1st column: the file mode for the soft and hard links differ soft link: lrwxr-xr-x filetype: l = symbolic link owner permissions: rwx = readable, writable, executable group permissions: r-x = readable, not writable, executable other permissions: r-x = readable, not writable, executable hard link: -rw-r--r-- filetype: - = regular file owner permissions: rw- = readable, writable, not executable group permissions: r-- = readable, not writable, not executable other permissions: r-- = readable, not writable, not executable 2nd column: number of links is higher for the hard linked files 5th column: the size of the soft link is smaller, because it's a reference as opposed to a copy last column: the symbolic link shows the linked-to file via ->
更改foo的文件名不会影响foo-hard:
$ mv foo foo-new
$ cat foo-hard
Cat
更改foo的内容反映在foo-hard中:
$ printf Dog >> foo
$ cat foo-hard
CatDog
像foo-hard这样的硬链接指向文件的inode(内容)。
这不是像bar-soft这样的软链接的情况:
$ mv bar bar-new
$ ls bar-soft
bar-soft
$ cat bar-soft
cat: bar-soft: No such file or directory
无法找到文件的内容,因为软链接指向已更改的名称,而不是指向内容。
同样地,如果foo被删除,foo-hard仍然保存内容;如果bar被删除,bar-soft只是一个指向不存在文件的链接。