Hard Link in File System

Table of Contents

1 什么是硬链接

In computing, a hard link is a directory entry that associates a name with a file on a file system.

hardlink.png

Figure 1: An illustration of the concept of hard linking.

In the figure 1, two hard links, named "A.TXT" and "B.TXT", point to the same physical data.

参考:https://en.wikipedia.org/wiki/Hard_link

1.1 什么是软链接

Symbolic link or soft link, unlike hard link, points to filename, not file data itself.

2 创建和检测硬链接

All files are hard links, with link counts of at least 1.

2.1 On MS Windows

Hard links can be created using the mklink /H command on Windows NT 6.0 and later systems (such as Windows Vista), and in earlier systems (Windows XP, Windows Server 2003) using fsutil.exe hardlink create.

可以用命令 fsutil.exe hardlink list 来检测某个文件有哪些硬链接。如:

C:\windows\system32>fsutil.exe hardlink list C:\Windows\System32\notepad.exe
\Windows\System32\notepad.exe
\Windows\winsxs\amd64_microsoft-windows-notepadwin_31bf3856ad364e35_6.1.7600.16385_none_9ebebe8614be1470\notepad.exe

2.2 On Unix-like system

Hard links can be created using the ln or link command-line utilities on Unix-like system.

如何查找当前目录下的硬链接文件。
方法一:
用命令 ls -l 可以得到文件硬链接的引用数。
用命令 ls -i 可以得到文件inode号码(同一文件的inode号码相同)。
用命令 find . -inum NUM 可以得到当前目录下inode号码为NUM的所有文件。

方法二:
用命令 find . -samefile file1 可以得到和文件file1硬链接到同一文件的所有文件。

3 Limitations of hard link

  1. Many modern operating systems do NOT allow hard links to directories.
  2. Hard links can be created to files only on the same volume.

Author: cig01

Created: <2011-07-23 Sat 00:00>

Last updated: <2015-08-29 Sat 12:59>

Creator: Emacs 25.1.1 (Org mode 9.0.7)