2.22 硬鏈接文件

硬連接定義

  • 硬連接:當系統要讀取一個文件時,會先讀inode信息,而後再根據inode中的信息到塊區域將數據取出來。而硬連接是直接再創建一個inode連接到文件放置的塊區域。即創建硬連接時該文件內容沒有變化,只是增長了一個指向這個文件的inode,並不會額外佔用磁盤空間。node

  • 硬連接不支持對目錄作連接,只支持對文件作連接windows

硬連接和軟鏈接對比

  • 硬連接
[root@localhost ~]# ln 1.txt 1_heard.txt
  • 軟連接
[root@localhost ~]# ln -s 1.txt 1_sorft.txt
  • 對比:會發現軟連接很小,硬連接很大
    • ls -i 查看inode號
    • 會發現硬連接文件和源文件使用了同一個inode號,大小相同,可以使用find命令查找到硬連接
[root@localhost ~]# ls -l
總用量 32
drwxr-xr-x. 3 root root   45 10月 26 16:11 111
-rw-r--r--. 1 root root    0 10月 26 15:54 111.12.txt
-rw-r--r--. 2 root root  391 10月 30 15:10 1_heard.txt
lrwxrwxrwx. 1 root root    5 10月 30 15:06 1_sorft.txt -> 1.txt
[root@localhost ~]# ls -i
33584735 111          33575033 1_sorft.txt  33575034 2.txt   33574987 anaconda-ks.cfg
33584737 111.12.txt   33575035 1.txt        33584734 3.txt   33575006 anaconda-ks.cfg.1
33575035 1_heard.txt  33584733 1.txt~       33584736 3.txt~  33575036 windows.txt

硬連接文件特性

  • 就是建立的文件和另一個文件是相同的inode號,這兩個文件相互爲硬連接文件。(軟連接文件是有原有的目標,它有一個真正的文件,軟連接僅僅爲一個快捷方式)
  • 再刪除源文件後,會發現==軟連接==一直閃爍,表示找不到源文件了,那這個軟連接文件就沒有用了
  • 再刪除源文件後,會發現==硬連接==,會發現正常使用,但inode會變成1
[root@localhost ~]# rm 1.txt
rm:是否刪除普通文件 "1.txt"?y
[root@localhost ~]# ls -l
總用量 28
drwxr-xr-x. 3 root root   45 10月 26 16:11 111
-rw-r--r--. 1 root root    0 10月 26 15:54 111.12.txt
-rw-r--r--. 1 root root  391 10月 30 15:10 1_heard.txt
lrwxrwxrwx. 1 root root    5 10月 30 15:06 1_sorft.txt -> 1.txt
  • 真正存數據的地方,存這個文件信息的位置在inode上
    • inode號表明着一個inode,inode是存在於文件系統中特殊的東西,這個inode會去記錄一些文件的屬性,如文件的時間、權限、位置等
[root@localhost ~]# ls -i 1_heard.txt
33575035 1_heard.txt
  • 硬連接不會佔用雙份空間,由於使用的同一個inode

硬連接的限制

  • 硬連接的限制:
  1. 目錄不能作硬連接
    [root@localhost ~]# ln 111 hf11     在硬連接指向目錄的時候,會提示錯誤
    ln: "111": 不容許將硬連接指向目錄
  2. 不能跨文件系統(跨分區),由於不一樣的文件系統有不一樣的inode,不一樣的table目錄體系。
[root@localhost ~]# df -h
文件系統        容量  已用  可用 已用% 掛載點
/dev/sda3        18G  1.1G   17G    6% /
devtmpfs        483M     0  483M    0% /dev
tmpfs           493M     0  493M    0% /dev/shm
tmpfs           493M  6.7M  486M    2% /run
tmpfs           493M     0  493M    0% /sys/fs/cgroup
/dev/sda1       197M  109M   88M   56% /boot
tmpfs            99M     0   99M    0% /run/user/0
[root@localhost ~]# ls -i /boot/
    71 config-3.10.0-514.el7.x86_64
    67 grub
131136 grub2
    76 initramfs-0-rescue-513f8b3950084e768a99df3a6cd3d9e6.img
    75 initramfs-3.10.0-514.el7.x86_64.img
189445 initramfs-3.10.0-514.el7.x86_64kdump.img
    74 initrd-plymouth.img
    72 symvers-3.10.0-514.el7.x86_64.gz
    70 System.map-3.10.0-514.el7.x86_64
    77 vmlinuz-0-rescue-513f8b3950084e768a99df3a6cd3d9e6
    73 vmlinuz-3.10.0-514.el7.x86_64
[root@localhost ~]# ln /boot/config-3.10.0-514.el7.x86_64 /tmp/conifg.1
ln: 沒法建立硬連接"/tmp/conifg.1" => "/boot/config-3.10.0-514.el7.x86_64": 無效的跨設備鏈接

硬連接總結

  • 硬連接文件不能跨分區建立,由於每一個分區都有各自的inode。
    • 好比、:d1分區有a文件的inode號是88,而d2分區的b文件的inode號也是88.
    • 正是由於各個分區的inode號各自爲營,若是跨分區建立硬連接文件就會致使混亂。
    • 爲了不這個問題,因此不容許跨分區建立硬連接文件。
  • 硬連接能夠刪除,由於使用了相同的inode
相關文章
相關標籤/搜索