Linux 軟鏈接和硬鏈接

在Linux 系統(tǒng)中,鏈接是一種文件共享的方式,它分為軟鏈接和硬鏈接,對(duì)應(yīng)的命令是ln

<font color=CornflowerBlue>什么是軟連接</font>

軟鏈接又叫符號(hào)鏈接, 它類似于 windows系統(tǒng)中的快捷方式,常用于將目錄層次較深的文件鏈接到比較容易訪問(wèn)的目錄中

<font color=CornflowerBlue>創(chuàng)建軟鏈接</font>

使用 ln -s 源文件 軟鏈接 命令就可以創(chuàng)建一個(gè)軟鏈接,軟鏈接可以對(duì)文件、目錄、跨文件系統(tǒng)的文件或目錄

  • 創(chuàng)建文件的軟鏈接
[root@ecs-centos-7 tt]# stat a.txt | grep Links
Device: fd01h/64769d    Inode: 131096      Links: 1

[root@ecs-centos-7 tt]# ln -s a.txt sa.txt
[root@ecs-centos-7 tt]# ls -il
total 4
131096 -rw-r--r-- 1 root root 15 Aug 25 20:19 a.txt
131100 lrwxrwxrwx 1 root root  5 Aug 25 20:22 sa.txt -> a.txt

[root@ecs-centos-7 tt]# stat a.txt | grep Links
Device: fd01h/64769d    Inode: 131096      Links: 1

上面的例子中,創(chuàng)建了 a.txt 的軟鏈接 sa.txt ,創(chuàng)建成功之后,軟鏈接文件名后面會(huì)有一個(gè) -> 符號(hào)指向原始文件

創(chuàng)建軟鏈接前后的 stat a.txt | grep Links 命令是查看原始文件 a.txt 的鏈接數(shù)量,從例子中可以看出,創(chuàng)建軟鏈接之后,文件鏈接數(shù)保持不變

  • 創(chuàng)建目錄的軟鏈接
[root@ecs-centos-7 tt]# mkdir tmp
[root@ecs-centos-7 tt]# ln -s tmp/ stmp
[root@ecs-centos-7 tt]# ll
total 4
lrwxrwxrwx 1 root root    4 Aug 26 00:38 stmp -> tmp/
drwxr-xr-x 2 root root 4096 Aug 26 00:38 tmp
[root@ecs-centos-7 tt]# echo "1234" > stmp/a.txt
[root@ecs-centos-7 tt]# cat tmp/a.txt 
1234
  • 創(chuàng)建跨文件系統(tǒng)的軟連接

首先查看所有的文件系統(tǒng),結(jié)果如下:

[root@ecs-centos-7 tt]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        909M     0  909M   0% /dev
tmpfs           919M     0  919M   0% /dev/shm
tmpfs           919M  8.7M  911M   1% /run
tmpfs           919M     0  919M   0% /sys/fs/cgroup
/dev/vda1        40G   20G   18G  53% /
tmpfs           184M     0  184M   0% /run/user/0

從上面的例子可以看到,目前有 devtmpfs, tmpfs, /dev/vda1 三種文件系統(tǒng),我們選取 tmpfs/dev/vda1來(lái)測(cè)試跨文件系統(tǒng)的軟鏈接,具體測(cè)試如下:

[root@ecs-centos-7 tt]# touch /run/test.txt
[root@ecs-centos-7 tt]# cat /run/test.txt 
[root@ecs-centos-7 tt]# ln -s /run/test.txt /st.txt
[root@ecs-centos-7 tt]# echo "haha" > /st.txt 
[root@ecs-centos-7 tt]# cat /run/test.txt 
haha

上面的測(cè)試中,首先在tmpfs 文件系統(tǒng)的/run目錄中創(chuàng)建 test.txt 文件,此時(shí)文件是沒有內(nèi)容的

然后,在/dev/vda1文件系統(tǒng)的/ 目錄創(chuàng)建 /run/test.txt 的軟鏈接 st.txt, 同時(shí)往 st.txt 中寫入字符串 haha

最后,查看原始文件 /run/test.txt, 內(nèi)容和軟鏈接文件是相同的

<font color=CornflowerBlue>修改、刪除軟鏈接</font>

軟鏈接也是文件的一種,只要有權(quán)限,自然是可以修改的

  • 修改軟鏈接

軟鏈接是原始文件的快捷方式,所以修改軟鏈接文件實(shí)際上修改的是原始文件,具體的請(qǐng)看以下實(shí)例

