系统链接
硬链接
- 硬链接相当于对文件建立了一个别名,不支持目录
- 两个文件inode号相同,所以这两个文件是相同的文件
- 硬链接只能在同一个文件系统中,只有同一个文件系统中的同一个inode号才会指向同一个数据
- 创建时链接数+1
- 删除文件时
- rm命令时链接数减少
- 文件要存在,至少有一个链接数
- 由于链接数增加了,所以文件还存在
- ln src_file link_file
[root@centos7 data]#ll
total 4
-rw-r--r-- 1 root root 6 Aug 13 20:48 testfile.txt
[root@centos7 data]#ll -h
total 4.0K
-rw-r--r-- 1 root root 6 Aug 13 20:48 testfile.txt
[root@centos7 data]#
[root@centos7 data]#ln testfile.txt testfile_ln.txt
# 大小相同
[root@centos7 data]#ll
total 8
-rw-r--r-- 2 root root 6 Aug 13 20:48 testfile_ln.txt
-rw-r--r-- 2 root root 6 Aug 13 20:48 testfile.txt
# 两个文件的inode相同
[root@centos7 data]#ls -i
68 testfile_ln.txt 68 testfile.txt
软连接(符号链接)
- 相当于文件的快捷方式,依赖于源文件
- inode编号不同
- 支持目录
- 可以跨分区
- ln -s src_file link_file
注意:src_file的相对路径,是相对于link_file的路径而言,多数软连接写的是相对路径
# 大小不同,不是同一个文件
[root@centos7 data]#ln -s /etc/fstab test_link
[root@centos7 data]#ll
total 0
lrwxrwxrwx 1 root root 10 Aug 13 21:00 test_link -> /etc/fstab
[root@centos7 data]#ll /etc/fstab
-rw-r--r-- 1 root root 595 Aug 7 11:26 /etc/fstab
# 跨分区,inode不同
[root@centos7 data]#ls -i /etc/fstab
68034624 /etc/fstab
[root@centos7 data]#ls -i test_link
67 test_link
硬链接和符号链接的区别
本质区别
- 硬链接:同一个文件多个名字
- 软链接:不是同一个文件,而是一个文件对应快捷方式
表现形式
- 链接数
- 硬链接连接数增加
- 软连接不增加
- 文件系统
- 硬链接不跨文件系统
- 软连接可以跨文件系统
- 目录
- 硬链接不支持目录
- 软连接支持
- 原始和链接文件关系
- 软连接依赖于原始文件
- 硬链接不依赖
- 大小
- 硬链接其大小即为文件大小
- 软连接其大小为指向的路径字符串的长度