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


当前回答

一些例子可能会有所帮助。

创建两个包含数据的文件:

$ 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只是一个指向不存在文件的链接。

其他回答

在进行增量备份时,硬链接非常有用。例如,请参阅rsnapshot。这个想法是使用硬链接进行复制:

拷贝备份号n到n + 1 拷贝备份n - 1到n ... 拷贝备份0到备份1 用任何更改过的文件更新备份0。

除了您所做的任何更改之外,新的备份不会占用任何额外的空间,因为所有增量备份都将指向未更改的文件的同一组inode。

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

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 */

加上以上所有答案,查找硬链接和软链接文件的差异可以理解为:

在当前目录中有一个文件f6,还有一个名为t2的目录。

名为f1和。/t2/f2的文件是到f6的符号链接。

f7和。/t2/f8文件是f6的硬链接。

要找到软链接和硬链接,我们可以使用:

$ find -L . -samefile f6 

> ./f1
> ./f6
> ./f7
> ./t2/f2
> ./t2/f8

找到硬链接,我们可以使用:

$ find . -xdev -samefile f6

> ./f6
> ./f7
> ./t2/f8

因为硬链接可以在同一个文件系统上创建,所以我们可以在同一个文件系统/挂载点中搜索所有没有使用-L选项的硬链接(使用-xdev选项)。它节省了不必要的搜索到不同的挂载点。

所以搜索硬链接比搜索软链接快一些(如果我错了或不清楚,请纠正)。

我推荐你们去维基百科:

符号链接 硬链接

以下几点:

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

一些例子可能会有所帮助。

创建两个包含数据的文件:

$ 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只是一个指向不存在文件的链接。