软连接:
符号连接(Symbolic Link),也叫软连接。软链接文件有类似于Windows的快捷方式。它实际上是一个特殊的文件。在符号连接中,文件实际上是一个文本文件,其中包含的有另一文件的位置信息。
语法格式:ln -s 目标文件(绝对路径) 连接名称(或绝对路径)
例如:
[root@centos7-01 ~]# ls -l
总用量 4
-rw-------. 1 root root 1418 5月 25 06:43 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 6月 8 02:07 test123123
[root@centos7-01 ~]# ln -s test123123 123
[root@centos7-01 ~]# ls -l
总用量 4
lrwxrwxrwx. 1 root root 10 6月 8 02:25 123 -> test123123
-rw-------. 1 root root 1418 5月 25 06:43 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 6月 8 02:07 test123123
[root@centos7-01 ~]#
总结:软连接创建 最后使用绝对路径,可以为文件、目录创建软件,可以跨分区创建软件;不能对源文件进行删除,否则连接文件会失效。
硬链接:
说明:
硬连接指通过索引节点来进行连接。在Linux的文件系统中,保存在磁盘分区中的文件不管是什么类型都给它分配一个编号,称为索引节点号(Inode Index)。在Linux中,多个文件名指向同一索引节点是存在的。一般这种连接就是硬连接。硬连接的作用是允许一个文件拥有多个有效路径名,这样用户就可以建立硬连接到重要文件,以防止“误删”的功能。其原因如上所述,因为对应该目录的索引节点有一个以上的连接。只删除一个连接并不影响索引节点本身和其它的连接,只有当最后一个连接被删除后,文件的数据块及目录的连接才会被释放。也就是说,文件真正删除的条件是与之相关的所有硬连接文件均被删除。
例子:
[root@centos7-01 ~]# ls -l
总用量 4
lrwxrwxrwx. 1 root root 10 6月 8 02:25 123 -> test123123
-rw-------. 1 root root 1418 5月 25 06:43 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 6月 8 02:07 test123123
[root@centos7-01 ~]# ln anaconda-ks.cfg ans-header.cfg
[root@centos7-01 ~]# ls -l
总用量 8
lrwxrwxrwx. 1 root root 10 6月 8 02:25 123 -> test123123
-rw-------. 2 root root 1418 5月 25 06:43 anaconda-ks.cfg
-rw-------. 2 root root 1418 5月 25 06:43 ans-header.cfg
drwxr-xr-x. 2 root root 6 6月 8 02:07 test123123
[root@centos7-01 ~]# ls -li
总用量 8
33580682 lrwxrwxrwx. 1 root root 10 6月 8 02:25 123 -> test123123
33580642 -rw-------. 2 root root 1418 5月 25 06:43 anaconda-ks.cfg
33580642 -rw-------. 2 root root 1418 5月 25 06:43 ans-header.cfg
50718116 drwxr-xr-x. 2 root root 6 6月 8 02:07 test123123
[root@centos7-01 ~]# rm -f anaconda-ks.cfg
[root@centos7-01 ~]# ls -li
总用量 4
33580682 lrwxrwxrwx. 1 root root 10 6月 8 02:25 123 -> test123123
33580642 -rw-------. 1 root root 1418 5月 25 06:43 ans-header.cfg
50718116 drwxr-xr-x. 2 root root 6 6月 8 02:07 test123123
[root@centos7-01 ~]#
总结: 硬链接可以为文件创建,不能对目录创建硬链接,不能进行跨分区创建硬链接;可以对源文件进行删除。