软链接和硬链接的区别

软链接和硬链接的区别

  1. 本质
    硬链接:本质是同一个文件
    软链接:本质不是同一个文件
  2. 跨设备
    硬链接:不支持
    软链接:支持
  3. inode
    硬链接:相同
    软链接:不同
  4. 链接数
    硬链接:创建新的硬链接,链接数会增加,删除硬链接,链接数减少
    软链接:创建或删除,链接数不会变化
  5. 文件夹
    硬链接:不支持
    软链接:支持
  6. 相对路径
    硬链接:原始文件相对路径是相对于当前工作目录
    软链接:原始文件的相对路径是相对于链接文件的相对路径
  7. 删除源文件
    硬链接:只是链接数减一,但链接文件的访问不受影响
    软链接:链接文件将无法访问
  8. 文件类型
    硬链接:和源文件相同
    软链接:链接文件,和源文件无关
  9. 文件大小
    硬链接: 和源文件相同
    软链接: 源文件的路径的长度

实例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:~#
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • JVM 1:JVM基础知识 什么是JVM 常见的JVM 2:ClassFileFormat 3:类编译-加载-初始...
    皮皮魏阅读 241评论 0 0
  • 一、目录结构 1.Windows文件系统 看到的是一个个驱动器盘符,例如:C盘,D盘等每个驱动器都有自己的根目录结...
    郑元吉阅读 683评论 0 0
  • 一、选择题 1.1 设超级用户root当前所在目录为:/usr/local,键入cd命令后, 用户当前所在目录为(...
    闫梦超阅读 410评论 0 0
  • 基本Dos命令 切换盘符 例如 盘符: (注意为英文冒号) 查看当前目录下的所有文件 切换目录 切换目录(chan...
    倒数第一的学霸阅读 153评论 0 0
  • 一、选择题(每题2分)1.1 设超级用户root当前所在目录为:/usr/local,键入cd命令后, 用户当前所...
    蜜桃猫七七阅读 264评论 0 0