改动连接的源文件是否对硬连接和软连接出来的文件是否有影响?(一个小实验)

回复 收藏
第一次发帖,还请大家多多指出不足。下面是一个小实验,虽说有结论但是不知道为什么会产生这样的结果。还忘大家帮我解释下稍微深一点的理解,为什么会有这样的结果!
[root@CentOs ~]# cd /tmp/test/
[root@CentOs test]# touch file1
[root@CentOs test]# touch file2

首先先创建了两个文件分别为file1和file2.
[root@CentOs test]# ln file1 file1had
[root@CentOs test]# echo 'one'>file1

把file1创建了硬连接file1had,并添加内容“one”
[root@CentOs test]# ll
总用量 8
-rw-r--r-- 2 root root 4 11月 21 23:22 file1
-rw-r--r-- 2 root root 4 11月 21 23:22 file1had
-rw-r--r-- 1 root root 0 11月 21 23:21 file2

-----------------------硬连接实验---------------------
查看test文件夹下的内容,可以看到硬连接创建成功
[root@CentOs test]# cat file1had
one

查看硬连接出的文件file1had文件
[root@CentOs test]# mv file1 /tmp/hard
[root@CentOs test]# ls -l /tmp/hard
-rw-r--r-- 2 root root 4 11月 21 23:22 /tmp/hard

这时要注意了,我把file1文件移动到了test文件夹下的,名称变为hard
[root@CentOs test]# cat file1had
one

此时,我再次查看file1had还是可以访问的。
[root@CentOs test]# echo 'two'>>/tmp/hard
[root@CentOs test]# cat /tmp/test/file1had
one
two

再对把hard文件追加内容(注意这个hard文件是之前的file1文件),并再次查看filehad也是可以访问的
[root@CentOs test]# mv /tmp/hard /etc/ss
[root@CentOs test]# cat /tmp/test/file1had
one
two

这时我再把hard这个硬连接的源文件移到/etc/下,名称变为ss,依旧还是可以访问的
由此得出结论:硬连接的源文件无论移到什么地方,被硬连接的文件还是可以访问。

-------------------软连接实验--------------------
[root@CentOs test]# ln -s file2 softfile2
[root@CentOs test]# ll
总用量 4
-rw-r--r-- 2 root root 8 11月 21 23:24 file1had
-rw-r--r-- 1 root root 0 11月 21 23:21 file2
lrwxrwxrwx 1 root root 5 11月 21 23:39 softfile2 -> file2

为file2创建软连接softfile2,并查看test文件下的内容。
[root@CentOs test]# echo 'one'>file2
[root@CentOs test]# cat softfile2
one

file2添加内容“one”,并查看软连接文件softfile2
[root@CentOs test]# mv file2 /tmp/softfile
将file2移到/tmp/下,名称变为softfile
[root@CentOs test]# cat softfile2
cat: softfile2: 没有那个文件或目录
再次查看软连接文件softfile2,则提示没有那个文件或目录了。
由此得出结论:软连接的源文件移到别的地方后,被软连接的文件是失效了。


2014-11-21 16:11 举报
已邀请:
0

flyfish

赞同来自:

你可以这样想 软连接 目的文件是指向源文件的,所以源文件没了就不能访问了
而硬链接 目的文件是指向源文件的存储的地方,其实源文件也是指向这个地方,它俩是平等的,所以源文件修改了是修改的这个存储的地方,所以硬链接也跟这边,而源文件没了也没关系,因为硬链接不是指向源文件,而是指向储存的地方

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: