一、硬鏈接
鏈接數就是名字的數量:
ln(link) 硬鏈接,多個相同的inode文件(同分區),多個硬鏈接文件的存在在硬盤上只占用一個文件的容量
創建的硬鏈接是平等的,inode相同,刪除某一個互不影響,其他文件仍然可以使用
硬鏈接的本質:給一個文件創建多個名字
創建多個硬鏈接,觀察鏈接數的變化(+1),鏈接數是幾就是有幾個文件名
同一分區中,同一個inode號必定是一個文件
原始的文件
[root@CentOS7 testdir]# ll -i man.txt 15 -rw-r--r--. 1 root root 15978 Aug 8 2008 man.txt
創建第一個硬鏈接
[root@CentOS7 testdir]# ln man.txt test/f11 [root@CentOS7 testdir]# ll -i man.txt test/f11 15 -rw-r--r--. 2 root root 15978 Aug 8 2008 man.txt 15 -rw-r--r--. 2 root root 15978 Aug 8 2008 test/f11
創建第二個硬鏈接
[root@CentOS7 testdir]# ln man.txt testdir/f22 [root@CentOS7 testdir]# ll -i man.txt testdir/f22 test/f11 15 -rw-r--r--. 3 root root 15978 Aug 8 2008 man.txt 15 -rw-r--r--. 3 root root 15978 Aug 8 2008 testdir/f22 15 -rw-r--r--. 3 root root 15978 Aug 8 2008 test/f11
當對一個文件創建多個硬鏈接時,所有文件的inode相同,權限、大小、時間等屬性相同。
[root@CentOS7 testdir]# echo "aaaaaaaaaaaaaaaaaaa" >man.txt [root@CentOS7 testdir]# ll -i testdir/f22 test/f11 man.txt test/f 15 -rw-r--r--. 4 root root 8 Jul 29 09:49 man.txt 15 -rw-r--r--. 4 root root 8 Jul 29 09:49 testdir/f22 15 -rw-r--r--. 4 root root 8 Jul 29 09:49 test/f 15 -rw-r--r--. 4 root root 8 Jul 29 09:49 test/f11
當向一個文件寫入數據,其他文件的屬性內容等也會發生變化
硬鏈接不能跨分區,跨設備創建
[root@CentOS7 testdir]# ln man.txt /roo/a ln: creating hard link `/roo/a' => `man.txt': No such file or directory
硬鏈接不能針對目錄
[root@CentOS7 testdir]# ln Help/ H ln: `Help/': hard link not allowed for directory
當刪除原始文件后,鏈接文件仍然可以查看
[root@CentOS7 testdir]# ll -i man.txt man 131 -rw-r--r--. 6 root root 3256 Aug 1 16:54 man 131 -rw-r--r--. 6 root root 3256 Aug 1 16:54 man.txt [root@CentOS7 testdir]# rm -f man.txt [root@CentOS7 testdir]# tail man -Z, --ditroff use groff and force it to produce ditroff -?, --help give this help list --usage give a short usage message -V, --version print program version Mandatory or optional arguments to long options are also mandatory or optional for any corresponding short options. Report bugs to cjwatson@debian.org.
二、軟(soft)鏈接:
軟鏈接相當于Windows的快捷方式
[root@CentOS7 testdir]# ln -s man.txt man [root@CentOS7 testdir]# ls -li man.txt man 12 lrwxrwxrwx. 1 root root 7 Jul 29 10:04 man -> man.txt 15 -rw-r--r--. 4 root root 8 Jul 29 09:49 man.txt
創建軟鏈接文件,鏈接文件會以綠色字體標識并指向原文件,通過觀察可以發現兩個文件的inode編號不同
對原始文件創建軟鏈接文件
[root@CentOS7 testdir]# ln –s man.txt /roo/man.txt [root@CentOS7 testdir]# ll /root/man.txt lrwxrwxrwx. 1 root root 7 Aug 1 16:55 /root/man.txt -> man.txt [root@CentOS7 testdir]# ln –s ../../testdir/man.txt /root/man1 [root@CentOS7 testdir]# ll /root/man1 lrwxrwxrwx. 1 root root 9 Aug 1 16:56 /root/man1 -> ../../testdir/man.txt [root@CentOS7 testdir]# ln -s /testdir/ /root/test1 [root@CentOS7 testdir]# ll -d /testdir/ /root/test1 lrwxrwxrwx. 1 root root 9 Aug 1 17:02 /root/test1 -> /testdir/ drwxr-xr-x. 3 root root 34 Aug 1 16:53 /testdir/
通過上面的實例可以發現軟鏈接可以針對目錄,跨分區創建,并且創建的時候要注意路徑的問題,如果路徑錯誤,鏈接文件會保存顯示的
軟鏈接時需要注意絕對路徑和相對路徑,相對于軟鏈接的路徑而不是當前目錄的路徑(指向相對于當前工作目錄或某目錄的位置)
當刪除原始文件后,創建的軟鏈接文件將不能訪問
[root@CentOS7 testdir]# ln -s /testdir/man /root/111111 [root@CentOS7 testdir]# ll -i /testdir/man /root/111111 105103873 lrwxrwxrwx. 1 root root 12 Aug 1 17:16 /root/111111 -> /testdir/man 131 -rw-r--r--. 5 root root 3256 Aug 1 16:54 /testdir/man [root@CentOS7 testdir]# rm -f /testdir/man [root@CentOS7 testdir]# ll /testdir/man /root/111111 ls: cannot access /testdir/man: No such file or directory lrwxrwxrwx. 1 root root 12 Aug 1 17:16 /root/111111 -> /testdir/man [root@CentOS7 testdir]# cat /root/111111 cat: /root/111111: No such file or directory
三、軟硬鏈接的區別:
軟鏈接和硬鏈接的區別主要在于:刪除原始文件后,軟鏈接將失效,但硬鏈接仍然可用;軟鏈接適用于文件或目錄,但硬鏈接只可用于文件,不能為目錄建立硬鏈接;軟鏈接與原始文件可以位于不同的文件系統中,但硬鏈接必須與原始文件在同一文件系統(如一個Linux分區)內。
原創文章,作者:cyh5217,如若轉載,請注明出處:http://www.www58058.com/29045