最近我在一次工作面试中被问到这个问题。我诚实地说,我知道符号链接的行为和如何创建一个,但不了解硬链接的使用,以及它与符号链接的区别。
当前回答
从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
其他回答
俗话说,一幅画胜过千言万语。以下是我对它的想象:
下面是我们如何得到这张照片的:
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都有一个inodennumber。inode号用于访问inode。到文件的所有硬链接可能有不同的名称,但共享相同的inode号。因为所有的硬链接都有相同的inodennumber(访问相同的inode),所以它们都指向相同的物理内存。
符号链接是一种特殊的文件。因为它也是一个文件,它将有一个文件名和一个inode号。如上所述,inode号访问指向数据的inode。现在,符号链接的特殊之处在于,符号链接中的inodennumbers访问那些指向另一个文件的“路径”的inode。更具体地说,符号链接中的inode号访问指向另一个硬链接的inode。
当我们在GUI中移动、复制、删除文件时,我们使用的是文件的硬链接,而不是物理内存。当我们删除一个文件时,我们正在删除该文件的硬链接。我们并没有清除物理内存。如果文件的所有硬链接都被删除,那么将无法访问存储的数据,尽管它可能仍然存在于内存中
符号链接为文件提供了另一个名称,在某种程度上类似于硬链接。但是,即使文件中仍然存在符号链接,也可以删除文件。
我刚刚发现了一个简单的方法来理解硬链接在一个常见的场景,软件安装。
有一天,我下载了一个软件到下载文件夹进行安装。在我做sudo make install后,一些可执行文件被cped到本地bin文件夹。这里,cp创建硬链接。我对这个软件很满意,但很快就意识到,从长远来看,下载并不是一个好地方。所以我把软件文件夹移动到源目录。好吧,我仍然可以像以前一样运行软件而不用担心任何目标链接的事情,就像在Windows中一样。这意味着硬链接可以直接找到inode和其他文件。