最近我在一次工作面试中被问到这个问题。我诚实地说,我知道符号链接的行为和如何创建一个,但不了解硬链接的使用,以及它与符号链接的区别。
当前回答
从MSDN,
符号链接
A symbolic link is a file-system object that points to another file system object. The object being pointed to is called the target. Symbolic links are transparent to users; the links appear as normal files or directories, and can be acted upon by the user or application in exactly the same manner. Symbolic links are designed to aid in migration and application compatibility with UNIX operating systems. Microsoft has implemented its symbolic links to function just like UNIX links. Symbolic links can either be absolute or relative links. Absolute links are links that specify each portion of the path name; relative links are determined relative to where relative–link specifiers are in a specified path
绝对符号链接的一个例子
X: "C:\alpha\beta\absLink\gamma\file"
Link: "absLink" maps to "\\machineB\share"
Modified Path: "\\machineB\share\gamma\file"
一个相对符号链接的例子
X: C:\alpha\beta\link\gamma\file
Link: "link" maps to "..\..\theta"
Modified Path: "C:\alpha\beta\..\..\theta\gamma\file"
Final Path: "C:\theta\gamma\file"
硬链接
硬链接是文件的文件系统表示形式 多个路径引用同一个卷中的单个文件。
要在windows中创建硬链接,请导航到要创建链接的位置并输入以下命令:
mklink /H Link_name target_path
请注意,您可以以任何顺序删除硬链接,而不管它们是按什么顺序创建的。同时,硬链接不能创建时
引用位于不同的本地驱动器中 参考包括网络驱动器。换句话说,其中一个引用是一个网络驱动器 要创建的硬链接与目标在同一路径
结
NTFS支持另一种称为结的链接类型。MSDN对它的定义如下:
连接(也称为软链接)与硬链接的不同之处在于,它引用的存储对象是单独的目录,并且连接可以链接位于同一计算机上不同本地卷上的目录。否则,连接与硬链接的操作相同。
硬链接部分和结节部分的粗体部分显示了两者的基本区别。
命令在窗口中创建一个连接,导航到要创建链接的位置,然后输入:
mklink /J link_name target_path
其他回答
一些例子可能会有所帮助。
创建两个包含数据的文件:
$ 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只是一个指向不存在文件的链接。
加上以上所有答案,查找硬链接和软链接文件的差异可以理解为:
在当前目录中有一个文件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选项)。它节省了不必要的搜索到不同的挂载点。
所以搜索硬链接比搜索软链接快一些(如果我错了或不清楚,请纠正)。
符号链接为文件提供了另一个名称,在某种程度上类似于硬链接。但是,即使文件中仍然存在符号链接,也可以删除文件。
我推荐你们去维基百科:
符号链接 硬链接
以下几点:
与硬链接不同,符号链接可以跨文件系统(大多数情况下)。 符号链接可以指向目录。 硬链接指向一个文件,并允许您使用多个名称引用同一个文件。 只要至少有一个链接,数据就仍然可用。
俗话说,一幅画胜过千言万语。以下是我对它的想象:
下面是我们如何得到这张照片的:
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现在什么都不指向。其他答案讨论了每种方法的利与弊。