[root@ecs-centos-7 tt]# cat sa.txt 
this is a file
[root@ecs-centos-7 tt]# cat a.txt 
this is a file
[root@ecs-centos-7 tt]# echo "111" >> sa.txt 
[root@ecs-centos-7 tt]# cat sa.txt 
this is a file
111
[root@ecs-centos-7 tt]# cat a.txt 
this is a file
111

例子中,對(duì)軟鏈接 sa.txt 進(jìn)行了修改,原始文件 a.txt 的內(nèi)容也跟著同步改變了

  • 刪除原始文件

軟鏈接文件是原始文件的快捷方式,上面的例子中可以看到,刪除原始文件之后,軟鏈接文件后面的箭頭指向的原始文件 a.txt 在不停機(jī)的閃動(dòng),表示鏈接指向的原始文件已經(jīng)失效了

通過(guò)cat sa.txt 命令查看鏈接內(nèi)容時(shí)會(huì)提示文件不存在

<font color=CornflowerBlue>什么是硬鏈接</font>

硬鏈接可以看成文件的別名, 它和原始文件擁有相同的索引節(jié)點(diǎn)(關(guān)于索引節(jié)點(diǎn)的知識(shí)請(qǐng)查看 理解Linux中inodes)

刪除任何一處的文件,都不影響另一處文件的正常訪問(wèn),只有刪除最后一個(gè)文件之后,文件內(nèi)容的數(shù)據(jù)塊才會(huì)被刪除

熟悉 c/c++ 指針的朋友可以把硬鏈接看成指向同一塊內(nèi)存的多個(gè)指針變量,文件內(nèi)容存儲(chǔ)在指針變量指向的內(nèi)存塊中

指針變量超出其作用域時(shí)會(huì)被系統(tǒng)回收,此時(shí)不會(huì)回收它所指向的內(nèi)存的,當(dāng)所有的指針變量都被回收時(shí),才會(huì)回收它們所指向的內(nèi)存(這里假設(shè)應(yīng)用程序能正確的處理堆內(nèi)存的回收)

<font color=CornflowerBlue>創(chuàng)建、修改、刪除硬鏈接</font>

  • 創(chuàng)建硬鏈接

創(chuàng)建硬鏈接命令: ln 源文件 硬鏈接

[root@ecs-centos-7 tt]# stat b.txt | grep Links
Device: fd01h/64769d    Inode: 131096      Links: 1

root@ecs-centos-7 tt]# ln b.txt hb.txt
[root@ecs-centos-7 tt]# ls -li
total 8
131096 -rw-r--r-- 2 root root 17 Aug 25 23:40 b.txt
131096 -rw-r--r-- 2 root root 17 Aug 25 23:40 hb.txt

[root@ecs-centos-7 tt]# stat b.txt | grep Links
Device: fd01h/64769d    Inode: 131096      Links: 2

上面的例子中,創(chuàng)建了 b.txt 的硬鏈接 hb.txt

創(chuàng)硬鏈接前后的 stat b.txt | grep Links 命令是查看 b.txt 的鏈接數(shù)量,從例子中可以看出,創(chuàng)建硬鏈接后文件鏈接數(shù)增加了

  • 修改硬鏈接,刪除原始文件

從以上例子中可以看出,修改硬鏈接文件內(nèi)容,原始文件也會(huì)同步修改,而刪除原始文件時(shí),硬鏈接文件內(nèi)容沒有任何變化

<font color=CornflowerBlue>硬鏈接注意事項(xiàng)</font>

  • ==無(wú)法跨文件系統(tǒng)創(chuàng)建硬鏈接==
[root@ecs-centos-7 /]# ln /run/test.txt /htest.txt
ln: failed to create hard link ‘/htest.txt’ => ‘/run/test.txt’: Invalid cross-device link
  • ==不允許創(chuàng)建目錄的硬鏈接==
[root@ecs-centos-7 tt]# ln tmp/ htmp
ln: ‘tmp/’: hard link not allowed for directory

<font color=CornflowerBlue>總結(jié)</font>

以上分別介紹了軟鏈接和硬鏈接,下面以圖表的形式總結(jié)下兩者的異同

鏈接 索引節(jié)點(diǎn)號(hào) 權(quán)限 大小 創(chuàng)建目錄鏈接 跨文件系統(tǒng) 原始文件的鏈接計(jì)數(shù)
軟鏈接 新的索引節(jié)點(diǎn) 有自己的文件屬性和權(quán)限 和原始文件不同 不會(huì)增加
硬鏈接 和原始文件相同 和原始文件相同 和原始文件相同 不能 不能 會(huì)增加
推薦閱讀

理解Linux中inodes

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容