怎樣理解Linux的軟連接和硬鏈接?

2021-02-07    分類: 網站建設

我們現(xiàn)代的操作系統(tǒng)需要防止程序崩潰導致信息丟失,需要將信息存儲在文件之中。而且文件能夠被多個進程同時讀取。在Linux中所以的資源,外設都抽象成了文件,所以就有了Linux中“一切皆文件”的特性。當然有文件,肯定是不夠的,總不能把所有的文件放在一起管理,實在是太亂,不易管理維護。Linux就引入了目錄的概念,在Windows中可以稱之為文件夾。目錄的引入就會讓Linux的根文件系統(tǒng)外觀上變成了一個層次分明的目錄樹。如下圖:

使用什么命令可以查看inode號?

可以使用stat和ls -i 命令查看,如下圖所示:

什么是硬鏈接?

硬鏈接是指通過索引節(jié)點來進行連接。也就是存在多個文件名指向同一個inode。這樣就可以將重要的文件建立硬鏈接,來防止“誤刪”的操作。

命令:

  1. link oldfile newfile  

可以創(chuàng)建硬鏈接。硬鏈接的inode是相同的,但是文件名不同,所以它有一些特性:

  1. 文件有相同的inode和data blocks;
  2. 不能對不存在的文件創(chuàng)建硬鏈接
  3. 不能跨文件系統(tǒng)創(chuàng)建(因為在各自文件系統(tǒng)下inode是唯一的,當跨文件系統(tǒng)就會出現(xiàn)inode重復的情況發(fā)生)
  4. 不能對目錄創(chuàng)建,只能對文件進行創(chuàng)建
  5. 如果刪除了一個硬鏈接文件,并不會影響其他的同inode文件(inode中存在鏈接計數(shù)器,刪除一個硬鏈接相當于計數(shù)器減一,反之加一。直到為0,刪除inode)

例如:

  1. # ls -li  
  2. total 0  
  3.   
  4. // 只能對已存在的文件創(chuàng)建硬連接 
  5. # link test.file test_hard.link  
  6. link: cannot create link `test_hard.link' to `test.file': No such file or directory  
  7.   
  8. # echo "This is an original file" > test.file  
  9. # cat test.file  
  10. This is an original file  
  11. # stat test.file  
  12.  File: `test.file' 
  13.  Size: 25           Blocks: 8          IO Block: 4096   regular file  
  14. Device: 807h/2055d      Inode: 660650      Links: 2  
  15. Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)  
  16. ...  
  17. // 文件有相同的 inode 號以及 data block  
  18. # link test.file test_hard.link | ls -li  
  19. total 8  
  20. 660650 -rw-r--r-- 2 root root 25 Sep  1 17:44 test_hard.link  
  21. 660650 -rw-r--r-- 2 root root 25 Sep  1 17:44 test.file  
  22.   
  23. // 不能交叉文件系統(tǒng) 
  24. # ln /dev/input/event5 /root/bfile.txt  
  25. ln: failed to create test_hard link `/root/bfile.txt' => `/dev/input/event5':  
  26. Invalid cross-device link  
  27.   
  28. // 不能對目錄進行創(chuàng)建硬連接 
  29. # mkdir -p test.dir/test  
  30. # ln test.dir/ test_hardlink.dir  
  31. ln: `test.dir/': test_hard link not allowed for directory  
  32. # ls -iF  
  33. 660650 test_hard.link  657948 test.dir/  660650 test.file 

具體的解釋可以參考硬鏈接的5點特性。

什么是軟鏈接?

軟連接就和硬鏈接完全不同,軟連接是用戶數(shù)據(data blocks)中記錄的是另一個文件的路徑名的指向??梢岳斫鉃檐涍B接其實就是一個普通的文件,只是他的內容非常的特殊。所以軟連接有他自己的inode號以及data blocks。那我總結下軟連接的特性:

  1. 軟連接有自己的文件屬性
  2. 可以對不存在的文件創(chuàng)建
  3. 軟鏈接可以跨文件系統(tǒng)
  4. 軟鏈接可以對目錄創(chuàng)建
  5. 軟鏈接創(chuàng)建不會造成鏈接計數(shù)器增加,因為就不是同一個inode
  6. 若鏈接的文件被刪除了,該鏈接就是沒有意義了,但是也可以重新創(chuàng)建。

下圖展示下軟鏈接的訪問過程:

例如:

  1. # ls -li  
  2.  total 0  
  3.   
  4.  // 可對不存在的文件創(chuàng)建軟鏈接 
  5.  # ln -s test.file test_soft.link  
  6.  # ls -liF  
  7.  total 0  
  8.  789467 lrwxrwxrwx 1 root root 8 Sep  1 18:00 test_soft.link -> test.file  
  9.   
  10.  // 由于被指向的文件不存在,此時的軟鏈接 test_soft.link 就是死鏈接 
  11.  # cat test_soft.link  
  12.  cat: test_soft.link: No such file or directory  
  13.   
  14.  // 創(chuàng)建被指向的文件 test.file,test_soft.link 恢復成正常的軟鏈接 
  15.  # echo "This is an original file_A" >> test.file  
  16.  # cat test_soft.link  
  17.  This is an original file_A  
  18.   
  19.  // 對不存在的目錄創(chuàng)建軟鏈接 
  20.  # ln -s test.dir test_soft.link.dir  
  21.  # mkdir -p test.dir/test  
  22.  # tree . -F --inodes  
  23.  .  
  24. ├── [ 789497]  test.dir/  
  25. │   └── [ 789498]  test/  
  26. ├── [ 789495]  test.file  
  27. ├── [ 789495]  test_soft.link -> test.file  
  28. └── [ 789497]  test_soft.link.dir -> test.dir/ 

本文標題:怎樣理解Linux的軟連接和硬鏈接?
標題網址:http://www.bm7419.com/news/99623.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供標簽優(yōu)化、電子商務、App設計、網站設計、網站內鏈、外貿網站建設

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都網頁設計公司