首页 > 其他 > 详细

硬链接和软连接

时间:2015-06-16 19:26:45      阅读:188      评论:0      收藏:0      [点我收藏+]


- = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = 

ln -s file1 file2 # 创建文件软链接

软链接,又叫符号链接。

文件用户数据块存放的是另一个文件的路径名的指向。

软链接就是一个普通文件,只是数据块内容有点特殊。

原文件和软链接的inode不同,但修改任何一个文件都会改变文件的内容。


[root@Centos6 ~]# ls -li

total 4

392110 lrwxrwxrwx 1 root root  6 Jun 16 15:20 test.doc -> test.log

 10060 lrwxrwxrwx 1 root root  6 Jun 16 15:20 test.log -> test.txt

392109 -rw-r--r-- 1 root root 24 Jun 16 15:47 test.txt

[root@Centos6 ~]# cat test.txt 

Address: 8.8.8.8

[root@Centos6 ~]# cat test.log 

Address: 8.8.8.8

[root@Centos6 ~]# cat test.doc 

Address: 8.8.8.8

[root@Centos6 ~]# echo -n modified >> test.txt 

[root@Centos6 ~]# cat test.txt 

Address: 8.8.8.8

modified[root@Centos6 ~]# cat test.log 

Address: 8.8.8.8

modified[root@Centos6 ~]# cat test.doc 

Address: 8.8.8.8

modified[root@Centos6 ~]# rm -f test.doc 

[root@Centos6 ~]# ls -li

total 4

 10060 lrwxrwxrwx 1 root root  6 Jun 16 15:20 test.log -> test.txt

392109 -rw-r--r-- 1 root root 32 Jun 16 15:50 test.txt

[root@Centos6 ~]# rm -f test.txt 

[root@Centos6 ~]# ls -li

total 0

 10060 lrwxrwxrwx lrwxrwxrwx 1 root root 6 Jun 16 15:20 test.log -> test.txt

[root@Centos6 ~]# cat test.log 

cat: test.log: No such file or directory


test.txt为原文件

test.log为test.txt的软链接

test.doc为test.log的软链接


各文件的inode不同。当修改任何一个文件时其他文件也跟着改变。

当删除原文件test.txt后,test.log则变成了死链接,ls将显示为红色,并且看不到文件内容了。




- = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = 

ln file1 file2       # 创建硬链接
link file1 file2     # 创建硬链接

硬链接就是一个文件使用了多个别名(他们有共同的inode)。

由于硬链接是有着相同inode号仅文件名不同的文件,因此删除一个硬链接文件并不影响其他有相同inode号的文件。

硬链接只能对文件创建,不能对目录创建。

硬链接可由link或者ln命令创建。


[u02@Centos6 ~]$ cat test.txt 

Hello u02

[u02@Centos6 ~]$ ls -li

total 4

128020 -rw-r--r-- 1 u02 u02 10 Jun 16 16:51 test.txt

[u02@Centos6 ~]$ ln test.txt test01.txt

[u02@Centos6 ~]$ ls -li

total 8

128020 -rw-r--r-- 2 u02 u02 10 Jun 16 16:51 test01.txt

128020 -rw-r--r-- 2 u02 u02 10 Jun 16 16:51 test.txt

[u02@Centos6 ~]$ echo "Hello $USER" >> test01.txt

[u02@Centos6 ~]$ cat test01.txt 

Hello u02

Hello u02

[u02@Centos6 ~]$ cat test.txt 

Hello u02

Hello u02

[u02@Centos6 ~]$ rm -f test.txt 

[u02@Centos6 ~]$ cat test01.txt 

Hello u02

Hello u02










本文出自 “winterysea的博客” 博客,请务必保留此出处http://winterysea.blog.51cto.com/9677346/1662412

硬链接和软连接

原文:http://winterysea.blog.51cto.com/9677346/1662412

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!