zoukankan      html  css  js  c++  java
  • linux系统中软链接和硬链接

    linux系统中软链接相当于windows系统中的快捷方式,实质为原始文件的所在的绝对路径

    硬链接相当于针对原始文件的存放位置创建了一个指针(linux就该这么学p132)

    硬链接相当于同一个文件的两个名字。

    1、准备测试数据

    [root@linuxprobe test]# seq 5 > a.txt
    [root@linuxprobe test]# cat a.txt
    1
    2
    3
    4
    5

    2、创建软链接和硬链接

    [root@linuxprobe test]# ln -s a.txt a.link  ## 软链接,注意l标志,见下图
    [root@linuxprobe test]# ln a.txt a.hard ## 硬链接,注意链接数
    [root@linuxprobe test]# ll
    total 8
    -rw-r--r--. 2 root root 10 Oct 25 23:01 a.hard
    lrwxrwxrwx. 1 root root  5 Oct 25 23:02 a.link -> a.txt
    -rw-r--r--. 2 root root 10 Oct 25 23:01 a.txt

    3、直接访问软链接和硬链接

    [root@linuxprobe test]# cat a.link
    1
    2
    3
    4
    5
    [root@linuxprobe test]# cat a.hard
    1
    2
    3
    4
    5

    删除原始文件后访问软链接和硬链接

    [root@linuxprobe test]# ls
    a.hard  a.link  a.txt
    [root@linuxprobe test]# rm -f a.txt
    [root@linuxprobe test]# cat a.hard  ## 硬链接不受影响(知道连接数为0才完全删除)
    1
    2
    3
    4
    5
    [root@linuxprobe test]# cat a.link  ## 软链接原始文件删除后不能访问
    cat: a.link: No such file or directory

    4、软链接和硬链接所占用的磁盘空间

    [root@linuxprobe test]# ls
    [root@linuxprobe test]# dd if=/dev/zero bs=10M count=1 of=a.txt
    1+0 records in
    1+0 records out
    10485760 bytes (10 MB) copied, 0.00722364 s, 1.5 GB/s
    [root@linuxprobe test]# ll -h
    total 10M
    -rw-r--r--. 1 root root 10M Oct 25 23:12 a.txt
    [root@linuxprobe test]# ln -s a.txt a.link
    [root@linuxprobe test]# ll -h  ## 软链接几乎不占用磁盘空间
    total 10M
    lrwxrwxrwx. 1 root root   5 Oct 25 23:12 a.link -> a.txt
    -rw-r--r--. 1 root root 10M Oct 25 23:12 a.txt
    [root@linuxprobe test]# ln a.txt a.hard
    [root@linuxprobe test]# ll -h  ## 硬链接占用和原始文件一样的磁盘空间
    total 20M
    -rw-r--r--. 2 root root 10M Oct 25 23:12 a.hard
    lrwxrwxrwx. 1 root root   5 Oct 25 23:12 a.link -> a.txt
    -rw-r--r--. 2 root root 10M Oct 25 23:12 a.txt

    5、硬链接的inode号是一样的

    [root@linuxprobe test]# ls -il
    total 20480
    102787161 -rw-r--r--. 2 root root 10485760 Oct 25 23:12 a.hard
    102787163 lrwxrwxrwx. 1 root root        5 Oct 25 23:12 a.link -> a.txt
    102787161 -rw-r--r--. 2 root root 10485760 Oct 25 23:12 a.txt

    6、硬链接节省空间?

    [root@linuxprobe test]# ls
    a.hard  a.link  a.txt
    [root@linuxprobe test]# ll -h
    total 20M
    -rw-r--r--. 2 root root 10M Oct 25 23:12 a.hard
    lrwxrwxrwx. 1 root root   5 Oct 25 23:12 a.link -> a.txt
    -rw-r--r--. 2 root root 10M Oct 25 23:12 a.txt
    [root@linuxprobe test]# du -sh ./
    10M     ./

    7、硬链接可以移动、软链接不可以移动

    [root@linuxprobe test]# ls
    [root@linuxprobe test]# seq 5 > a.txt
    [root@linuxprobe test]# ln a.txt a.hard
    [root@linuxprobe test]# ln -s a.txt a.link
    [root@linuxprobe test]# ll -h
    total 8.0K
    -rw-r--r--. 2 root root 10 Oct 25 23:29 a.hard
    lrwxrwxrwx. 1 root root  5 Oct 25 23:29 a.link -> a.txt
    -rw-r--r--. 2 root root 10 Oct 25 23:29 a.txt
    [root@linuxprobe test]# mkdir test
    [root@linuxprobe test]# mv a.hard a.link test/
    [root@linuxprobe test]# cd test/
    [root@linuxprobe test]# ls
    a.hard  a.link
    [root@linuxprobe test]# cat a.hard  ## 硬链接移动之后仍然可以查看
    1
    2
    3
    4
    5
    [root@linuxprobe test]# cat a.link  ##  软链接移动之后不能查看
    cat: a.link: No such file or directory

    8、原始文件重命名,硬链接不受影响,软链接失效

    [root@linuxprobe test]# ls
    [root@linuxprobe test]# seq 5 > a.txt
    [root@linuxprobe test]# cat a.txt
    1
    2
    3
    4
    5
    [root@linuxprobe test]# ln a.txt a.hard
    [root@linuxprobe test]# ln -s a.txt a.link
    [root@linuxprobe test]# cat a.hard
    1
    2
    3
    4
    5
    [root@linuxprobe test]# cat a.link
    1
    2
    3
    4
    5
    [root@linuxprobe test]# mv a.txt aaa.txt
    [root@linuxprobe test]# cat a.hard
    1
    2
    3
    4
    5
    [root@linuxprobe test]# cat a.link
    cat: a.link: No such file or directory
  • 相关阅读:
    leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
    leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String
    leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search
    leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String
    leetcode 162. Find Peak Element
    leetcode 88. Merge Sorted Array
    leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
    Android的API版本和名称对应关系
    spring 定时任务执行两次解决办法
    解析字符串为泛型的方法
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/13875916.html
Copyright © 2011-2022 走看看