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


当前回答

在这个答案中,当我说文件时,我指的是内存中的位置

所有保存的数据都使用称为inode的数据结构存储在内存中,每个inode都有一个inodennumber。inode号用于访问inode。到文件的所有硬链接可能有不同的名称,但共享相同的inode号。因为所有的硬链接都有相同的inodennumber(访问相同的inode),所以它们都指向相同的物理内存。

符号链接是一种特殊的文件。因为它也是一个文件,它将有一个文件名和一个inode号。如上所述,inode号访问指向数据的inode。现在,符号链接的特殊之处在于,符号链接中的inodennumbers访问那些指向另一个文件的“路径”的inode。更具体地说,符号链接中的inode号访问指向另一个硬链接的inode。

当我们在GUI中移动、复制、删除文件时,我们使用的是文件的硬链接,而不是物理内存。当我们删除一个文件时,我们正在删除该文件的硬链接。我们并没有清除物理内存。如果文件的所有硬链接都被删除,那么将无法访问存储的数据,尽管它可能仍然存在于内存中

其他回答

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一样。

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

符号链接为文件提供了另一个名称,在某种程度上类似于硬链接。但是,即使文件中仍然存在符号链接,也可以删除文件。

我对使用的两点看法:

软链接可以用来缩短长路径名,例如:

ln -s /long/folder/name/on/long/path/file.txt /short/file.txt

对/short/file.txt所做的更改将应用于原始文件。

硬链接可以用来移动大文件:

$ ls -lh /myapp/dev/
total 10G
-rw-r--r-- 2 root root 10G May 22 12:09 application.bin

ln /myapp/dev/application.bin /myapp/prd/application.bin

即时复制到不同的文件夹,原始文件(在/myapp/dev上)可以移动或删除,而不会触及/myapp/prd上的文件

一个目录条目链接一个结构:

struct dentry{
    ino_t ino;
    char  name[256];
}

ino是inode的编号,name是文件名,inode结构可能是这样的:

struct inode{
      link_t nlink; 
      ...
}

例如,你创建一个文件/1,目录条目可能是这样的:

struct dentry{
     ino_t ino; /* such as 15 */
     char  name[256]; /* "1" */
} 

inode结构可能是这样的:

   struct inode{ /* inode number 15 */
         link_t nlink; /* nlink = 1 */
         ...
    }

然后你创建一个硬链接(可能是/100),目录条目可能是这样的:

  struct dentry{
     ino_t ino; /* 15 */
     char  name[256]; /* 100 */
  }

inode结构可能是这样的:

   struct inode{ /* inode numebr 15 */
         link_t nlink; /* nlink = 2 */
         ...
    }

然后你创建一个符号链接(可能是/200)到文件1,目录条目可能是这样的:

  struct dentry{
        ino_t ino; /* such as 16 */
        char  name[256]; /* "200" */
  }

inode结构可能是这样的:

   struct inode{ /* inode number 15 */ 
         link_t nlink; /* nlink = 2 */
         ...
    }

   struct inode{ /* inode number 16 */
         link_t nlink; /* nlink = 1 */
         ...
    } /* the data of inode 16 maybe /1 or 1 */

我推荐你们去维基百科:

符号链接 硬链接

以下几点:

与硬链接不同,符号链接可以跨文件系统(大多数情况下)。 符号链接可以指向目录。 硬链接指向一个文件,并允许您使用多个名称引用同一个文件。 只要至少有一个链接,数据就仍然可用。