软链接与硬链接的区别
首先从使用的角度讲,两者没有任何区别,都与正常的文件访问方式一样,支持读写,如果是可执行文件的话也可以直接执行。区别在于底层原理,举例说明:
- 创建一个文件
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 都指向同一个文件在硬盘中的区块
软链接: 保存了其代表的文件的绝对路径,是另外一种文件,在硬盘上有独立的区块,访问时替换自身路径。