1 硬链接与软链接的【语法】

软链接:ln -s 源文件 目标文件

硬链接:ln 源文件 目标文件

【-s : symbolic,符号/代号】

2 软链接/硬链接的【比喻】 / (编辑)同步性

[root@centos7 ~]# cat demofile.txt

001 - init - create demofile.txt - 20201023 0036

[root@centos7 ~]# ll

total 4

-rw-r--r-- 1 root root 48 Oct 22 16:37 demofile.txt

2-1 软链接 ≈ Windows的快捷方式

【实验1】

[root@centos7 ~]# ln -s ./demofile.txt ./file.soft

[root@centos7 ~]# ll -i (或ls -i)

total 4

12333 -rw-r--r-- 1 root root 48 Oct 22 16:37 demofile.txt

30833 lrwxrwxrwx 1 root root 14 Oct 22 16:38 file.soft -> ./demofile.txt

【#解释# 从软链接与源文件的[时间戳/innode]不同】

------------------------------------

[root@centos7 ~]# vi file.soft 【编辑软链接 : 验证软链接文件与源文件的同步性】

modify - add content - in file.soft (新增内容)

[root@centos7 ~]# ls -li

total 4

12333 -rw-r--r-- 1 root root 85 Oct 22 16:41 demofile.txt

30833 lrwxrwxrwx 1 root root 14 Oct 22 16:38 file.soft -> ./demofile.txt

【#解释# 从软链接的时间戳可见: 编辑软链接文本后,并没有改变软链接的最近修改时间,而源文件的时间戳被改变了;

说明:

编辑软链接,本质上是在编辑源文件,二者文本内容同步】

[root@centos7 ~]# cat demofile.txt

001 - init - create demofile.txt - 20201023 0036

modify - add content - in file.soft

[root@centos7 ~]# cat file.soft

001 - init - create demofile.txt - 20201023 0036

modify - add content - in file.soft

------------------------------------

[root@centos7 ~]# vi demofile.txt 【编辑源文件 : 验证软链接文件与源文件的同步性】

modify - add content - in demofile.txt (新增内容)

[root@centos7 ~]# ll -i

total 4

12333 -rw-r--r-- 1 root root 124 Oct 22 16:47 demofile.txt

30833 lrwxrwxrwx 1 root root 14 Oct 22 16:38 file.soft -> ./demofile.txt

【#解释# 从软链接的时间戳可见: 编辑源文件文本后,并没有改变软链接的最近修改时间,而源文件的时间戳被改变了;

说明:

编辑源文件,本质上是在编辑源文件,依旧不会变动软链接的修改时间,二者文本内容同步】

[root@centos7 ~]# cat file.soft

001 - init - create demofile.txt - 20201023 0036

modify - add content - in file.soft

modify - add content - in demofile.txt

[root@centos7 ~]# cat demofile.txt

001 - init - create demofile.txt - 20201023 0036

modify - add content - in file.soft

modify - add content - in demofile.txt

通过上述实验,可证明: 软链接就是Windows中的快捷方式!

2-2 硬链接 ≈ cp -p + 同步

【实验2】

[root@centos7 ~]# ln ./demofile.txt ./file.hard

[root@centos7 ~]# ll -i (或ls -i)

total 8

12333 -rw-r--r-- 2 root root 124 Oct 22 16:47 demofile.txt

12333 -rw-r--r-- 2 root root 124 Oct 22 16:47 file.hard

30833 lrwxrwxrwx 1 root root 14 Oct 22 16:38 file.soft -> ./demofile.txt

【#解释# 从硬链接与源文件的[时间戳/innode]完全相同】

------------------------------------

[root@centos7 ~]# cp demofile.txt demofile.txt.cp1 【拷贝源文件(cp),验证: 拷贝文件的时间戳是否与源文件一致】

[root@centos7 ~]# ll -i

total 12

12333 -rw-r--r-- 2 root root 124 Oct 22 16:47 demofile.txt

30834 -rw-r--r-- 1 root root 124 Oct 22 16:56 demofile.txt.cp1

12333 -rw-r--r-- 2 root root 124 Oct 22 16:47 file.hard

30833 lrwxrwxrwx 1 root root 14 Oct 22 16:38 file.soft -> ./demofile.txt

【#解释# 拷贝文件(cp)的[时间戳/innode]与源文件不一致】

------------------------------------

[root@centos7 ~]# cp -p demofile.txt demofile.txt.cp2 【拷贝源文件(cp -p),验证: 拷贝文件的时间戳是否与源文件一致】

【#解释# cp -p的 -p:= same as --preserve=mode,ownership,timestamps】

[root@centos7 ~]# ll -i

total 16

12333 -rw-r--r-- 2 root root 124 Oct 22 16:47 demofile.txt

30834 -rw-r--r-- 1 root root 124 Oct 22 16:56 demofile.txt.cp1

29719 -rw-r--r-- 1 root root 124 Oct 22 16:47 demofile.txt.cp2

12333 -rw-r--r-- 2 root root 124 Oct 22 16:47 file.hard

30833 lrwxrwxrwx 1 root root 14 Oct 22 16:38 file.soft -> ./demofile.txt

【#解释# 拷贝文件(cp -p)的[innode]与源文件不一致,但[时间戳]一致!】

------------------------------------

[root@centos7 ~]# vi file.hard 【编辑硬链接 : 验证硬链接文件与源文件的同步性】

modify - add content - in file.hard(新增内容)

[root@centos7 ~]# ls -li

total 16

12333 -rw-r--r-- 2 root root 160 Oct 22 17:02 demofile.txt

30834 -rw-r--r-- 1 root root 124 Oct 22 16:56 demofile.txt.cp1

