软链接和硬链接的区别
- 本质
硬链接:本质是同一个文件
软链接:本质不是同一个文件 - 跨设备
硬链接:不支持
软链接:支持 - inode
硬链接:相同
软链接:不同 - 链接数
硬链接:创建新的硬链接,链接数会增加,删除硬链接,链接数减少
软链接:创建或删除,链接数不会变化 - 文件夹
硬链接:不支持
软链接:支持 - 相对路径
硬链接:原始文件相对路径是相对于当前工作目录
软链接:原始文件的相对路径是相对于链接文件的相对路径 - 删除源文件
硬链接:只是链接数减一,但链接文件的访问不受影响
软链接:链接文件将无法访问 - 文件类型
硬链接:和源文件相同
软链接:链接文件,和源文件无关 - 文件大小
硬链接: 和源文件相同
软链接: 源文件的路径的长度
实例1:创建软链接和硬链接并查看元数据信息
File: 'a'
Size: 5 Blocks: 8 IO Block: 4096 regular file
Device: fc00h/64512d Inode: 4456462 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/wx562635) Gid: ( 1000/wx562635)
Access: 2021-02-03 21:11:54.045882976 +0800
Modify: 2021-02-03 21:11:54.045882976 +0800
Change: 2021-02-03 21:11:54.045882976 +0800
创建硬链接文件 a.hardlink并查看属性,除了文件名不同,元数据信息完全一致。
wx562635@U16-template:~$ stat a.hardlink
File: 'a.hardlink'
Size: 5 Blocks: 8 IO Block: 4096 regular file
Device: fc00h/64512d Inode: 4456462 Links: 2
Access: (0664/-rw-rw-r--) Uid: ( 1000/wx562635) Gid: ( 1000/wx562635)
Access: 2021-02-03 21:11:54.045882976 +0800
Modify: 2021-02-03 21:11:54.045882976 +0800
Change: 2021-02-03 21:12:54.902765333 +0800
创建软链接并查看属性,元数据信息和原文件差异很大,文件大小、block、文件类型、inode和三个时间属性都不相同
wx562635@U16-template:~$ stat a.ln
File: 'a.ln' -> 'a'
Size: 1 Blocks: 0 IO Block: 4096 symbolic link
Device: fc00h/64512d Inode: 4456463 Links: 1
Access: (0777/lrwxrwxrwx) Uid: ( 1000/wx562635) Gid: ( 1000/wx562635)
Access: 2021-02-03 21:14:44.157308596 +0800
Modify: 2021-02-03 21:14:37.772856495 +0800
Change: 2021-02-03 21:14:37.772856495 +0800
实验2: 跨分区创建链接
Filesystem Size Used Avail Use% Mounted on
udev 970M 0 970M 0% /dev
tmpfs 199M 12M 187M 7% /run
/dev/mapper/U16--template--vg-root 97G 2.2G 90G 3% /
tmpfs 992M 0 992M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 992M 0 992M 0% /sys/fs/cgroup
/dev/sda1 720M 111M 573M 17% /boot
tmpfs 199M 0 199M 0% /run/user/1000
在/root目录创建a.txt,并创建硬链接和软链接,都能成功。
root@U16-template:~# ln -P a.txt a.ln
root@U16-template:~# ln -s a.txt a.softlink
root@U16-template:~# ll
total 24
drwx------ 2 root root 4096 Feb 3 21:20 ./
drwxr-xr-x 23 root root 4096 Jan 16 15:27 ../
-rw-r--r-- 2 root root 0 Feb 3 21:18 a.ln
lrwxrwxrwx 1 root root 5 Feb 3 21:20 a.softlink -> a.txt
-rw-r--r-- 2 root root 0 Feb 3 21:18 a.txt
然后到/boot目录执行相同的操作,创建硬链接失败并报错;软链接创建成功
root@U16-template:/boot# ln -P /root/a.txt a.ln
ln: failed to create hard link 'a.ln' => '/root/a.txt': Invalid cross-device link
root@U16-template:/boot# ln -s /root/a.txt a.softlink
root@U16-template:/boot# ll a.softlink
lrwxrwxrwx 1 root root 11 Feb 3 21:22 a.softlink -> /root/a.txt
实验3: 对目录创建链接,目录不允许创建硬链接
root@U16-template:~# ln -s folder folder.softlink
root@U16-template:~# ll
drwx------ 3 root root 4096 Feb 3 21:25 ./
drwxr-xr-x 23 root root 4096 Jan 16 15:27 ../
drwxr-xr-x 2 root root 4096 Feb 3 21:24 folder/
lrwxrwxrwx 1 root root 6 Feb 3 21:25 folder.softlink -> folder/
root@U16-template:~# ln -P folder folder.hardlink
ln: folder: hard link not allowed for directory
实验4: 对不存在的文件或目录创建链接
在当前目录不存在文件nothere的情况下,可以直接创建软链接 aaa.ln 但显示红色代表死链接;不过事后创建nothere文件后,死链接变成了正常链接,并且可以正常使用。
换做硬链接就不行,无法对不存在的文件创建硬链接。
aaa.ln nothere
root@U16-template:~# ln -P nofile no.link
ln: failed to access 'nofile': No such file or directory
root@U16-template:~#