Linux中软连接与硬连接详细解读

目标:node

1.测试环境模拟linux

2.软连接特性ide

3.硬连接特性测试

4.总结rem


1.测试环境模拟


1it

2table

3class

4test

5import

6

7

8

9

10

11

12

13

[root@localhost home]# mkdir test 建立测试文件夹

[root@localhost home]# cd test/  进入测试文件夹

[root@localhost test]# touch link  建立原文件link

[root@localhost test]# echo "my name is link">>link  写入内容到原文件link

[root@localhost test]# cat link  查看原文件内容

my name is link

[root@localhost test]# ln -s link softlink  建立软连接

[root@localhost test]# ln link hardlink  建立硬连接

[root@localhost test]# ll

total 8

-rw-r--r--. 2 root root 16 Dec 8 18:21 hardlink  硬连接

-rw-r--r--. 2 root root 16 Dec 8 18:21 link  原文件

lrwxrwxrwx. 1 root root 4 Dec 8 18:22 softlink -> link  软连接


2.软连接特性


1

2

3

-rw-r--r--. 2 root root 16 Dec 8 18:21 link 原文件

 

lrwxrwxrwx. 1 root root 4 Dec 8 18:22 softlink -> link 软连接

对比差异是否是发现有几点不一样?

1.原文件inode为2软连接为1

2.权限不一样

3.文件大小不一样

4.软连接后面有个指向link的标志



1

2

[root@localhost test]# cat softlink

my name is link

软连接内容同样。



1

2

3

4

[root@localhost test]# rm softlink

rm: remove symbolic link ‘softlink’? y

[root@localhost test]# cat link

my name is link

删除软连接原文件是正常的



1

2

3

4

[root@localhost test]# rm link

rm: remove regular file ‘link’? y

[root@localhost test]# cat softlink

cat: softlink: No such file or directory

删除原文件软连接找不到文件了,综上证实软连接就是个快捷方式而已!!!


若是我把软连接更名称会发生什么?

1

2

3

4

5

6

7

[root@localhost test]# mv softlink testsoftlink

[root@localhost test]# ll

total 8

-rw-r--r--. 1 root root 16 Dec 8 18:36 link

lrwxrwxrwx. 1 root root 4 Dec 8 18:34 testsoftlink -> link

[root@localhost test]# cat testsoftlink

my name is link

实验证实更名并无什么卵用,打开软连接照样能够看到内容,为何?

由于linux识别一个文件不看名称,看inode值!!!

也就是说inode值相同文件内容同样。


那么文件能够建立软连接,目录能够吗?

1

2

3

4

5

[root@localhost test]# mkdir wj

[root@localhost test]# ln -s wj softwj

[root@localhost test]# ll

lrwxrwxrwx. 1 root root 2 Dec 8 18:54 softwj -> wj

drwxr-xr-x. 2 root root 6 Dec 8 18:54 wj

目录能够建立软连接


3.硬连接特性


1

2

3

4

5

6

[root@localhost test]# ll

total 8

-rw-r--r--. 2 root root 16 Dec 8 18:36 hardlink

-rw-r--r--. 2 root root 16 Dec 8 18:36 link

[root@localhost test]# cat hardlink

my name is link

观察得出硬连接就是个原文件的备份



1

2

3

4

[root@localhost test]# rm link

rm: remove regular file ‘link’? y

[root@localhost test]# cat hardlink

my name is link

删除原文件,硬连接是能够看到内容的,so。这就是与软连接的不一样之处之一。


那么硬连接是否能够像软连接同样建立目录连接呢?

1

2

3

[root@localhost test]# mkdir cs

[root@localhost test]# ln cs hardcs

ln: ‘cs’: hard link not allowed for directory

不能够的。为何呢?

由于那个惟一值!若是目录inode同样会怎么样?

在访问软连接的时候经过软连接直接的跳转到原文件,这样就访问了内容

在访问软连接目录的时候经过遍历目录内容也能够找到,就算文件夹里面inode值有同样的循环了,linux能够在8个循环内终结。

可是

若是咱们的硬连接访问了,其实原文件变不变与它已经没有关系了

咱们的硬连接若是有硬连接目录,那么遍历的时候遇到inode值同样的目录里面的内容,所有遍历一遍,环路至少在目录的linux系统中终结不了,因此硬连接目录是不能建立滴!!!


4.总结

软连接相似快捷方式,原文件内容变了软连接的也会变,影响文件的不是名称而是inode值,软连接是能够建立软连接目录的。

硬连接相似备份,原文件内容变化不影响硬连接,因此一般在工做用做为快照使用,硬连接没有硬连接目录。

相关文章
相关标签/搜索