linux软链接重启,linux 软连接与硬链接

实验环境

创建链接文件

root@mark:~/link_test# ls -l -h

total 28K

-rw-r--r-- 1 root root 26K May 6 02:56 qemu.help

root@mark:~/link_test# ln -s qemu.help soft_link

root@mark:~/link_test# ln qemu.help hard_link

root@mark:~/link_test# ls -l -i

total 56

1048801 -rw-r--r-- 2 root root 26000 May 6 02:56 hard_link

1048801 -rw-r--r-- 2 root root 26000 May 6 02:56 qemu.help

1048802 lrwxrwxrwx 1 root root 9 May 6 03:00 soft_link -> qemu.help

软链接文件的9的意义是保存了"qemu.help"这个字符串。

修改源文件名称

root@mark:~/link_test# ls -l -h

total 56K

-rw-r--r-- 2 root root 26K May 6 02:56 hard_link

-rw-r--r-- 2 root root 26K May 6 02:56 qemu.help_new

lrwxrwxrwx 1 root root 9 May 6 03:00 soft_link -> qemu.help

软链接已经失效,硬链接正常

删除文件

root@mark:~/link_test# ls -l -h

total 56K

-rw-r--r-- 2 root root 26K May 6 02:56 hard_link

-rw-r--r-- 2 root root 26K May 6 02:56 qemu.help

lrwxrwxrwx 1 root root 9 May 6 03:00 soft_link -> qemu.help

root@mark:~/link_test# rm hard_link

root@mark:~/link_test# rm soft_link

root@mark:~/link_test# ls -l -h

total 28K

-rw-r--r-- 1 root root 26K May 6 02:56 qemu.help

删除两个链接文件都不影响源文件的访问

root@mark:~/link_test# ln qemu.help hard_link2

root@mark:~/link_test# ln -s qemu.help soft_link2

root@mark:~/link_test# ls -l

total 56

-rw-r--r-- 2 root root 26000 May 6 02:56 hard_link2

-rw-r--r-- 2 root root 26000 May 6 02:56 qemu.help

lrwxrwxrwx 1 root root 9 May 6 03:06 soft_link2 -> qemu.help

root@mark:~/link_test# rm qemu.help

root@mark:~/link_test# ls -l

total 28

-rw-r--r-- 1 root root 26000 May 6 02:56 hard_link2

lrwxrwxrwx 1 root root 9 May 6 03:06 soft_link2 -> qemu.help

删除源文件后,硬链接可以正常访问,软链接已经失效

root@mark:~/link_test# echo "abcd " > qemu.help

root@mark:~/link_test# cat soft_link2

abcd

新建一个同名的源文件,软链接又恢复正常了,再次证明软链接只是记录了一个源文件的路径名称

删除源文件后,即使重启后,硬链接也不会失效。

跨文件系统

total 4

-rw-r--r-- 1 root root 6 May 6 03:07 qemu.help

lrwxrwxrwx 1 root root 9 May 6 03:06 soft_link2 -> qemu.help

root@mark:~/link_test# ln qemu.help /var/run/xx

ln: failed to create hard link '/var/run/xx' => 'qemu.help': Invalid cross-device link

root@mark:~/link_test# ln -s qemu.help /var/run/xx

root@mark:~/link_test#

每个文件系统都有各自的inode的分配方法。

所以硬链接不能跨文件系统,软链接是新建了一个dentry,软链接磁盘上只是存储了源文件的一个路径名称

根据inode删除文件

root@mark:~/link_test# ls -l -i

total 32

1048801 -rw-r--r-- 1 root root 26000 May 6 02:56 hard_link2

1048803 -rw-r--r-- 1 root root 6 May 6 03:07 qemu.help

1048802 lrwxrwxrwx 1 root root 9 May 6 03:06 soft_link2 -> qemu.help

root@mark:~/link_test# find . -inum 1048801

./hard_link2

root@mark:~/link_test# find . -inum 1048801 -delete

root@mark:~/link_test# ls -l

total 4

-rw-r--r-- 1 root root 6 May 6 03:07 qemu.help

lrwxrwxrwx 1 root root 9 May 6 03:06 soft_link2 -> qemu.help

总结

根据上面的测试,可以大概了解软链接与硬链接的区别,可以根据不同的需要做不同的链接,也可以用于定位一些问题。

软链接

可以跨文件系统

文件类型为 l(链接文件)

与源文件的inode号不同

文件大小是源文件的路径名称字符串的长度

硬链接

文件类型为 - (普通文件)

与源文件的inode号相同

stat命令查看文件,links加1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值