软链接和硬链接的区别-个人笔记

建立软链接和硬链接的shell命令:

# 对file文件建立硬链接
ln file hard

# 对file文件建立软链接
ln -s file soft

以下为实验过程以及总结

fei@hutu:~/Desktop/temp$ touch file # 建立一个准备建立软链接和硬链接的文件
fei@hutu:~/Desktop/temp$ ln file hard  # 为文件file建立一个硬链接 hard
fei@hutu:~/Desktop/temp$ ln -s file soft  # 为文件file建立一个软链接 soft
fei@hutu:~/Desktop/temp$ ls -li   # 查看当前目录下所有文件的类型及其他信息
total 0
667807 -rw-rw-r-- 2 fei fei 0 10月 10 21:46 file
fei@hutu:~/Desktop/temp$ echo "content for terminal" > hard  # 根据硬链接在inode对应的内存中写入内容
fei@hutu:~/Desktop/temp$ cat soft  # 根据软链接查看对应文件对应内存的内容
content for terminal
fei@hutu:~/Desktop/temp$ rm file   # 删除原用于建立软 硬链接的文件
fei@hutu:~/Desktop/temp$ ls -li    # 可以看到硬链接不改变 但是软链接失效
total 4
667807 -rw-rw-r-- 1 fei fei 21 10月 10 21:50 hard
668122 lrwxrwxrwx 1 fei fei  4 10月 10 21:47 soft -> file
fei@hutu:~/Desktop/temp$ cat soft
cat: soft: No such file or directory
fei@hutu:~/Desktop/temp$ cat hard  # 硬链接不受影响 inode不变 内容也不变
content for terminal
fei@hutu:~/Desktop/temp$ touch file  # 在对应路径下面建立相同原始同名但内容不作要求的文件
fei@hutu:~/Desktop/temp$ ls -li
total 4
660321 -rw-rw-r-- 1 fei fei  0 10月 10 21:56 file
667807 -rw-rw-r-- 1 fei fei 21 10月 10 21:50 hard
668122 lrwxrwxrwx 1 fei fei  4 10月 10 21:47 soft -> file
fei@hutu:~/Desktop/temp$ cat file  # 由于file在建立的时候没有写入内容,故为空
fei@hutu:~/Desktop/temp$ cat soft  # soft此时重新恢复链接,但是指向新的file
fei@hutu:~/Desktop/temp$ cat hard  # hard 的inode和内容不改变
content for terminal

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值