最近我在一次工作面试中被问到这个问题。我诚实地说,我知道符号链接的行为和如何创建一个,但不了解硬链接的使用,以及它与符号链接的区别。
当前回答
简单地说,硬链接:就是给一个文件添加一个新的名字,也就是说,一个文件可以同时有很多名字,所有的名字都是相等的,没有一个是优先的,硬链接并不是指复制文件的所有内容,并不是说创建一个新的文件,它只是创建一个可供选择的名字。
符号链接(symlink):是指向另一个文件的文件指针,如果符号链接指向一个已存在的文件,但该文件稍后被删除,则符号链接将继续指向相同的文件名,即使该文件名不再命名任何文件。
其他回答
加上以上所有答案,查找硬链接和软链接文件的差异可以理解为:
在当前目录中有一个文件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选项)。它节省了不必要的搜索到不同的挂载点。
所以搜索硬链接比搜索软链接快一些(如果我错了或不清楚,请纠正)。
当原始文件被移动时,硬链接非常有用。例如,将文件从/bin移动到/usr/bin或/usr/local/bin。到/bin中文件的任何符号链接都将被破坏,但是硬链接(直接到文件的inode的链接)不会关心。
硬链接可能占用更少的磁盘空间,因为它们只占用一个目录条目,而符号链接需要自己的inode来存储它所指向的名称。
Hard links also take less time to resolve - symlinks can point to other symlinks that are in symlinked directories. And some of these could be on NFS or other high-latency file systems, and so could result in network traffic to resolve. Hard links, being always on the same file system, are always resolved in a single look-up, and never involve network latency (if it's a hardlink on an NFS filesystem, the NFS server would do the resolution, and it would be invisible to the client system). Sometimes this is important. Not for me, but I can imagine high-performance systems where this might be important.
I also think things like mmap(2) and even open(2) use the same functionality as hardlinks to keep a file's inode active so that even if the file gets unlink(2)ed, the inode remains to allow the process continued access, and only once the process closes it does the file really go away. This allows for much safer temporary files (if you can get the open and unlink to happen atomically, which there may be a POSIX API for that I'm not remembering, then you really have a safe temporary file) where you can read/write your data without anyone being able to access it. Well, that was true before /proc gave everyone the ability to look at your file descriptors, but that's another story.
说到这里,恢复一个在进程a中打开,但在文件系统中未链接的文件需要使用硬链接来重新创建inode链接,这样当打开该文件的进程关闭或离开时,该文件不会消失。
我刚刚发现了一个简单的方法来理解硬链接在一个常见的场景,软件安装。
有一天,我下载了一个软件到下载文件夹进行安装。在我做sudo make install后,一些可执行文件被cped到本地bin文件夹。这里,cp创建硬链接。我对这个软件很满意,但很快就意识到,从长远来看,下载并不是一个好地方。所以我把软件文件夹移动到源目录。好吧,我仍然可以像以前一样运行软件而不用担心任何目标链接的事情,就像在Windows中一样。这意味着硬链接可以直接找到inode和其他文件。
一些例子可能会有所帮助。
创建两个包含数据的文件:
$ 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只是一个指向不存在文件的链接。
另外:
硬链接的读取性能优于符号链接(微性能) 符号链接可以被复制,版本控制,等等。换句话说,它们是一个实际的文件。另一方面,硬链接的级别略低,您会发现,与符号链接相比,提供将硬链接作为硬链接而不是普通文件处理的工具较少