濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > linux命令大全之ln命令詳解(創(chuàng)建軟鏈接和硬鏈接)

linux命令大全之ln命令詳解(創(chuàng)建軟鏈接和硬鏈接)

熱門標(biāo)簽:如何找電銷機(jī)器人 無錫ai智能語(yǔ)音電銷機(jī)器人廠家 濟(jì)南智能語(yǔ)音電銷機(jī)器人加盟 易聯(lián)系統(tǒng)外呼營(yíng)銷來回呼 江西意向度高的羿智云外呼系統(tǒng) 河南語(yǔ)音電銷機(jī)器人加盟公司 抖音商家地圖標(biāo)注入駐店 電銷機(jī)器人怎么打卡 無錫真人電銷機(jī)器人供應(yīng)商

ln是linux中又一個(gè)非常重要命令,它的功能是為某一個(gè)文件在另外一個(gè)位置建立一個(gè)同步的鏈接.當(dāng)我們需要在不同的目錄,用到相同的文件時(shí),我們不需要在每一個(gè)需要的目錄下都放一個(gè)必須相同的文件,我們只要在某個(gè)固定的目錄,放上該文件,然后在 其它的目錄下用ln命令鏈接(link)它就可以,不必重復(fù)的占用磁盤空間。

1.命令格式:
 ln [參數(shù)][源文件或目錄][目標(biāo)文件或目錄]

2.命令功能:
Linux文件系統(tǒng)中,有所謂的鏈接(link),我們可以將其視為檔案的別名,而鏈接又可分為兩種 : 硬鏈接(hard link)與軟鏈接(symbolic link),硬鏈接的意思是一個(gè)檔案可以有多個(gè)名稱,而軟鏈接的方式則是產(chǎn)生一個(gè)特殊的檔案,該檔案的內(nèi)容是指向另一個(gè)檔案的位置。硬鏈接是存在同一個(gè)文件系統(tǒng)中,而軟鏈接卻可以跨越不同的文件系統(tǒng)。

軟鏈接:
1.軟鏈接,以路徑的形式存在。類似于Windows操作系統(tǒng)中的快捷方式
2.軟鏈接可以 跨文件系統(tǒng) ,硬鏈接不可以
3.軟鏈接可以對(duì)一個(gè)不存在的文件名進(jìn)行鏈接
4.軟鏈接可以對(duì)目錄進(jìn)行鏈接

硬鏈接:
1.硬鏈接,以文件副本的形式存在。但不占用實(shí)際空間。
2.不允許給目錄創(chuàng)建硬鏈接
3.硬鏈接只有在同一個(gè)文件系統(tǒng)中才能創(chuàng)建
這里有兩點(diǎn)要注意:
第一,ln命令會(huì)保持每一處鏈接文件的同步性,也就是說,不論你改動(dòng)了哪一處,其它的文件都會(huì)發(fā)生相同的變化;
第二,ln的鏈接又分軟鏈接和硬鏈接兩種,軟鏈接就是ln –s 源文件 目標(biāo)文件,它只會(huì)在你選定的位置上生成一個(gè)文件的鏡像,不會(huì)占用磁盤空間,硬鏈接 ln 源文件 目標(biāo)文件,沒有參數(shù)-s, 它會(huì)在你選定的位置上生成一個(gè)和源文件大小相同的文件,無論是軟鏈接還是硬鏈接,文件都保持同步變化。
ln指令用在鏈接文件或目錄,如同時(shí)指定兩個(gè)以上的文件或目錄,且最后的目的地是一個(gè)已經(jīng)存在的目錄,則會(huì)把前面指定的所有文件或目錄復(fù)制到該目錄中。若同時(shí)指定多個(gè)文件或目錄,且最后的目的地并非是一個(gè)已存在的目錄,則會(huì)出現(xiàn)錯(cuò)誤信息。

