Linux中軟連接與硬連接詳細解讀

目標:node

1.測試環境模擬linux

2.軟連接特性ide

3.硬連接特性測試

4.總結rem


1.測試環境模擬


1it

2table

3class

4test

5import

6

7

8

9

10

11

12

13

[root@localhost home]# mkdir test 建立測試文件夾

[root@localhost home]# cd test/  進入測試文件夾

[root@localhost test]# touch link  建立原文件link

[root@localhost test]# echo "my name is link">>link  寫入內容到原文件link

[root@localhost test]# cat link  查看原文件內容

my name is link

[root@localhost test]# ln -s link softlink  建立軟連接

[root@localhost test]# ln link hardlink  建立硬連接

[root@localhost test]# ll

total 8

-rw-r--r--. 2 root root 16 Dec 8 18:21 hardlink  硬連接

-rw-r--r--. 2 root root 16 Dec 8 18:21 link  原文件

lrwxrwxrwx. 1 root root 4 Dec 8 18:22 softlink -> link  軟連接


2.軟連接特性


1

2

3

-rw-r--r--. 2 root root 16 Dec 8 18:21 link 原文件

 

lrwxrwxrwx. 1 root root 4 Dec 8 18:22 softlink -> link 軟連接

對比差異是否是發現有幾點不一樣?

1.原文件inode爲2軟連接爲1

2.權限不一樣

3.文件大小不一樣

4.軟連接後面有個指向link的標誌



1

2

[root@localhost test]# cat softlink

my name is link

軟連接內容同樣。



1

2

3

4

[root@localhost test]# rm softlink

rm: remove symbolic link ‘softlink’? y

[root@localhost test]# cat link

my name is link

刪除軟連接原文件是正常的



1

2

3

4

[root@localhost test]# rm link

rm: remove regular file ‘link’? y

[root@localhost test]# cat softlink

cat: softlink: No such file or directory

刪除原文件軟連接找不到文件了,綜上證實軟連接就是個快捷方式而已!!!


若是我把軟連接更名稱會發生什麼?

1

2

3

4

5

6

7

[root@localhost test]# mv softlink testsoftlink

[root@localhost test]# ll

total 8

-rw-r--r--. 1 root root 16 Dec 8 18:36 link

lrwxrwxrwx. 1 root root 4 Dec 8 18:34 testsoftlink -> link

[root@localhost test]# cat testsoftlink

my name is link

實驗證實更名並無什麼卵用,打開軟連接照樣能夠看到內容,爲何?

由於linux識別一個文件不看名稱,看inode值!!!

也就是說inode值相同文件內容同樣。


那麼文件能夠建立軟連接,目錄能夠嗎?

1

2

3

4

5

[root@localhost test]# mkdir wj

[root@localhost test]# ln -s wj softwj

[root@localhost test]# ll

lrwxrwxrwx. 1 root root 2 Dec 8 18:54 softwj -> wj

drwxr-xr-x. 2 root root 6 Dec 8 18:54 wj

目錄能夠建立軟連接


3.硬連接特性


1

2

3

4

5

6

[root@localhost test]# ll

total 8

-rw-r--r--. 2 root root 16 Dec 8 18:36 hardlink

-rw-r--r--. 2 root root 16 Dec 8 18:36 link

[root@localhost test]# cat hardlink

my name is link

觀察得出硬連接就是個原文件的備份



1

2

3

4

[root@localhost test]# rm link

rm: remove regular file ‘link’? y

[root@localhost test]# cat hardlink

my name is link

刪除原文件,硬連接是能夠看到內容的,so。這就是與軟連接的不一樣之處之一。


那麼硬連接是否能夠像軟連接同樣建立目錄連接呢?

1

2

3

[root@localhost test]# mkdir cs

[root@localhost test]# ln cs hardcs

ln: ‘cs’: hard link not allowed for directory

不能夠的。爲何呢?

由於那個惟一值!若是目錄inode同樣會怎麼樣?

在訪問軟連接的時候經過軟連接直接的跳轉到原文件,這樣就訪問了內容

在訪問軟連接目錄的時候經過遍歷目錄內容也能夠找到,就算文件夾裏面inode值有同樣的循環了,linux能夠在8個循環內終結。

可是

若是咱們的硬連接訪問了,其實原文件變不變與它已經沒有關係了

咱們的硬連接若是有硬連接目錄,那麼遍歷的時候遇到inode值同樣的目錄裏面的內容,所有遍歷一遍,環路至少在目錄的linux系統中終結不了,因此硬連接目錄是不能建立滴!!!


4.總結

軟連接相似快捷方式,原文件內容變了軟連接的也會變,影響文件的不是名稱而是inode值,軟連接是能夠建立軟連接目錄的。

硬連接相似備份,原文件內容變化不影響硬連接,因此一般在工做用做爲快照使用,硬連接沒有硬連接目錄。

相關文章
相關標籤/搜索