Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

Linux 基礎命令(35):ln 硬連結與軟連結

ln 是 Linux 裡另一個關鍵命令,功能是在另一個位置為同一個檔案建立連結。當多個目錄都要用同一份檔案時,不必每處都放一份副本,只要固定放在某個目錄,其他位置用 ln 連過去即可,省磁碟又省維護。

語法與功能

格式:ln [參數][來源檔案或目錄][目標檔案或目錄]

功能:Linux 檔案系統有所謂的連結(link),可視為檔案的別名,分兩種。硬連結(hard link)讓一個檔案擁有多個名稱;軟連結(symbolic link)則是產生一個特殊檔案,內容指向另一個檔案的位置。硬連結不能跨檔案系統,軟連結可以。

軟連結的特性:
1. 以路徑形式存在,類似 Windows 的捷徑;
2. 可跨檔案系統,硬連結不行;
3. 可以對不存在的檔名建立連結;
4. 可以對目錄建立連結。

硬連結的特性:
1. 以檔案副本形式存在,但不佔實際空間;
2. 不允許對目錄建立硬連結;
3. 只能在同一個檔案系統內建立。

兩點要特別注意:第一,ln 會保持各連結的同步性,改哪一處,其他處都會跟著變;第二,軟連結是 ln -s 來源 目標,只在你指定的位置生成鏡像、不佔磁碟空間,硬連結是 ln 來源 目標(不加 -s),會在指定位置生成一個與來源同大小的檔案,兩者內容都同步變化。

ln 也能一口氣連結多個檔案或目錄:若最後的目的地是已存在的目錄,前面所有來源都會被連結進去;若目的地不是既有目錄,就會報錯。

常用選項

-b 刪除並覆蓋既有連結
-d 允許超級使用者對目錄建立硬連結
-f 強制執行
-i 互動模式,檔案存在時先詢問
-n 把符號連結當一般目錄處理
-s 建立軟連結(符號連結)
-v 顯示詳細處理過程
-S "<備份字尾>" --suffix 指定備份檔字尾
-V "<備份方式>" --version-control 指定備份方式
--help 顯示說明
--version 顯示版本

實作演練

實例一:為檔案建立軟連結

命令:ln -s log2013.log link2013

輸出:

[root@localhost test]# ll

-rw-r--r-- 1 root bin      61 11-13 06:03 log2013.log

[root@localhost test]# ln -s log2013.log link2013

[root@localhost test]# ll

lrwxrwxrwx 1 root root     11 12-07 16:01 link2013 -> log2013.log

-rw-r--r-- 1 root bin      61 11-13 06:03 log2013.log

說明:若 log2013.log 不見了,link2013 就跟著失效。

實例二:為檔案建立硬連結

命令:ln log2013.log ln2013

輸出:

[root@localhost test]# ll

lrwxrwxrwx 1 root root     11 12-07 16:01 link2013 -> log2013.log

-rw-r--r-- 1 root bin      61 11-13 06:03 log2013.log

[root@localhost test]# ln log2013.log ln2013

[root@localhost test]# ll

lrwxrwxrwx 1 root root     11 12-07 16:01 link2013 -> log2013.log

-rw-r--r-- 2 root bin      61 11-13 06:03 ln2013

-rw-r--r-- 2 root bin      61 11-13 06:03 log2013.log

說明:ln2013 與 log2013.log 各項屬性完全相同,連結數變為 2。

實例三:刪除並重建來源檔,觀察兩種連結的差異

輸出:

[root@localhost test]# ll

lrwxrwxrwx 1 root root     11 12-07 16:01 link2013 -> log2013.log

-rw-r--r-- 2 root bin      61 11-13 06:03 ln2013

-rw-r--r-- 2 root bin      61 11-13 06:03 log2013.log

[root@localhost test]# rm -rf log2013.log

[root@localhost test]# ll

