最近我在一次工作面试中被问到这个问题。我诚实地说,我知道符号链接的行为和如何创建一个,但不了解硬链接的使用,以及它与符号链接的区别。


当前回答

当原始文件被移动时,硬链接非常有用。例如,将文件从/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。符号链接将指向实际文件本身。

因此,如果我们有一个名为“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”的任何内容,直到不再有硬链接指向它。

另外:

硬链接的读取性能优于符号链接(微性能) 符号链接可以被复制,版本控制,等等。换句话说,它们是一个实际的文件。另一方面,硬链接的级别略低,您会发现,与符号链接相比,提供将硬链接作为硬链接而不是普通文件处理的工具较少

俗话说,一幅画胜过千言万语。以下是我对它的想象:

下面是我们如何得到这张照片的:

Create a name myfile.txt in the file system that points to a new inode (which contains the metadata for the file and points to the blocks of data that contain its contents, i.e. the text "Hello, World!": $ echo 'Hello, World!' > myfile.txt Create a hard link my-hard-link to the file myfile.txt, which means "create a file that should point to the same inode that myfile.txt points to": $ ln myfile.txt my-hard-link Create a soft link my-soft-link to the file myfile.txt, which means "create a file that should point to the file myfile.txt": $ ln -s myfile.txt my-soft-link

看看如果myfile.txt被删除(或移动)会发生什么:my-hard-link仍然指向相同的内容,因此不受影响,而my-soft-link现在什么都不指向。其他答案讨论了每种方法的利与弊。

在文件系统下面,文件由inode表示。(或者是多个索引节点?不确定。)

文件系统中的文件基本上是一个到inode的链接。 因此,硬链接只是创建另一个文件,该文件具有指向相同底层inode的链接。

当您删除一个文件时,它会删除到底层inode的一个链接。只有当到inode的所有链接都被删除时,inode才会被删除(或可删除/可覆盖)。

符号链接是指向文件系统中另一个名称的链接。

一旦建立了硬链接,链接就指向inode。删除、重命名或移动原始文件不会影响硬链接,因为它链接到底层inode。对inode上数据的任何更改都反映在引用该inode的所有文件中。

注意:硬链接只在同一个文件系统内有效。符号链接可以跨文件系统,因为它们只是另一个文件的名称。

What you think of as an ordinary "file" is actually two separate things: The data of a file, and a directory entry. When you create a hard link for a file, you actually create a second directory entry which refers to the same data. Both directory entries have the exact same functionality; each one can be used to open the file to read it. So you don't really have "a file plus a hard link", you have "file data with two directory entries". What you think of as deleting a file actually deletes a directory entry, and when the last directory entry for the data is deleted, then the data itself is deleted as well. For ordinary files that have only one directory entry, deleting the directory entry will delete the data as always. (While a file is opened, the OS creates a temporary link to the file, so even when you delete all directory entries, the data stays but disappears as soon as you close the file).

例如,创建文件a .txt,硬链接B.txt,然后删除a .txt。当您创建a .txt时,会创建一些数据,并创建一个目录条目a .txt。在创建硬链接时,创建了另一个目录条目B.txt,指向完全相同的数据。当您删除a .txt时,您仍然拥有所有数据和一个目录条目B.txt,就像您首先创建了一个文件B.txt一样。

软链接只是一个(几乎)普通的文件,只是它不包含数据,而是另一个目录条目的路径。如果你删除了软链接所指向的文件,那么软链接将包含一个不再指向目录条目的路径;它坏了。如果你删除软链接,就像删除任何其他文件一样,它指向的文件不受影响。