Linux硬鏈接、軟鏈接的介紹及兩者的區別

一、硬鏈接

鏈接數就是名字的數量:

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

(0)
cyh5217cyh5217
上一篇 2016-08-04 21:40
下一篇 2016-08-04 21:40

相關推薦

  • HA專題: 使用pacemaker+corosync實現MySQL高可用

    HA專題: 使用pacemaker+corosync實現MySQL高可用 前言 實驗拓撲 實驗環境 實驗步驟 準備工作 安裝HA組件并配置 配置NFS 配置MySQL 配置HA資源 測試 總結 前言 上篇文章我們介紹了使用pacemkaer+corosync實現簡單的nginx高可用, 這篇文章我們介紹如何使用pacemaker+corosync實現MySQ…

    Linux干貨 2016-04-11
  • N22-第二周博客作業

    1、Linux系統上常見的文件管理類命令有哪些,其常用的使用方法及其示例演示。 常見的文件類管理命令:cp,rm,mv cp復制文件和目錄: -f, –force 強制執行 -i 顯示交互信息,默認cp 帶-i選項 -r,-R 遞歸復制目錄 -s 創建一個符號鏈接而不復制文件 -d 復制符號鏈接本身 cp aa.link qqq 相當于創建qqq…

    Linux干貨 2016-08-22
  • 高級文件系統的管理

    一、遷移分區 分區 /dev/sda6 注意同步問題  創建分區,把原先家目錄下的文件拷貝到新掛載的文件中 mkfs.ext4 /dev/sda6  mkdir /mnt/home mount /dev/sda6 /mnt/home cp -a /home/*  /mnt/home   init 1 切換單用戶模式,把…

    Linux干貨 2016-11-27
  • 推薦-LVS專題: LVS+Keepalived并使用DNS輪詢實現Director的高可用和負載均衡

    LVS專題: LVS+Keepalived并使用DNS輪詢實現Director的高可用和負載均衡 前言 什么是KeepAlived 實驗介紹 實驗拓撲 實驗環境 實驗步驟 配置KeepAlived(1) 實現Director 的VIP互為主從 測試 配置LVS 配置KeepAlived(2) 測試LVS 配置RS的IP和web服務 配置DNS 最終測試 總結…

    Linux干貨 2016-04-09
  • raid 0磁盤陣列

    raid0 1  先給sdb磁盤和sdc磁盤分區 [root@localhost ~]# fdisk /dev/sdb    Command (m for help): n Command action   e   extended   p   primary part…

    Linux干貨 2017-04-25
  • 壓縮與歸檔

    Linux干貨 2015-05-04
欧美性久久久久