在Linux系統(tǒng)上創(chuàng)建軟連接和硬連接的方法
在本指南中,我將介紹什么是鏈接以及如何更好地使用它們以及所需的概念。
通過執(zhí)行 man ln 命令,可以看到這是在文件之間建立鏈接,而沒有提及是軟鏈接或硬鏈接。
shashi@linuxtechi ~}$ man ln
類似地,命令 man link 描述為調(diào)用鏈接功能創(chuàng)建文件。
Soft Link
軟鏈接顧名思義就是創(chuàng)建到新文件的新鏈接。在這種情況下,新文件的節(jié)點(diǎn)號(hào)將指向舊文件。
Hard Link
在這種情況下,舊文件和新文件都將指向相同的索引節(jié)點(diǎn)號(hào)。
Symbolic Link
在某些 Unix/Linux 系統(tǒng)中,符號(hào)和軟鏈接都被視為相同的。但是實(shí)際的差異是,新的文件和舊文件將指向一個(gè)新的 Inode 號(hào)碼,這將完全取決于實(shí)施。
注意: 在許多情況下,符號(hào)和軟鏈接術(shù)語可以互換使用,但是人們必須知道什么時(shí)候用什么。
Creating Hard Link
(1) man ln 命令輸出如下
shashi@linuxtechi ~}$ man ln ln - make links between files
(2) ln 命令無參數(shù),輸出如下
shashi@linuxtechi ~}$ln ln: missing file operand Try 'ln --help' for more information.
(3) 在兩個(gè)文件之間創(chuàng)建硬鏈接,首先檢查文件是否存在,否則將創(chuàng)建文件,然后鏈接它們。
shashi@linuxtechi ~}$ ls -la total 24 drwx------ 4 root root 4096 Feb 6 15:23 . drwxr-xr-x 23 root root 4096 Jan 25 16:39 .. -rw-r--r-- 1 root root 3122 Jan 25 16:41 .bashrc drwx------ 2 root root 4096 Feb 6 15:23 .cache -rw-r--r-- 1 root root 0 Jan 25 16:41 .hushlogin -rw-r--r-- 1 root root 148 Aug 17 2015 .profile drwxr-xr-x 2 root root 4096 Jan 25 16:41 .ssh
(4) 使用 touch 命令創(chuàng)建文件
shashi@linuxtechi ~}$ touch 123.txt shashi@linuxtechi ~}$ ls -l total 0 -rw-r--r-- 1 root root 0 Feb 6 15:51 123.txt
(5) 使用 cat 命令將內(nèi)容輸入到文件中,然后按 ctrl+c 保存并退出。
shashi@linuxtechi ~}$ cat > 123.txt Welcome to this World! ^C
(6) 創(chuàng)建 123.txt 和 321.txt 文件之間的硬鏈接
shashi@linuxtechi ~}$ ln 123.txt 321.txt shashi@linuxtechi ~}$ ls -l total 8 -rw-r--r-- 2 root root 23 Feb 6 15:52 123.txt -rw-r--r-- 2 root root 23 Feb 6 15:52 321.txt
(7) 檢查文件的索引節(jié)點(diǎn)號(hào)
shashi@linuxtechi ~}$ ls -li total 8 794583 -rw-r--r-- 2 root root 23 Feb 6 15:52 123.txt 794583 -rw-r--r-- 2 root root 23 Feb 6 15:52 321.txt $ cat 321.txt Welcome to this World!
(8) 再創(chuàng)建一個(gè)名為 456.txt 的文件,并使用 ln 命令將其鏈接到 321.txt
shashi@linuxtechi ~}$ ls -li total 12 794583 -rw-r--r-- 3 root root 23 Feb 6 15:52 123.txt 794583 -rw-r--r-- 3 root root 23 Feb 6 15:52 321.txt 794583 -rw-r--r-- 3 root root 23 Feb 6 15:52 456.txt $ cat 456.txt Welcome to this World! shashi@linuxtechi ~}$ ls -l total 12 -rw-r--r-- 3 root root 23 Feb 6 15:52 123.txt -rw-r--r-- 3 root root 23 Feb 6 15:52 321.txt -rw-r--r-- 3 root root 23 Feb 6 15:52 456.txt
(9) 當(dāng)源文件或這些文件中的任何一個(gè)被刪除時(shí),它不會(huì)影響其他文件。
shashi@linuxtechi ~}$ rm 123.txt shashi@linuxtechi ~}$ ls -l total 8 -rw-r--r-- 2 root root 23 Feb 6 15:52 321.txt -rw-r--r-- 2 root root 23 Feb 6 15:52 456.txt shashi@linuxtechi ~}$ ls -li total 8 794583 -rw-r--r-- 2 root root 23 Feb 6 15:52 321.txt 794583 -rw-r--r-- 2 root root 23 Feb 6 15:52 456.txt shashi@linuxtechi ~}$ cat 456.txt Welcome to this World!
(10) 不允許跨目錄創(chuàng)建硬鏈接。
shashi@linuxtechi ~}$ls -l total 8 -rw-r--r-- 2 root root 23 Feb 6 15:52 321.txt -rw-r--r-- 2 root root 23 Feb 6 15:52 456.txt shashi@linuxtechi ~}$ mkdir abc shashi@linuxtechi ~}$ ln abc def ln: abc: hard link not allowed for directory
(11) 對(duì)一個(gè)文件內(nèi)容的任何更改都將影響并相應(yīng)地更改其他文件的內(nèi)容。
shashi@linuxtechi ~}$ vi 321.txt Welcome to this World! You are welcomed to this new world :wq shashi@linuxtechi ~}$ ls -l total 12 -rw-r--r-- 2 root root 59 Feb 6 16:24 321.txt -rw-r--r-- 2 root root 59 Feb 6 16:24 456.txt drwxr-xr-x 2 root root 4096 Feb 6 16:18 abc shashi@linuxtechi ~}$ cat 456.txt Welcome to this World! You are welcomed to this new world
Creating Soft Link:
(1) 使用 touch 命令創(chuàng)建文件 src.txt,使用 cat 命令輸入內(nèi)容,然后按 ctrl+c 保存并退出。
shashi@linuxtechi ~}$ touch src.txt shashi@linuxtechi ~}$ cat > src.txt Hello World ^C shashi@linuxtechi ~}$ ls -l total 4 -rw-r--r-- 1 root root 12 Feb 6 16:32 src.txt
(2) 將目標(biāo)文件創(chuàng)建為 dst.txt,并使用 ln -s 命令創(chuàng)建符號(hào)鏈接 (也稱為軟鏈接)。查看 dst.txt 文件的內(nèi)容,可以看到與 src.txt 相同的內(nèi)容。
shashi@linuxtechi ~}$ ln -s src.txt dst.txt shashi@linuxtechi ~}$ ls -l total 4 lrwxrwxrwx 1 root root 7 Feb 6 16:33 dst.txt -> src.txt -rw-r--r-- 1 root root 12 Feb 6 16:32 src.txt shashi@linuxtechi ~}$ cat dst.txt Hello World
(3) 在符號(hào)鏈接的情況下,源文件和目標(biāo)文件的 inode 號(hào)不同。在權(quán)限中出現(xiàn)字母 l,表明這些是鏈接。dst.txt ->src.txt 將是現(xiàn)在建立的新鏈接。
shashi@linuxtechi ~}$ ls -li total 4 794584 lrwxrwxrwx 1 root root 7 Feb 6 16:33 dst.txt -> src.txt 794583 -rw-r--r-- 1 root root 12 Feb 6 16:32 src.txt
(4) 允許對(duì)目錄進(jìn)行符號(hào)創(chuàng)建
shashi@linuxtechi ~}$ mkdir abc shashi@linuxtechi ~}$ ln -s abc def $ ls -l total 8 drwxr-xr-x 2 root root 4096 Feb 6 16:34 abc lrwxrwxrwx 1 root root 3 Feb 6 16:34 def -> abc lrwxrwxrwx 1 root root 7 Feb 6 16:33 dst.txt -> src.txt -rw-r--r-- 1 root root 12 Feb 6 16:32 src.txt
(5) 源和目標(biāo)的 Inode 編號(hào)不同
shashi@linuxtechi ~}$ ls -li total 8 794585 drwxr-xr-x 2 root root 4096 Feb 6 16:34 abc 794586 lrwxrwxrwx 1 root root 3 Feb 6 16:34 def -> abc 794584 lrwxrwxrwx 1 root root 7 Feb 6 16:33 dst.txt -> src.txt 794583 -rw-r--r-- 1 root root 12 Feb 6 16:32 src.txt
(6) 如前所述,可以為目錄創(chuàng)建符號(hào)鏈接。一旦創(chuàng)建了這些帶有符號(hào)鏈接的目錄,就可以在這些目錄中創(chuàng)建文件。當(dāng)在源目錄中創(chuàng)建文件時(shí),同樣的情況也會(huì)反映在目標(biāo)目錄中。
shashi@linuxtechi ~}$ $ cd abc shashi@linuxtechi ~}$ touch 123.txt shashi@linuxtechi ~}$ vi 123.txt Hello :wq! shashi@linuxtechi ~}$ touch 456.txt shashi@linuxtechi ~}$ cd .. shashi@linuxtechi ~}$ ls -l total 8 drwxr-xr-x 2 root root 4096 Feb 6 16:36 abc lrwxrwxrwx 1 root root 3 Feb 6 16:34 def -> abc lrwxrwxrwx 1 root root 7 Feb 6 16:33 dst.txt -> src.txt -rw-r--r-- 1 root root 12 Feb 6 16:32 src.txt shashi@linuxtechi ~}$ cd def shashi@linuxtechi ~}$ ls -l total 4 -rw-r--r-- 1 root root 6 Feb 6 16:37 123.txt -rw-r--r-- 1 root root 0 Feb 6 16:36 456.txt shashi@linuxtechi ~}$ cat 123.txt Hello
Removing Soft Link / Symbolic Links
使用 rm 和 unlink 命令刪除軟鏈接或符號(hào)鏈接
# rm <soft-link-filename> # unlink <soft-link-filename>
刪除軟鏈接目錄
# rm <soft-link-directory> # unlink <soft-link-directory>
以上就是在Linux系統(tǒng)上創(chuàng)建軟連接和硬連接的方法的詳細(xì)內(nèi)容,更多關(guān)于Linux創(chuàng)建軟連接和硬連接的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
分享9個(gè)實(shí)戰(zhàn)及面試常用Linux Shell腳本編寫
這篇文章主要介紹了9個(gè)實(shí)戰(zhàn)及面試常用Shell腳本編寫,非常不錯(cuò),具有一定的收藏價(jià)值,需要的朋友可以參考下2018-10-10IO復(fù)用之select poll epoll的總結(jié)(推薦)
下面小編就為大家?guī)硪黄狪O復(fù)用之select poll epoll的總結(jié)(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01Linux安裝yum時(shí)出現(xiàn)apt-get?install?E:?無法定位軟件包問題解決
這篇文章主要介紹了Linux安裝yum時(shí)出現(xiàn)apt-get?install?E:?無法定位軟件包問題解決的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-12-12linux NFS安裝配置及常見問題、/etc/exports配置文件、showmount命令
這篇文章主要介紹了linux NFS安裝配置及常見問題,介紹的也比較詳細(xì)特分享下,方便需要的朋友2014-07-07Linux中的進(jìn)程狀態(tài)和優(yōu)先級(jí)
這篇文章主要介紹了Linux中的進(jìn)程狀態(tài)和優(yōu)先級(jí)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09