ln是linux中又一個非常重要命令,它的功能是為某一個檔案在另外一個位置建立一個同步的連結.當我們需要在不同的目錄,用到相同的檔案時,我們不需要在每一個需要的目錄下都放一個必須相同的檔案,我們只要在某個固定的目錄,放上該檔案,然後在 其它的目錄下用ln命令連結(link)它就可以,不必重複的佔用磁碟空間。
1.命令格式:
1 |
ln [引數][原始檔或目錄][目標檔案或目錄] |
2.命令功能:
Linux檔案系統中,有所謂的連結(link),我們可以將其視為檔案的別名,而連結又可分為兩種 : 硬連結(hard link)與軟連結(symbolic link),硬連結的意思是一個檔案可以有多個名稱,而軟連結的方式則是產生一個特殊的檔案,該檔案的內容是指向另一個檔案的位置。硬連結是存在同一個檔案系統中,而軟連結卻可以跨越不同的檔案系統。
軟連結:
1.軟連結,以路徑的形式存在。類似於Windows作業系統中的快捷方式
2.軟連結可以 跨檔案系統 ,硬連結不可以
3.軟連結可以對一個不存在的檔名進行連結
4.軟連結可以對目錄進行連結
硬連結:
1.硬連結,以檔案副本的形式存在。但不佔用實際空間。
2.不允許給目錄建立硬連結
3.硬連結只有在同一個檔案系統中才能建立
這裡有兩點要注意:
第一,ln命令會保持每一處連結檔案的同步性,也就是說,不論你改動了哪一處,其它的檔案都會發生相同的變化;
第二,ln的連結又分軟連結和硬連結兩種,軟連結就是ln –s 原始檔 目標檔案,它只會在你選定的位置上生成一個檔案的映象,不會佔用磁碟空間,硬連結 ln 原始檔 目標檔案,沒有引數-s, 它會在你選定的位置上生成一個和原始檔大小相同的檔案,無論是軟連結還是硬連結,檔案都保持同步變化。
ln指令用在連結檔案或目錄,如同時指定兩個以上的檔案或目錄,且最後的目的地是一個已經存在的目錄,則會把前面指定的所有檔案或目錄複製到該目錄中。若同時指定多個檔案或目錄,且最後的目的地並非是一個已存在的目錄,則會出現錯誤資訊。
3.命令引數:
必要引數:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
-b 刪除,覆蓋以前建立的連結 -d 允許超級使用者製作目錄的硬連結 -f 強制執行 -i 互動模式,檔案存在則提示使用者是否覆蓋 -n 把符號連結視為一般目錄 -s 軟連結(符號連結) -v 顯示詳細的處理過程 |
選擇引數:
1 2 3 4 5 6 7 |
-S “-S<字尾備份字串> ”或 “--suffix=<字尾備份字串>” -V “-V<備份方式>”或“--version-control=<備份方式>” --help 顯示幫助資訊 --version 顯示版本資訊 |
4.使用例項:
例項1:給檔案建立軟連結
命令:
1 |
ln -s log2013.log link2013 |
輸出:
1 2 3 4 5 6 7 8 9 10 11 |
[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,如果log2013.log丟失,link2013將失效
例項2:給檔案建立硬連結
命令:
1 |
ln log2013.log ln2013 |
輸出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[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 |
說明:
為log2013.log建立硬連結ln2013,log2013.log與ln2013的各項屬性相同
例項3:接上面兩例項,連結完畢後,刪除和重建連結原檔案
命令:
輸出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
[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 ---xrw-r-- 1 root bin 302108 11-13 06:03 log2012.log -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 2013-03 2013-04 2013-05 2013-06 2013-07 2013-08 2013-09 2013-10 2013-11 2013-12 [root@localhost test]# cat ln2013 hostnamebaidu=baidu.com hostnamesina=sina.com hostnames=true |
說明:
1.原始檔被刪除後,並沒有影響硬連結檔案;軟連結檔案在centos系統下不斷的閃爍,提示原始檔已經不存在
2.重建原始檔後,軟連結不在閃爍提示,說明已經連結成功,找到了連結檔案系統;重建後,硬連結檔案並沒有受到原始檔影響,硬連結檔案的內容還是保留了刪除前原始檔的內容,說明硬連結已經失效
例項4:將檔案連結為另一個目錄中的相同名字
命令:
1 |
ln log2013.log test3 |
輸出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
[root@localhost test]# ln log2013.log test3 [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 96 12-07 16:21 log2013.log [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 2013-02 2013-03 2013-04 2013-05 2013-06 2013-07 2013-08 2013-09 2013-10[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目錄中建立了log2013.log的硬連結,修改test3目錄中的log2013.log檔案,同時也會同步到原始檔
例項5:給目錄建立軟連結
命令:
1 |
ln -sv /opt/soft/test/test3 /opt/soft/test/test5 |
輸出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
[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 test]# cd 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]# [root@localhost test5]# [root@localhost test5]# ll lrwxrwxrwx 1 root root 5 12-07 16:57 test3 -> test3 [root@localhost test5]# rm -rf test3 [root@localhost test5]# ll [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]# [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]# ll 總計 4 -rw-r--r-- 2 root root 80 12-07 16:36 log2013.log -rw-r--r-- 1 root root 0 12-07 17:05 log2014.log [root@localhost test3]# cd .. [root@localhost test5]# cd .. |
說明:
1.目錄只能建立軟連結
2.目錄建立連結必須用絕對路徑,相對路徑建立會不成功,會提示:符號連線的層數過多 這樣的錯誤
3.在連結目標目錄中修改檔案都會在原始檔目錄中同步變化
本系列文章:
每天一個 Linux 命令(1):ls命令
每天一個 Linux 命令(2):cd命令
每天一個 Linux 命令(3):pwd命令
每天一個 Linux 命令(4):mkdir命令
每天一個 Linux 命令(5):rm 命令
每天一個 Linux 命令(6):rmdir 命令
每天一個 Linux 命令(7):mv命令
每天一個 Linux 命令(8):cp 命令
每天一個 Linux 命令(9):touch 命令
每天一個 Linux 命令(10):cat 命令
每天一個 Linux 命令(11):nl 命令
每天一個 Linux 命令(12):more 命令
每天一個 Linux 命令(13):less 命令
每天一個 Linux 命令(14):head 命令
每天一個 Linux 命令(15):tail 命令
每天一個 Linux 命令(16):which命令
每天一個 Linux 命令(17):whereis 命令
每天一個 Linux 命令(18):locate 命令
每天一個 Linux 命令(19):find 命令概覽
每天一個 Linux 命令(20):find命令之exec
每天一個 Linux 命令(21):find命令之xargs
每天一個 Linux 命令(22):find 命令的引數詳解
每天一個 Linux 命令(23):Linux 目錄結構
每天一個 Linux 命令(24):Linux 檔案型別與副檔名
每天一個 Linux 命令(25):Linux 檔案屬性詳解
每天一個 Linux 命令(26):用 SecureCRT 來上傳和下載檔案
每天一個 Linux 命令(27):linux chmod 命令
每天一個 Linux 命令(28):tar 命令
每天一個 Linux 命令(29): chgrp 命令
每天一個 Linux 命令(30): chown 命令
每天一個 Linux 命令(31): /etc/group 檔案詳解
每天一個 Linux 命令(32):gzip 命令
每天一個 Linux 命令(33):df 命令
每天一個 Linux 命令(34): du 命令