• 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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    二、软链接

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    改变连接文件和源文件

    [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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

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

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    修改链接目录和源目录

    [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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

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

    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    二、硬链接

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    发现链接文件和源文件是的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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    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
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    【Unity入门计划】制作RubyAdventure03-使用碰撞体&触发器实现世界交互
    性能优化之分页查询 | StartDT Tech Lab 12
    Vue学习——目录结构与运行流程(20)
    学生学徒作品分享——金融大模型-房屋租金价格影响因素分析与预测
    【云原生 | Kubernetes 系列】---Prometheus 联邦
    Linux服务器——进程/线程池
    C# 移除链表元素
    孤僻型性格分析,如何改变孤僻型性格?
    带你掌握如何查看并读懂昇腾平台的应用日志
    OpenCV-Python实战(2) —— 使用OpenCV的绘图功能创建OpenCV的徽标
  • 原文地址:https://blog.csdn.net/weixin_53946852/article/details/125992434