1、链接的作用:为一个文件在其他位置创建链接。
格式: ln [-S] 源文件或[目录] 目标文件或[目录]
解释说明:在linux系统中,如文件A需要在多处被使用时,若无链接,则我们会将文件A复制到多个文件夹下,当源文件被修改时,又需要再次把源文件复制替换到所需的目录下,不仅浪费空间,操作也不方便,于是链接产生了。
2、链接分为软链接和硬链接,不同之处如下:
硬链接:类似于windows的备份
-
索引节点
在linux系统中,系统是通过索引节点而非文件名来定位每一个文件,当文件被创建时,内核为每个新建的文件分配一个唯一的索引节点,它保存了本文件的属性。inode可简单理解为文件指针,它指向文件的实际存储位置。在访问文件时,索引节点被复制到内存中(实现了文件的快速访问),由于硬链接是指向索引节点的,所以文件位置的改变,不会影响到硬链接.
3、软链接和硬链接的操作
-----------3.1 以可读方式 查看全部的文件系统:-----------
[root@lotus-wxj home]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 37G 17G 21G 44% /
devtmpfs 894M 0 894M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 11M 900M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/sda1 1014M 180M 835M 18% /boot
tmpfs 182M 48K 182M 1% /run/user/1000
/dev/sr0 4.3G 4.3G 0 100% /run/media/wxj/CentOS 7 x86_64
--------------------3.2 建立软链接:-----------------------
*******跨文件系统建立软链接:
[root@lotus-wxj home]# ln aa /home/dtest
[root@lotus-wxj home]# ls -li /home /dev/test/bb
173861 lrwxrwxrwx. 1 root root 2 5月 4 13:35 /dev/test/bb -> aa
/home:
总用量 0
76 -rw-r--r--. 3 root root 0 5月 4 11:47 aa
67146868 drwxr-xr-x. 2 root root 16 5月 4 13:30 dtest
100828569 drwxr-xr-x. 2 root root 6 5月 4 13:31 dtest1
76 -rw-r--r--. 3 root root 0 5月 4 11:47 f2
********为目录建立软链接:
[root@lotus-wxj home]# ln -s dtest dtest2
[root@lotus-wxj home]# ls -li /home /dev/test
/dev/test:
总用量 0
173861 lrwxrwxrwx. 1 root root 2 5月 4 13:35 bb -> aa
/home:
总用量 0
76 -rw-r--r--. 3 root root 0 5月 4 11:47 aa
67146868 drwxr-xr-x. 2 root root 16 5月 4 13:30 dtest
77634 lrwxrwxrwx. 1 root root 5 5月 4 13:42 dtest2 -> dtest
76 -rw-r--r--. 3 root root 0 5月 4 11:47 f2
---------------3.3 建立硬链接--------------------
**********为文件aa建立硬链接f2,aa和f2的节点信息一致
[root@lotus-wxj home]# ln aa f2
aa f2
[root@lotus-wxj home]# ls -li
总用量 0
76 -rw-r--r--. 2 root root 0 5月 4 11:47 aa
76 -rw-r--r--. 2 root root 0 5月 4 11:47 f2
-
跨文件系统建立硬链接时报错:
[root@lotus-wxj home]# ln aa /dev/test/hf1
ln: 无法创建硬链接"/dev/test/hf1" => “aa”: 无效的跨设备连接 -
为目录建立硬链接时报错:
[root@lotus-wxj home]# ln dtest dtest1
ln: “dtest”: 不允许将硬链接指向目录
--------------3.4 删除软链接和硬链接----------------
删除文件aa,软链接并未删除,因为aa还有硬链接f2存在,aa删除,链接数目原来为3,现在减一变成了2
[root@lotus-wxj home]# rm -rf aa
[root@lotus-wxj home]# ls -li /home /dev/test
/dev/test:
总用量 0
173861 lrwxrwxrwx. 1 root root 2 5月 4 13:35 bb -> aa
/home:
总用量 0
67146868 drwxr-xr-x. 2 root root 16 5月 4 13:30 dtest
77634 lrwxrwxrwx. 1 root root 5 5月 4 13:42 dtest2 -> dtest
76 -rw-r--r--. 2 root root 0 5月 4 11:47 f2
********文件cc创建了两个软链接sf1和sf2,把cc删除后,sf1和sf2指向cc颜色闪烁因为目标文件为空,但软链接是可以指向不存在的文件的
[root@lotus-wxj home]# ls -li /home
总用量 0
77639 -rw-r--r--. 1 root root 0 5月 4 13:53 cc
67146868 drwxr-xr-x. 2 root root 16 5月 4 13:30 dtest
77634 lrwxrwxrwx. 1 root root 5 5月 4 13:42 dtest2 -> dtest
77640 lrwxrwxrwx. 1 root root 2 5月 4 13:53 sf1 -> cc
77669 lrwxrwxrwx. 1 root root 2 5月 4 13:54 sf2 -> cc
[root@lotus-wxj home]# rm cc
[root@lotus-wxj home]# ls -li
总用量 0
67146868 drwxr-xr-x. 2 root root 16 5月 4 13:30 dtest
77634 lrwxrwxrwx. 1 root root 5 5月 4 13:42 dtest2 -> dtest
77640 lrwxrwxrwx. 1 root root 2 5月 4 13:53 ~~sf1 -> cc~~
77669 lrwxrwxrwx. 1 root root 2 5月 4 13:54 ~~sf2 -> cc~~