3.命令參數(shù):
必要參數(shù):
-b 刪除,覆蓋以前建立的鏈接
-d 允許超級(jí)用戶制作目錄的硬鏈接
-f 強(qiáng)制執(zhí)行
-i 交互模式,文件存在則提示用戶是否覆蓋
-n 把符號(hào)鏈接視為一般目錄
-s 軟鏈接(符號(hào)鏈接)
-v 顯示詳細(xì)的處理過程

選擇參數(shù):
-S “-S字尾備份字符串> ”或 “--suffix=字尾備份字符串>”
-V “-V備份方式>”或“--version-control=備份方式>”
--help 顯示幫助信息
--version 顯示版本信息

4.使用實(shí)例:
實(shí)例1:給文件創(chuàng)建軟鏈接
命令:ln -s log2013.log link2013
輸出:

復(fù)制代碼
代碼如下:

[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文件創(chuàng)建軟鏈接link2013,如果log2013.log丟失,link2013將失效

實(shí)例2:給文件創(chuàng)建硬鏈接
命令:ln log2013.log ln2013
輸出:

復(fù)制代碼
代碼如下:

[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創(chuàng)建硬鏈接ln2013,log2013.log與ln2013的各項(xiàng)屬性相同

實(shí)例3:接上面兩實(shí)例,鏈接完畢后,刪除和重建鏈接原文件
命令:ll
輸出:

復(fù)制代碼
代碼如下:

[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系統(tǒng)下不斷的閃爍,提示源文件已經(jīng)不存在
2.重建源文件后,軟鏈接不在閃爍提示,說明已經(jīng)鏈接成功,找到了鏈接文件系統(tǒng);重建后,硬鏈接文件并沒有受到源文件影響,硬鏈接文件的內(nèi)容還是保留了刪除前源文件的內(nèi)容,說明硬鏈接已經(jīng)失效

實(shí)例4:將文件鏈接為另一個(gè)目錄中的相同名字
命令:ln log2013.log test3
輸出:

復(fù)制代碼
代碼如下:

[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目錄中創(chuàng)建了log2013.log的硬鏈接,修改test3目錄中的log2013.log文件,同時(shí)也會(huì)同步到源文件

實(shí)例5:給目錄創(chuàng)建軟鏈接
命令:ln -sv /opt/soft/test/test3 /opt/soft/test/test5
輸出:

復(fù)制代碼
代碼如下:

[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: 符號(hào)連接的層數(shù)過多
[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
創(chuàng)建指向“/opt/soft/test/test3”的符號(hào)鏈接“/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
總計(jì) 4
-rw-r--r-- 2 root root 80 12-07 16:36 log2013.log
[root@localhost test3]# touch log2014.log
[root@localhost test3]# ll
總計(jì) 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.目錄只能創(chuàng)建軟鏈接
2.目錄創(chuàng)建鏈接必須用絕對(duì)路徑,相對(duì)路徑創(chuàng)建會(huì)不成功,會(huì)提示:符號(hào)連接的層數(shù)過多 這樣的錯(cuò)誤
3.在鏈接目標(biāo)目錄中修改文件都會(huì)在源文件目錄中同步變化

標(biāo)簽:宿州 綏化 保山 亳州 河源 常德 濱州 江蘇

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《linux命令大全之ln命令詳解(創(chuàng)建軟鏈接和硬鏈接)》,本文關(guān)鍵詞  linux,命令,大全,之,詳解,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《linux命令大全之ln命令詳解(創(chuàng)建軟鏈接和硬鏈接)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于linux命令大全之ln命令詳解(創(chuàng)建軟鏈接和硬鏈接)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    英山县| 武鸣县| 永兴县| 玛曲县| 敦化市| 吉安市| 慈溪市| 深水埗区| 新绛县| 辽宁省| 清镇市| 子长县| 庆云县| 维西| 翁源县| 新昌县| 汉寿县| 陵水| 湖北省| 达日县| 镇沅| 临城县| 博客| 西乌珠穆沁旗| 吉水县| 会同县| 嘉鱼县| 潜山县| 杭锦后旗| 平潭县| 丰镇市| 德令哈市| 洪雅县| 三明市| 白河县| 惠水县| 图片| 卫辉市| 瑞丽市| 福清市| 南城县|