您的位置:首页 > 其它

硬链接和软连接

2015-06-16 17:05 232 查看
- = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - =
ln -s file1 file2 # 创建文件软链接
软链接,又叫符号链接。
文件用户数据块存放的是另一个文件的路径名的指向。
软链接就是一个普通文件,只是数据块内容有点特殊。
原文件和软链接的inode不同,但修改任何一个文件都会改变文件的内容。

[root@Centos6 ~]# ls -litotal 4392110 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.txt392109 -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.8modified[root@Centos6 ~]# cat test.log Address: 8.8.8.8modified[root@Centos6 ~]# cat test.doc Address: 8.8.8.8modified[root@Centos6 ~]# rm -f test.doc [root@Centos6 ~]# ls -litotal 4 10060 lrwxrwxrwx 1 root root 6 Jun 16 15:20 test.log -> test.txt392109 -rw-r--r-- 1 root root 32 Jun 16 15:50 test.txt[root@Centos6 ~]# rm -f test.txt [root@Centos6 ~]# ls -litotal 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

本文出自 “勿忘初心” 博客,请务必保留此出处http://winterysea.blog.51cto.com/9677346/1662412
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: