最近我在一次工作面试中被问到这个问题。我诚实地说,我知道符号链接的行为和如何创建一个,但不了解硬链接的使用,以及它与符号链接的区别。
当前回答
俗话说,一幅画胜过千言万语。以下是我对它的想象:
下面是我们如何得到这张照片的:
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的所有文件中。
注意:硬链接只在同一个文件系统内有效。符号链接可以跨文件系统,因为它们只是另一个文件的名称。
符号链接链接到路径名。它可以在系统文件树中的任何位置,甚至在创建链接时不需要存在。目标路径可以是相对路径,也可以是绝对路径。
硬链接是指向inode的附加指针,这意味着它们只能存在于与目标相同的卷上。到文件的附加硬链接与用于引用文件的“原始”名称难以区分。
我推荐你们去维基百科:
符号链接 硬链接
以下几点:
与硬链接不同,符号链接可以跨文件系统(大多数情况下)。 符号链接可以指向目录。 硬链接指向一个文件,并允许您使用多个名称引用同一个文件。 只要至少有一个链接,数据就仍然可用。
我对使用的两点看法:
软链接可以用来缩短长路径名,例如:
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上的文件
一些例子可能会有所帮助。
创建两个包含数据的文件:
$ 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只是一个指向不存在文件的链接。