Linux —— 软链接和硬链接

一、引入知识

ls -l能显示出文件的详细信息,那么权限位后面的数字是什么意思呢?

  • 如果是目录:这个数字就表示目录里还有几个目录(包括隐藏目录...
  • 如果是文件:就表示该文件的连接数
[root@lamp tmp]# ll
total 0
drwxr-xr-x 3 root root 18 Jul 26 12:52 dir
-rw-r--r-- 1 root root  0 Jul 26 12:51 file1
-rw-r--r-- 1 root root  0 Jul 26 12:51 file2
-rw-r--r-- 1 root root  0 Jul 26 12:51 file3
-rw-r--r-- 1 root root  0 Jul 26 12:51 file4
[root@lamp tmp]# ll -a dir
total 0
drwxr-xr-x 3 root root 18 Jul 26 12:52 .
drwxr-xr-x 3 root root 69 Jul 26 12:51 ..
drwxr-xr-x 2 root root  6 Jul 26 12:52 test

二、软链接

1.软连接的创建

1)文件创建软链接

语法:ln -s [源文件] [连接文件]

[root@lamp tmp]# ll
total 0
drwxr-xr-x 3 root root 18 Jul 26 12:52 dir
-rw-r--r-- 1 root root  0 Jul 26 12:51 file1
-rw-r--r-- 1 root root  0 Jul 26 12:51 file2
-rw-r--r-- 1 root root  0 Jul 26 12:51 file3
-rw-r--r-- 1 root root  0 Jul 26 12:51 file4
[root@lamp tmp]# ln -s file1 test1
[root@lamp tmp]# ll
total 0
drwxr-xr-x 3 root root 18 Jul 26 12:52 dir
-rw-r--r-- 1 root root  0 Jul 26 12:51 file1
-rw-r--r-- 1 root root  0 Jul 26 12:51 file2
-rw-r--r-- 1 root root  0 Jul 26 12:51 file3
-rw-r--r-- 1 root root  0 Jul 26 12:51 file4
lrwxrwxrwx 1 root root  5 Jul 26 12:59 test1 -> file1

改变连接文件和源文件

[root@lamp tmp]# cat file1
[root@lamp tmp]# cat test1
[root@lamp tmp]# echo "hello" >> file1
[root@lamp tmp]# cat test1
hello
[root@lamp tmp]# echo "world" >> test1
[root@lamp tmp]# cat file1
hello
world

发现,改变链接文件源文件也会改变,同样改变源文件链接文件也会跟着改变

2)目录创建软链接

[root@lamp ~]# ln -s tmp dirtest
[root@lamp ~]# ll
lrwxrwxrwx   1 root root         3 Jul 26 13:05 dirtest -> tmp
drwxr-xr-x   3 root root        82 Jul 26 12:59 tmp

[root@lamp ~]# ll tmp
total 4
drwxr-xr-x 3 root root 18 Jul 26 12:52 dir
-rw-r--r-- 1 root root 12 Jul 26 13:01 file1
-rw-r--r-- 1 root root  0 Jul 26 12:51 file2
-rw-r--r-- 1 root root  0 Jul 26 12:51 file3
-rw-r--r-- 1 root root  0 Jul 26 12:51 file4
lrwxrwxrwx 1 root root  5 Jul 26 12:59 test1 -> file1

[root@lamp ~]# ll dirtest/
total 4
drwxr-xr-x 3 root root 18 Jul 26 12:52 dir
-rw-r--r-- 1 root root 12 Jul 26 13:01 file1
-rw-r--r-- 1 root root  0 Jul 26 12:51 file2
-rw-r--r-- 1 root root  0 Jul 26 12:51 file3
-rw-r--r-- 1 root root  0 Jul 26 12:51 file4
lrwxrwxrwx 1 root root  5 Jul 26 12:59 test1 -> file1

修改链接目录和源目录

[root@lamp ~]# ll tmp
total 4
drwxr-xr-x 3 root root 18 Jul 26 12:52 dir
-rw-r--r-- 1 root root 12 Jul 26 13:01 file1
-rw-r--r-- 1 root root  0 Jul 26 12:51 file2
-rw-r--r-- 1 root root  0 Jul 26 12:51 file3
-rw-r--r-- 1 root root  0 Jul 26 12:51 file4
lrwxrwxrwx 1 root root  5 Jul 26 12:59 test1 -> file1
[root@lamp ~]# touch dirtest/test.c
[root@lamp ~]# ll tmp
total 4
drwxr-xr-x 3 root root 18 Jul 26 12:52 dir
-rw-r--r-- 1 root root 12 Jul 26 13:01 file1
-rw-r--r-- 1 root root  0 Jul 26 12:51 file2
-rw-r--r-- 1 root root  0 Jul 26 12:51 file3
-rw-r--r-- 1 root root  0 Jul 26 12:51 file4
lrwxrwxrwx 1 root root  5 Jul 26 12:59 test1 -> file1
-rw-r--r-- 1 root root  0 Jul 26 13:07 test.c


