天天看点

软链接和硬链接

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 ..