29719 -rw-r--r-- 1 root root 124 Oct 22 16:47 demofile.txt.cp2

12333 -rw-r--r-- 2 root root 160 Oct 22 17:02 file.hard

30833 lrwxrwxrwx 1 root root 14 Oct 22 16:38 file.soft -> ./demofile.txt

【#解释# 从硬链接的时间戳可见: 编辑硬链接文本后,硬链接文件的时间戳被改变了,且也已同步改变了源文件的最近修改时间!

说明:

编辑硬链接,本质上也是在编辑源文件,二者文本内容同步】

[root@centos7 ~]# cat demofile.txt

001 - init - create demofile.txt - 20201023 0036

modify - add content - in file.soft

modify - add content - in demofile.txt

modify - add content - in file.hard

[root@centos7 ~]# cat file.hard

001 - init - create demofile.txt - 20201023 0036

modify - add content - in file.soft

modify - add content - in demofile.txt

modify - add content - in file.hard

------------------------------------

[root@centos7 ~]# vi demofile.txt 【编辑源文件 : 验证硬链接文件与源文件的同步性】

modify - add content - in demofile.txt (新增内容)

[root@centos7 ~]# ll -i

total 16

12333 -rw-r--r-- 2 root root 199 Oct 22 17:06 demofile.txt

30834 -rw-r--r-- 1 root root 124 Oct 22 16:56 demofile.txt.cp1

29719 -rw-r--r-- 1 root root 124 Oct 22 16:47 demofile.txt.cp2

12333 -rw-r--r-- 2 root root 199 Oct 22 17:06 file.hard

30833 lrwxrwxrwx 1 root root 14 Oct 22 16:38 file.soft -> ./demofile.txt

【#解释# 从硬链接/软链接/源文件的时间戳可见: 编辑源文件文本后,源文件和硬文件的时间戳被改变了,但没有改变软链接的最近修改时间。

说明:

编辑源文件,本质上是在编辑源文件/硬文件,依旧不会变动软链接的修改时间,三者文本内容同步】

[root@centos7 ~]# cat file.hard

001 - init - create demofile.txt - 20201023 0036

modify - add content - in file.soft

modify - add content - in demofile.txt

modify - add content - in file.hard

modify - add content - in demofile.txt

[root@centos7 ~]# cat file.soft

001 - init - create demofile.txt - 20201023 0036

modify - add content - in file.soft

modify - add content - in demofile.txt

modify - add content - in file.hard

modify - add content - in demofile.txt

[root@centos7 ~]# cat demofile.txt

001 - init - create demofile.txt - 20201023 0036

modify - add content - in file.soft

modify - add content - in demofile.txt

modify - add content - in file.hard

modify - add content - in demofile.txt

通过上述实验,可证明:

硬链接 := cp -p + 同步

【小结】

通过上面的实验,还可知:

【(编辑)同步性】:编辑 硬链接/软链接/源文件 后,三者的文本内容始终保持同步

3 索引节点 | 删除(源文件/软链接/硬链接)操作的影响

3-1 删除操作的影响

删除软链接: 源文件/硬链接文件 存在,且可正常使用

删除硬链接: 源文件/软链接文件 存在,且可正常使用

删除源文件: 软链接 存在,但不可正常使用; 但硬链接 也存在,且可正常使用

3-2 索引节点

因此: 删除了源文件(demofile.txt),只是删除了从索引节点(12333)到源文件(demofile.txt)的映射关系,但不影响索引节点(12333)和新的硬链接文件(file.hard)的映射关系

【硬链接】

硬链接指通过索引节点来与源文件进行连接/同步。

在Linux的文件系统中,保存在磁盘分区中的文件不管是什么类型都给它分配1个编号,称为索引节点号(Inode Index)。

在Linux中,多个文件名可指向同一索引节点是存在的。一般这种链接,就是硬连接。

硬连接的作用是允许1个文件拥有N个有效路径名,这样用户就可以建立硬链接到重要文件,以【防止用户误删】的功能。

其原因如上所述,因为对应该目录的索引节点有1个以上的连接。

只删除一个连接并不影响索引节点本身和其它的连接,只有当最后一个连接被删除后,文件的数据块及目录的连接才会被释放。

也就是说,文件真正删除的条件是与之相关的所有硬连接文件均被删除。

【软链接】

另外一种链接称之为符号链接(Symbolic Link),也叫软链接。

软链接文件有类似于Windows的快捷方式。它实际上是一个特殊的文件。

在符号链接中,文件实际上是一个文本文件,其中包含的有另一文件的位置信息。

4 ln命令说明

-b like --backup but does not accept an argument

-d, -F, --directory allow the superuser to attempt to hard link

directories (note: will probably fail due to

system restrictions, even for the superuser)

-f, --force remove existing destination files

-i, --interactive prompt whether to remove destinations

-L, --logical dereference TARGETs that are symbolic links

-n, --no-dereference treat LINK_NAME as a normal file if

it is a symbolic link to a directory

-P, --physical make hard links directly to symbolic links

-r, --relative create symbolic links relative to link location

-s, --symbolic make symbolic links instead of hard links

-S, --suffix=SUFFIX override the usual backup suffix

-t, --target-directory=DIRECTORY specify the DIRECTORY in which to create

the links

-T, --no-target-directory treat LINK_NAME as a normal file always

-v, --verbose print name of each linked file

--help display this help and exit

--version output version information and exit

X 参考文献

软链接和硬链接到底有啥作用和区别 [推荐] - CSDN

Linux创建链接命令 ln -s创建软链接 - 博客园

5分钟让你明白“软链接”和“硬链接”的区别 - CSDN

文章链接

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。