什么是硬鏈接? 什么是軟連接? 有時候我們經常把硬鏈接和軟連接搞混了
1. 硬鏈接
硬鏈接雖然看起來和cp 差不多但是還是不一樣的,cp 的文件inode 號不是一樣的,且修改文件另一個不會一起變化的
- 源文件和硬鏈接文件擁有相同的Inode和Block
- 修改任意一個文件,另一個都改變
- 刪除任意一個文件,另一個都能使用
- 硬鏈接不能鏈接目錄
- 硬鏈接不能跨分區
[root@localhost ~]# vim test
查看新建文件內容
[root@localhost ~]# cat test
hello 123 234
生產硬鏈接,將test 文件硬鏈接到test1
[root@localhost ~]# ln test test1
檢查文件的inode 號是一樣的
[root@localhost ~]# ls -li
total 12
33574979 -rw-------. 1 root root 1418 Feb 8 06:43 anaconda-ks.cfg
34190349 -rw-r--r-- 2 root root 15 Feb 24 01:22 test
34190349 -rw-r--r-- 2 root root 15 Feb 24 01:22 test1
我們檢查test1 文件的內容是和我們源文件內容一樣
[root@localhost ~]# cat test1
hello 123 234
修改硬鏈接文件
[root@localhost ~]# vim test1
查看修改的內容
[root@localhost ~]# cat test1
hello 123 234 456
檢查源文件,發現源文件會隨的硬鏈接文件的改變而改變
[root@localhost ~]# cat test
hello 123 234 456
[root@localhost ~]#
刪除硬鏈接 不會影響到源文件
[root@localhost ~]# rm -rf test1
[root@localhost ~]# ls -li
total 8
33574979 -rw-------. 1 root root 1418 Feb 8 06:43 anaconda-ks.cfg
34190349 -rw-r--r-- 1 root root 19 Feb 24 01:23 test
給源文件生產一個硬鏈接
[root@localhost ~]# cat test
hello 123 234 456
[root@localhost ~]# ln test test1
[root@localhost ~]# ls -li
total 12
33574979 -rw-------. 1 root root 1418 Feb 8 06:43 anaconda-ks.cfg
34190349 -rw-r--r-- 2 root root 19 Feb 24 01:23 test
34190349 -rw-r--r-- 2 root root 19 Feb 24 01:23 test1
刪除源文件,硬鏈接文件沒有影響
[root@localhost ~]# rm -rf test
[root@localhost ~]#
[root@localhost ~]# ls -li
total 8
33574979 -rw-------. 1 root root 1418 Feb 8 06:43 anaconda-ks.cfg
34190349 -rw-r--r-- 1 root root 19 Feb 24 01:23 test1
[root@localhost ~]#
[root@localhost ~]# ln test /mnt/test2
ln: failed to create hard link ‘/mnt/test2’ => ‘test’: Invalid cross-device link
[root@localhost ~]#
[root@localhost ~]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 942M 0 942M 0% /dev
tmpfs tmpfs 954M 0 954M 0% /dev/shm
tmpfs tmpfs 954M 11M 944M 2% /run
tmpfs tmpfs 954M 0 954M 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 19G 4.1G 15G 23% /
/dev/sdb1 ext4 22G 47M 20G 1% /mnt
/dev/sda1 xfs 1.1G 182M 882M 18% /boot
tmpfs tmpfs 191M 0 191M 0% /run/user/0
創建一個目錄,嘗試硬鏈接一個目錄,硬鏈接失敗,硬鏈接不允許鏈接目錄
[root@localhost ~]# mkdir test-dir
[root@localhost ~]# mv test test-dir/
[root@localhost ~]# ln test-dir/ test-dir-123
ln: ‘test-dir/’: hard link not allowed for directory
- 軟鏈接和源文件擁有不同的Inode和Block
- 兩個文件修改任意一個,另一個都改變
- 刪除軟鏈接,源文件不受影響;刪除源文件,軟鏈接不能使用
- 軟鏈接沒有實際數據,只保存源文件的Inode,不論源文件多大,軟鏈接大小不變
- 軟鏈接的權限時最大權限,但由於沒有實際數據,最終訪問時需參考源文件權限
- 軟鏈接可以鏈接目錄
- 軟鏈接可以跨分區
創建test文件
[root@localhost test-dir]# ls -li
total 4
33575375 -rw-r--r-- 1 root root 4 Feb 24 01:24 test
生產軟連接
[root@localhost test-dir]# ln -s test test1
檢查inode 號是不一樣的
[root@localhost test-dir]# ls -li
total 4
33575375 -rw-r--r-- 1 root root 4 Feb 24 01:24 test
33575378 lrwxrwxrwx 1 root root 4 Feb 24 01:53 test1 -> test
查看軟鏈接文件的文件內容
[root@localhost test-dir]# cat test1
123
給軟連接文件追加數據
[root@localhost test-dir]# echo 456 >> test1
[root@localhost test-dir]#
[root@localhost test-dir]# cat test1
123
456
檢查源文件 ,源文件隨着軟鏈接文件的修改而變化
[root@localhost test-dir]# cat test
123
456
刪除軟鏈接
[root@localhost test-dir]# rm -rf test1
檢查軟連接的刪除不會影響到源文件
[root@localhost test-dir]# ls -li
total 4
33575375 -rw-r--r-- 1 root root 8 Feb 24 01:54 test
[
[root@localhost test-dir]# cat test
123
456
生產軟連接
[root@localhost test-dir]# ln -s test test1
[root@localhost test-dir]# ls -li
total 4
33575375 -rw-r--r-- 1 root root 8 Feb 24 01:54 test
33575376 lrwxrwxrwx 1 root root 4 Feb 24 01:55 test1 -> test
刪除源文件發現軟連接文件也會隨之源鏈接的刪除而消失
[root@localhost test-dir]# rm -rf test
[root@localhost test-dir]#
[root@localhost test-dir]# ls -li
total 0
33575376 lrwxrwxrwx 1 root root 4 Feb 24 01:55 test1 -> test
[root@localhost test-dir]# cat test1
cat: test1: No such file or directory
dd 創建一個大文件
[root@localhost test-dir]# dd if=/dev/zero of=test-big bs=100M count=2
2+0 records in
2+0 records out
209715200 bytes (210 MB) copied, 5.09727 s, 41.1 MB/s
[root@localhost test-dir]# du -sh *
0 test1
200M test-big
[root@localhost test-dir]#
[root@localhost test-dir]# ls -li
total 204800
33575376 lrwxrwxrwx 1 root root 4 Feb 24 01:55 test1 -> test
33575375 -rw-r--r-- 1 root root 209715200 Feb 24 01:57 test-big
軟鏈接源文件
[root@localhost test-dir]# ln -s test-big test-big-1
查看不論源文件多大,軟鏈接大小不變
[root@localhost test-dir]# ls -li
total 204800
33575376 lrwxrwxrwx 1 root root 4 Feb 24 01:55 test1 -> test
33575375 -rw-r--r-- 1 root root 209715200 Feb 24 01:57 test-big
33575378 lrwxrwxrwx 1 root root 8 Feb 24 01:57 test-big-1 -> test-big
[root@localhost test-dir]# du -sh test-big-1
0 test-big-1
[root@localhost test-dir]#
[root@localhost test-dir]# cd ..
軟連接目錄成功
[root@localhost ~]# ln -s test-dir/ test-dir-1
[root@localhost ~]# ls -li
total 1440260
19250 drwxr-xr-x 2 root root 22 Feb 22 23:41 123
33574979 -rw-------. 1 root root 1418 Feb 8 06:40 anaconda-ks.cfg
34000838 -rw-r--r-- 1 root root 950009856 Feb 23 02:25 CentOS-7-x86_64-Minimal-1804.iso
33575377 drwxr-xr-x 2 root root 53 Feb 24 01:57 test-dir
33575379 lrwxrwxrwx 1 root root 9 Feb 24 01:58 test-dir-1 -> test-dir/
34000836 -rw-r--r-- 1 root root 524298240 Feb 22 23:34 test.tar
34000837 -rw-r--r-- 1 root root 508959 Feb 22 23:39 test.tar.gz
