軟鏈接和硬鏈接

ln命令可以創建硬鏈接:
語法格式:
ln 源文件 目標文件
文件名1-》inode1-》blockA
文件名2-》inode1-》blockA
[root@localhost ~]# ln a.txt b.txt

[root@localhost ~]# ll -i a.txt b.txt
36433003 -rw-r--r-- 2 root root 10 Feb 24 09:05 a.txt
36433003 -rw-r--r-- 2 root root 10 Feb 24 09:05 b.txt

[root@localhost ~]# rm -rf a.txt
[root@localhost ~]# cat b.txt
aaaa
bbbb
#源文件被刪除,不影響鏈接文件的正常使用

[root@localhost ~]# mkdir test
[root@localhost ~]# ln test/ 123
ln: ‘test/’: hard link not allowed for directory
#硬鏈接不能針對目錄創建

[root@localhost ~]# ln /boot/grub2/grub.cfg grub.cfg
ln: failed to create hard link ‘grub.cfg’ => ‘/boot/grub2/grub.cfg’: Invalid cross-device link
#硬鏈接不允許跨分區創建
總結: 硬鏈接特點,創建時,不能跨分區,不能給文件夾。

軟鏈接:相當於windows中的快捷方式
[root@localhost ~]# ln -s b.txt c.txt
[root@localhost ~]# rm -rf b.txt

[root@localhost ~]# cat c.txt
cat: c.txt: No such file or directory

#源文件被刪除,鏈接文件失效
[root@localhost ~]# ln -s test/ 123
#能針對目錄創建
[root@localhost ~]# ln -s /boot/grub2/grub.cfg grub.cfg
#能跨分區創建

例:查看目錄的鏈接數
[root@localhost ~]# ll -d test/
drwxr-xr-x 2 root root 6 Feb 24 09:10 test/

[root@localhost ~]# ll -di test/
50599109 drwxr-xr-x 2 root root 6 Feb 24 09:10 test/
[root@localhost test]# ll -di .
50599109 drwxr-xr-x 2 root root 6 Feb 24 09:10 .
[root@localhost xuegod]# ll -di ..
50599109 drwxr-xr-x 3 root root 19 Feb 24 09:23 ..

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章