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

2021-02-07    分類: 網(wǎng)站建設(shè)

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

使用什么命令可以查看inode號(hào)?

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

什么是硬鏈接?

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

命令:

  1. link oldfile newfile  

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

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

例如:

  1. # ls -li  
  2. total 0  
  3.   
  4. // 只能對(duì)已存在的文件創(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 號(hào)以及 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. // 不能對(duì)目錄進(jìn)行創(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點(diǎn)特性。

什么是軟鏈接?

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

  1. 軟連接有自己的文件屬性
  2. 可以對(duì)不存在的文件創(chuàng)建
  3. 軟鏈接可以跨文件系統(tǒng)
  4. 軟鏈接可以對(duì)目錄創(chuàng)建
  5. 軟鏈接創(chuàng)建不會(huì)造成鏈接計(jì)數(shù)器增加,因?yàn)榫筒皇峭粋€(gè)inode
  6. 若鏈接的文件被刪除了,該鏈接就是沒有意義了,但是也可以重新創(chuàng)建。

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

例如:

  1. # ls -li  
  2.  total 0  
  3.   
  4.  // 可對(duì)不存在的文件創(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.  // 由于被指向的文件不存在,此時(shí)的軟鏈接 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 恢復(fù)成正常的軟鏈接 
  15.  # echo "This is an original file_A" >> test.file  
  16.  # cat test_soft.link  
  17.  This is an original file_A  
  18.   
  19.  // 對(duì)不存在的目錄創(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/ 

網(wǎng)頁名稱:怎樣理解Linux的軟連接和硬鏈接?
網(wǎng)頁網(wǎng)址:http://www.muchs.cn/news23/99623.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、營銷型網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、定制網(wǎng)站標(biāo)簽優(yōu)化、面包屑導(dǎo)航

廣告

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

外貿(mào)網(wǎng)站制作