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


当前回答

我推荐你们去维基百科:

符号链接 硬链接

以下几点:

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

其他回答

从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

符号链接链接到路径名。它可以在系统文件树中的任何位置,甚至在创建链接时不需要存在。目标路径可以是相对路径,也可以是绝对路径。

硬链接是指向inode的附加指针,这意味着它们只能存在于与目标相同的卷上。到文件的附加硬链接与用于引用文件的“原始”名称难以区分。

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

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

硬链接是Unix,它在Unix和Linux中都是旧的,但符号链接在Linux中是新的。

硬链接inode与原始文件inode相同。但是symbolik链接索引节点不同于原始文件索引节点。

硬链接文件的字节大小与原始文件的字节大小相同。但是符号链接文件的字节大小不像原始文件的字节大小。符号链接文件大小小于原始文件大小。

硬链接是原始文件的镜像副本。符号链接或软链接就像窗口中的快捷方式。

如果您删除原始文件,硬链接将保留其文件,您可以看到硬链接文件的内容。在符号链接中,如果删除原始文件,其符号链接将断开,符号链接仍然保留,但不能显示符号链接内容。

符号链接是新的,它有很多特点,但硬链接是旧的,这就是为什么它有较少的特点。

让我们用终端做一些硬的和象征性的链接: Echo“为什么这么严重”> file.txt

硬链接: Ln file.txt file_hard

symbolick链接: Ln -s file.txt file_sym

让我们看看inode的内容: ls李津

我刚刚发现了一个简单的方法来理解硬链接在一个常见的场景,软件安装。

有一天,我下载了一个软件到下载文件夹进行安装。在我做sudo make install后,一些可执行文件被cped到本地bin文件夹。这里,cp创建硬链接。我对这个软件很满意,但很快就意识到,从长远来看,下载并不是一个好地方。所以我把软件文件夹移动到源目录。好吧,我仍然可以像以前一样运行软件而不用担心任何目标链接的事情,就像在Windows中一样。这意味着硬链接可以直接找到inode和其他文件。