在Linux系統(tǒng)上創(chuàng)建軟連接和硬連接的方法
在本指南中,我將介紹什么是鏈接以及如何更好地使用它們以及所需的概念。
通過執(zhí)行 man ln 命令,可以看到這是在文件之間建立鏈接,而沒有提及是軟鏈接或硬鏈接。
shashi@linuxtechi ~}$ man ln
類似地,命令 man link 描述為調用鏈接功能創(chuàng)建文件。
Soft Link
軟鏈接顧名思義就是創(chuàng)建到新文件的新鏈接。在這種情況下,新文件的節(jié)點號將指向舊文件。
Hard Link
在這種情況下,舊文件和新文件都將指向相同的索引節(jié)點號。
Symbolic Link
在某些 Unix/Linux 系統(tǒng)中,符號和軟鏈接都被視為相同的。但是實際的差異是,新的文件和舊文件將指向一個新的 Inode 號碼,這將完全取決于實施。
注意: 在許多情況下,符號和軟鏈接術語可以互換使用,但是人們必須知道什么時候用什么。
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) 在兩個文件之間創(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 命令將內容輸入到文件中,然后按 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é)點號
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)建一個名為 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) 當源文件或這些文件中的任何一個被刪除時,它不會影響其他文件。
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) 對一個文件內容的任何更改都將影響并相應地更改其他文件的內容。
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 命令輸入內容,然后按 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) 將目標文件創(chuàng)建為 dst.txt,并使用 ln -s 命令創(chuàng)建符號鏈接 (也稱為軟鏈接)。查看 dst.txt 文件的內容,可以看到與 src.txt 相同的內容。
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) 在符號鏈接的情況下,源文件和目標文件的 inode 號不同。在權限中出現(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) 允許對目錄進行符號創(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) 源和目標的 Inode 編號不同
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)建符號鏈接。一旦創(chuàng)建了這些帶有符號鏈接的目錄,就可以在這些目錄中創(chuàng)建文件。當在源目錄中創(chuàng)建文件時,同樣的情況也會反映在目標目錄中。
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 命令刪除軟鏈接或符號鏈接
# rm <soft-link-filename> # unlink <soft-link-filename>
刪除軟鏈接目錄
# rm <soft-link-directory> # unlink <soft-link-directory>
以上就是在Linux系統(tǒng)上創(chuàng)建軟連接和硬連接的方法的詳細內容,更多關于Linux創(chuàng)建軟連接和硬連接的資料請關注腳本之家其它相關文章!
相關文章
分享9個實戰(zhàn)及面試常用Linux Shell腳本編寫
這篇文章主要介紹了9個實戰(zhàn)及面試常用Shell腳本編寫,非常不錯,具有一定的收藏價值,需要的朋友可以參考下2018-10-10Linux安裝yum時出現(xiàn)apt-get?install?E:?無法定位軟件包問題解決
這篇文章主要介紹了Linux安裝yum時出現(xiàn)apt-get?install?E:?無法定位軟件包問題解決的相關資料,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-12-12linux NFS安裝配置及常見問題、/etc/exports配置文件、showmount命令
這篇文章主要介紹了linux NFS安裝配置及常見問題,介紹的也比較詳細特分享下,方便需要的朋友2014-07-07