1. 程式人生 > >ln 硬鏈接與軟鏈接

ln 硬鏈接與軟鏈接

windows dir link roo symbol hard link nod 包括 靜態文件

1. 命令功能

ln 可以看做是link的簡寫,功能是創建鏈接文件,鏈接文件包括硬鏈接(hard link)和軟鏈接(符號鏈接,symbolic link)

2. 語法格式

ln [option] source target

ln 選項 源文件或目錄 目標文件或目錄

參數

參數說明

無選項

創建硬鏈接

-s

創建軟鏈接(符號鏈接)

目錄沒有硬鏈接,只有軟鏈接。

3. 硬軟鏈接文件知識

1. 硬鏈接

硬鏈接文件創建方式:ln 源文件 目標文件

每個文件都有一個inode(索引)節點,這個inode就是指向文件在磁盤中具體存放的位置編號。創建硬鏈接,就是在創建一個文件名,這個文件名指向同一個inode索引,相當於給文件的另一個入口。例如到一個房子開始只有一個門能進入,現在又開了另一個門當做出口。這樣做的好處,例如備份文件,刪除源文件,只是把源文件名刪除了,還可以通過硬鏈接文件訪問這個文件,放置誤刪除。

硬鏈接的特性:

  1. 具有相同inode節點的多個文件互為硬鏈接。
  2. 刪除源文件或硬鏈接文件其中之一,文件實體沒有被刪除。
  3. 只有刪除源文件及全部硬鏈接文件後,文件實體才會被刪除。
  4. 可以給文件設置硬鏈接,來防止重要文件被誤刪。
  5. 硬鏈接可以用rm命令刪除。
  6. ls –lih 查看第三列,即硬鏈接數。
  7. 對於靜態文件(文件沒有被調用),當對應的硬鏈接數為0時,文件就會被刪除

實例:創建硬鏈接

[root@localhost DIR]# ln 123 abc

[root@localhost DIR]# ls -l

total 0

-rw-r--r--. 2 root root 0 Mar 13 23:21 123

-rw-r--r--. 2 root root 0 Mar 13 23:21 abc #abc是123的硬鏈接文件

[root@localhost DIR]# ls –lih

total 0

786446 -rw-r--r--. 2 root root 0 Mar 13 23:21 123 #inode號相同;2 表示有硬鏈接數:3

786446 -rw-r--r--. 2 root root 0 Mar 13 23:21 abc

[root@localhost DIR]# ln 123 def

[root@localhost DIR]# ls -lih

total 0

786446 -rw-r--r--. 3 root root 0 Mar 13 23:21 123 # 硬鏈接數有3

786446 -rw-r--r--. 3 root root 0 Mar 13 23:21 abc

786446 -rw-r--r--. 3 root root 0 Mar 13 23:21 def

[root@localhost DIR]# ls -lih 123

786446 -rw-r--r--. 3 root root 0 Mar 13 23:21 123

2. 軟鏈接

軟鏈接(符號鏈接),類似windows中的快捷方式。

軟鏈接創建方式:ln –s 源文件 目標文件

[root@localhost home]# ls -l test.txt

-rw-r--r--. 1 root root 27 Mar 14 22:45 test.txt

[root@localhost home]# ln -s test.txt test.txt_link #創建軟鏈接

[root@localhost home]# ls -l test.txt test.txt_link

-rw-r--r--. 1 root root 27 Mar 14 22:45 test.txt

lrwxrwxrwx. 1 root root 8 Mar 14 22:45 test.txt_link -> test.txt #l:軟鏈接標識符

軟鏈接文件和源文件的inode號不一樣,鏈接數也不一樣

[root@localhost home]# ls -lih test.txt test.txt_link

786434 -rw-r--r--. 1 root root 27 Mar 14 22:45 test.txt

786448 lrwxrwxrwx. 1 root root 8 Mar 14 22:45 test.txt_link -> test.txt

ln 硬鏈接與軟鏈接