lrwxrwxrwx 1 root root     11 12-07 16:01 link2013 -> log2013.log

-rw-r--r-- 1 root bin      61 11-13 06:03 ln2013

[root@localhost test]# touch log2013.log

[root@localhost test]# ll

lrwxrwxrwx 1 root root     11 12-07 16:01 link2013 -> log2013.log

-rw-r--r-- 1 root bin      61 11-13 06:03 ln2013

-rw-r--r-- 1 root root      0 12-07 16:19 log2013.log

[root@localhost test]# vi log2013.log

2013-01

2013-02

2013-03

2013-04

2013-05

2013-06

2013-07

2013-08

2013-09

2013-10

2013-11

2013-12

[root@localhost test]# ll

lrwxrwxrwx 1 root root     11 12-07 16:01 link2013 -> log2013.log

-rw-r--r-- 1 root bin      61 11-13 06:03 ln2013

-rw-r--r-- 1 root root     96 12-07 16:21 log2013.log

[root@localhost test]# cat link2013

2013-01

2013-02

……

[root@localhost test]# cat ln2013

hostnamebaidu=baidu.com

hostnamesina=sina.com

hostnames=true

說明:
1. 來源檔刪除後,硬連結不受影響;軟連結在 CentOS 下會持續閃爍,提示來源已不存在。
2. 重建來源檔後,軟連結恢復正常;但硬連結仍保留刪除前的舊內容,也就是說它與新檔已經脫鉤了。

實例四:把檔案連結到另一目錄下的同名檔案

命令:ln log2013.log test3

輸出:

[root@localhost test]# ln log2013.log test3

[root@localhost test]# cd test3

[root@localhost test3]# ll

-rw-r--r-- 2 root root 96 12-07 16:21 log2013.log

[root@localhost test3]# vi log2013.log

2013-01

……

[root@localhost test3]# ll

-rw-r--r-- 2 root root 80 12-07 16:36 log2013.log

[root@localhost test3]# cd ..

[root@localhost test]# ll

lrwxrwxrwx 1 root root     11 12-07 16:01 link2013 -> log2013.log

-rw-r--r-- 1 root bin      61 11-13 06:03 ln2013

-rw-r--r-- 2 root root   80 12-07 16:36 log2013.log

[root@localhost test]#

說明:test3 裡的是硬連結,改它等於改來源檔,兩邊同步。

實例五:為目錄建立軟連結

命令:ln -sv /opt/soft/test/test3 /opt/soft/test/test5

輸出:

[root@localhost test]# ll

drwxr-xr-x 2 root root   4096 12-07 16:36 test3

drwxr-xr-x 2 root root   4096 12-07 16:57 test5

[root@localhost test5]# ll

lrwxrwxrwx 1 root root 5 12-07 16:57 test3 -> test3

[root@localhost test5]# cd test3

-bash: cd: test3: 符號連接的層數過多

[root@localhost test5]# rm -rf test3

[root@localhost test5]# ln -sv /opt/soft/test/test3 /opt/soft/test/test5

創建指向"/opt/soft/test/test3"的符號鏈接"/opt/soft/test/test5/test3"

[root@localhost test5]# ll

lrwxrwxrwx 1 root root 20 12-07 16:59 test3 -> /opt/soft/test/test3

[root@localhost test5]# cd test3

[root@localhost test3]# ll

總計 4

-rw-r--r-- 2 root root 80 12-07 16:36 log2013.log

[root@localhost test3]# touch log2014.log

[root@localhost test3]# cd ..

[root@localhost test5]# cd ..

說明:
1. 目錄只能建軟連結;
2. 目錄連結必須用絕對路徑,用相對路徑會失敗並出現「符號連接的層數過多」;
3. 在連結目錄中改檔案,來源目錄會同步變化。

查看全套教程:https://www.bianceng.cn/OS/Linux/201301/35075.htm

Copyright © Linux教程網 All Rights Reserved