SYNOPSIS
ln [OPTION]... [-T] TARGET LINK_NAME (1st form)
ln [OPTION]... TARGET (2nd form)
ln [OPTION]... TARGET... DIRECTORY (3rd form)
ln [OPTION]... -t DIRECTORY TARGET... (4th form)
- form1:創建一個名為LINK_NAME 的連接文件指向原文件TARGET
- form2:在當前目錄中創建一個與TARGET同名的鏈接文件(TARGET不能在當前目錄,目標可以是絕對路徑,也可以是相對路徑)
- form3 | form4:在指定的目錄DIRECTORY中,為每一個原文件TARGET創建一個鏈接文件。
硬鏈接與軟鏈接(-s)的聯系與區別(默認是建立硬鏈接)
我們知道文件都有文件名與數據,這在 Linux 上被分成兩個部分:用戶數據 (user data) 與元數據 (metadata)。用戶數據,即文件數據塊 (data block),數據塊是記錄文件真實內容的地方;而元數據則是文件的附加屬性,如文件大小、創建時間、所有者等信息。在 Linux 中,元數據中的 inode 號(inode 是文件元數據的一部分但其並不包含文件名,inode 號即索引節點號)才是文件的唯一標識而非文件名。文件名僅是為了方便人們的記憶和使用,系統或程序通過 inode 號尋找正確的文件數據塊。圖 1.展示了程序通過文件名獲取文件內容的過程。
圖 1. 通過文件名打開文件
由於硬鏈接是有着相同 inode 號僅文件名不同的文件,因此硬鏈接存在以下幾點特性:
- 文件有相同的 inode 及 data block;
- 只能對已存在的文件進行創建;
- 不能交叉文件系統進行硬鏈接的創建;
- 不能對目錄進行創建,只可對文件創建;
- 刪除一個硬鏈接文件並不影響其他有相同 inode 號的文件。
可選參數如下
--backup[=CONTROL]
make a backup of each existing destination file
-b like --backup but does not accept an argument
-d, -F, --directory
allow the superuser to attempt to hard link directories (note: will probably fail
due to system restrictions, even for the superuser)
-f, --force
remove existing destination files
-i, --interactive
prompt whether to remove destinations
-L, --logical
dereference TARGETs that are symbolic links
-n, --no-dereference
treat LINK_NAME as a normal file if it is a symbolic link to a directory
-P, --physical
make hard links directly to symbolic links
-r, --relative
create symbolic links relative to link location
-s, --symbolic
make symbolic links instead of hard links
-S, --suffix=SUFFIX
override the usual backup suffix
-t, --target-directory=DIRECTORY
specify the DIRECTORY in which to create the links
-T, --no-target-directory
treat LINK_NAME as a normal file always
-v, --verbose
print name of each linked file
--help display this help and exit
--version
output version information and exit
每個文件存在兩個計數器:i_count 與 i_nlink,即引用計數與硬鏈接計數。結構體 inode 中的 i_count 用於跟蹤文件被訪問的數量,而 i_nlink 則是上述使用 ls -l 等命令查看到的文件硬鏈接數。或者說 i_count 跟蹤文件在內存中的情況,而 i_nlink 則是磁盤計數器。當文件被刪除時,則 i_nlink 先被設置成 0。文件的這兩個計數器使得 Linux 系統升級或程序更新變的容易。系統或程序可在不關閉的情況下(即文件 i_count 不為 0),將新文件以同樣的文件名進行替換,新文件有自己的 inode 及 data block,舊文件會在相關進程關閉后被完整的刪除。
查看文件是否是硬鏈接
$ touch file1 # 創建新文件 file1
$ touch file2 # 創建新文件 file2
$ ln file1 file3 # 為 file1 創建硬鏈接 file3
$ ls -l
total 0
-rw-r--r-- 2 root root 0 Aug 12 16:59 file1
-rw-r--r-- 1 root root 0 Aug 12 17:00 file2
-rw-r--r-- 2 root root 0 Aug 12 16:59 file3
結果的第二列數字就是指向該文件的硬鏈接數. 注意, 硬鏈接和原文件是無法區分的. 所以 file3 是 file1 的硬鏈接也可以看作 file1 是 file3 的硬鏈接. 所以該數字大於 2 即說明該文件是硬鏈接.
查看文件的 inode number
ls -i # 可以與 ls -l 一起使用, 即 ls -il
$ ls -il
total 0
267105 -rw-r--r-- 2 root root 0 Aug 12 16:59 file1
267106 -rw-r--r-- 1 root root 0 Aug 12 17:00 file2
267105 -rw-r--r-- 2 root root 0 Aug 12 16:59 file3
這時結果的第一列就是文件的 inode number, 可以看出由於 file1 和 file3 互為硬鏈接, 所以他們的 inode number 相同.
如何找出所有硬鏈接到某個文件的文件?
首先使用
1
ls -i
查看文件的 inode number
然后使用
find -inum
查找所有指向該 inode 的文件
例子:
$ find . -inum 267105
./file3
./file1
ref :https://www.ibm.com/developerworks/cn/linux/l-cn-hardandsymb-links/