Linux软链接与硬链接的区别

软链接与硬链接的区别

首先从使用的角度讲,两者没有任何区别,都与正常的文件访问方式一样,支持读写,如果是可执行文件的话也可以直接执行。区别在于底层原理,举例说明:

  • 创建一个文件
touch testfile && echo "this is a test file" > testfile
cat testfile
this is a test file
  • 创建testfile的硬链接
$ ln testfile hard
$ ls -li
8716753368 -rw-r--r--  3 test  staff  20  2  5 09:27 hard
8716753368 -rw-r--r--  3 test  staff  20  2  5 09:27 testfile
  • 修改刚刚创建的硬链接
$ echo "add hard link" >> hard
$ cat myfile

This is a plain text file.
add hard link
  • 创建软链接
$ ln -s testfile soft
$ ls -li
8716753368 -rw-r--r--  3 test  staff  20  2  5 09:27 hard
8716786476 lrwxr-xr-x  1 test  staff   8  2  5 09:57 soft -> testfile
8716753368 -rw-r--r--  3 test  staff  20  2  5 09:27 testfile
  • 删除testfile
$ rm testfile 
$ cat hard 
this is a test file
$ cat soft
cat: soft: No such file or directory
  • 修改soft文件
$ echo "hello" >> soft
$ ls
hard     soft     testfile

总结:
硬链接: 与普通文件没什么不同,inode 都指向同一个文件在硬盘中的区块
软链接: 保存了其代表的文件的绝对路径,是另外一种文件,在硬盘上有独立的区块,访问时替换自身路径。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容