[root@lamp ~]# rm tmp/file1 -f
[root@lamp ~]# ll dirtest/
total 0
drwxr-xr-x 3 root root 18 Jul 26 12:52 dir
-rw-r--r-- 1 root root  0 Jul 26 12:51 file2
-rw-r--r-- 1 root root  0 Jul 26 12:51 file3
-rw-r--r-- 1 root root  0 Jul 26 12:51 file4
lrwxrwxrwx 1 root root  5 Jul 26 12:59 test1 -> file1
-rw-r--r-- 1 root root  0 Jul 26 13:07 test.c

改变链接目录源目录也会改变,同样改变源目录链接目录也会跟着改变

3. 删除软链接

文件直接把链接文件删除旧好了
如果是目录就使用unlink 目录名

[root@lamp tmp]# ll -i
total 0
 1655188 drwxr-xr-x 3 root root 18 Jul 26 12:52 dir
52382236 -rw-r--r-- 1 root root  0 Jul 26 12:51 file2
52382237 -rw-r--r-- 1 root root  0 Jul 26 12:51 file3
52382238 -rw-r--r-- 1 root root  0 Jul 26 12:51 file4
52382239 lrwxrwxrwx 1 root root  5 Jul 26 12:59 test1 -> file1
52382224 -rw-r--r-- 1 root root  0 Jul 26 13:07 test.c
[root@lamp tmp]# ln file2 test2
[root@lamp tmp]# ll -i
total 0
 1655188 drwxr-xr-x 3 root root 18 Jul 26 12:52 dir
52382236 -rw-r--r-- 2 root root  0 Jul 26 12:51 file2
52382237 -rw-r--r-- 1 root root  0 Jul 26 12:51 file3
52382238 -rw-r--r-- 1 root root  0 Jul 26 12:51 file4
52382239 lrwxrwxrwx 1 root root  5 Jul 26 12:59 test1 -> file1
52382236 -rw-r--r-- 2 root root  0 Jul 26 12:51 test2
52382224 -rw-r--r-- 1 root root  0 Jul 26 13:07 test.c

二、硬链接

1. 硬链接特点

  1. 文件内容相同
  2. 一个文件改变另一文件也跟着改变
  3. 删除一个文件不会影响其它硬链接
  4. inode号相同
  5. Linux中,不能直接对目录创建硬链接
    在这里插入图片描述

2. 硬链接的创建

语法:ln [源文件] [链接文件名]

[root@lamp tmp]# ll -i
total 0
 1655188 drwxr-xr-x 3 root root 18 Jul 26 12:52 dir
52382236 -rw-r--r-- 1 root root  0 Jul 26 12:51 file2
52382237 -rw-r--r-- 1 root root  0 Jul 26 12:51 file3
52382238 -rw-r--r-- 1 root root  0 Jul 26 12:51 file4
52382239 lrwxrwxrwx 1 root root  5 Jul 26 12:59 test1 -> file1
52382224 -rw-r--r-- 1 root root  0 Jul 26 13:07 test.c
[root@lamp tmp]# ln file2 test2
[root@lamp tmp]# ll -i
total 0
 1655188 drwxr-xr-x 3 root root 18 Jul 26 12:52 dir
52382236 -rw-r--r-- 2 root root  0 Jul 26 12:51 file2
52382237 -rw-r--r-- 1 root root  0 Jul 26 12:51 file3
52382238 -rw-r--r-- 1 root root  0 Jul 26 12:51 file4
52382239 lrwxrwxrwx 1 root root  5 Jul 26 12:59 test1 -> file1
52382236 -rw-r--r-- 2 root root  0 Jul 26 12:51 test2
52382224 -rw-r--r-- 1 root root  0 Jul 26 13:07 test.c

发现链接文件和源文件是的inode是相同的

在这里插入图片描述

3. 删除

发现删除源文件并不影响硬链接文件

[root@lamp tmp]# ll
total 0
drwxr-xr-x 3 root root 18 Jul 26 12:52 dir
-rw-r--r-- 2 root root  0 Jul 26 12:51 file2
-rw-r--r-- 1 root root  0 Jul 26 12:51 file3
-rw-r--r-- 1 root root  0 Jul 26 12:51 file4
-rw-r--r-- 2 root root  0 Jul 26 12:51 test2
-rw-r--r-- 1 root root  0 Jul 26 13:07 test.c
[root@lamp tmp]# echo "hello word" >> test2
[root@lamp tmp]# cat file2
hello word
[root@lamp tmp]# rm -f file2
[root@lamp tmp]# cat test2
hello word

3. 特殊硬链接

目录一般不能直接创建硬链接的
但系统中有一些特殊的连接,不是我们自己创建的.
我们说自己创建的硬链接是删除不影响的,但这是一定不能删除的

[root@lamp ~]# ll -id /etc/init.d/
50332840 drwxr-xr-x. 2 root root 83 Jul 24 15:17 /etc/init.d/
[root@lamp ~]# ll -id /etc/rc.d/init.d
50332840 drwxr-xr-x. 2 root root 83 Jul 24 15:17 /etc/rc.d/init.d

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱敲代码的三毛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值