• linux-硬链接与软连接


    什么是硬链接? 什么是软连接?  有时候我们经常把硬链接和软连接搞混了

    1.  硬链接

      硬链接虽然看起来和cp 差不多但是还是不一样的,cp 的文件inode 号不是一样的,且修改文件另一个不会一起变化的

    • 源文件和硬链接文件拥有相同的Inode和Block      
    • 修改任意一个文件,另一个都改变
    • 删除任意一个文件,另一个都能使用
    • 硬链接不能链接目录
    • 硬链接不能跨分区
     
    演示:
    新建一个文件

    [root@localhost ~]# vim test
    查看新建文件内容
    [root@localhost ~]# cat test
    hello 123 234

    生产硬链接,将test 文件硬链接到test1 
    [root@localhost ~]# ln test test1

    检查文件的inode 号是一样的
    [root@localhost ~]# ls -li
    total 12
    33574979 -rw-------. 1 root root 1418 Feb 8 06:43 anaconda-ks.cfg
    34190349 -rw-r--r-- 2 root root 15 Feb 24 01:22 test
    34190349 -rw-r--r-- 2 root root 15 Feb 24 01:22 test1

    我们检查test1 文件的内容是和我们源文件内容一样
    [root@localhost ~]# cat test1
    hello 123 234

    修改硬链接文件 
    [root@localhost ~]# vim test1
    查看修改的内容
    [root@localhost ~]# cat test1
    hello 123 234 456

    检查源文件,发现源文件会随的硬链接文件的改变而改变
    [root@localhost ~]# cat test
    hello 123 234 456
    [root@localhost ~]#
    删除硬链接  不会影响到源文件
    [root@localhost ~]# rm -rf test1
    [root@localhost ~]# ls -li
    total 8
    33574979 -rw-------. 1 root root 1418 Feb 8 06:43 anaconda-ks.cfg

    34190349 -rw-r--r-- 1 root root 19 Feb 24 01:23 test

    给源文件生产一个硬链接

    [root@localhost ~]# cat test
    hello 123 234 456
    [root@localhost ~]# ln test test1
    [root@localhost ~]# ls -li
    total 12
    33574979 -rw-------. 1 root root 1418 Feb 8 06:43 anaconda-ks.cfg
    34190349 -rw-r--r-- 2 root root 19 Feb 24 01:23 test
    34190349 -rw-r--r-- 2 root root 19 Feb 24 01:23 test1

    删除源文件,硬链接文件没有影响
    [root@localhost ~]# rm -rf test
    [root@localhost ~]#
    [root@localhost ~]# ls -li
    total 8
    33574979 -rw-------. 1 root root 1418 Feb 8 06:43 anaconda-ks.cfg
    34190349 -rw-r--r-- 1 root root 19 Feb 24 01:23 test1
    [root@localhost ~]#

    将硬链接cp 到其他分区报错,硬链接不能跨分区

    [root@localhost ~]# ln test /mnt/test2
    ln: failed to create hard link ‘/mnt/test2’ => ‘test’: Invalid cross-device link
    [root@localhost ~]#
    [root@localhost ~]# df -TH
    Filesystem Type Size Used Avail Use% Mounted on
    devtmpfs devtmpfs 942M 0 942M 0% /dev
    tmpfs tmpfs 954M 0 954M 0% /dev/shm
    tmpfs tmpfs 954M 11M 944M 2% /run
    tmpfs tmpfs 954M 0 954M 0% /sys/fs/cgroup
    /dev/mapper/centos-root xfs 19G 4.1G 15G 23% /
    /dev/sdb1 ext4 22G 47M 20G 1% /mnt
    /dev/sda1 xfs 1.1G 182M 882M 18% /boot
    tmpfs tmpfs 191M 0 191M 0% /run/user/0

    创建一个目录,尝试硬链接一个目录,硬链接失败,硬链接不允许链接目录

    [root@localhost ~]# mkdir test-dir
    [root@localhost ~]# mv test test-dir/
    [root@localhost ~]# ln test-dir/ test-dir-123
    ln: ‘test-dir/’: hard link not allowed for directory

     
     
     
     
    2. 软连接
    • 软链接和源文件拥有不同的Inode和Block
    • 两个文件修改任意一个,另一个都改变
    • 删除软链接,源文件不受影响;删除源文件,软链接不能使用
    • 软链接没有实际数据,只保存源文件的Inode,不论源文件多大,软链接大小不变
    • 软链接的权限时最大权限,但由于没有实际数据,最终访问时需参考源文件权限
    • 软链接可以链接目录
    • 软链接可以跨分区
    演示:
    创建test文件

    [root@localhost test-dir]# ls -li
    total 4
    33575375 -rw-r--r-- 1 root root 4 Feb 24 01:24 test
    生产软连接
    [root@localhost test-dir]# ln -s test test1

    检查inode 号是不一样的

    [root@localhost test-dir]# ls -li
    total 4
    33575375 -rw-r--r-- 1 root root 4 Feb 24 01:24 test
    33575378 lrwxrwxrwx 1 root root 4 Feb 24 01:53 test1 -> test

    查看软链接文件的文件内容
    [root@localhost test-dir]# cat test1
    123
    给软连接文件追加数据
    [root@localhost test-dir]# echo 456 >> test1
    [root@localhost test-dir]#
    [root@localhost test-dir]# cat test1
    123
    456

    检查源文件 ,源文件随着软链接文件的修改而变化
    [root@localhost test-dir]# cat test
    123
    456
    删除软链接
    [root@localhost test-dir]# rm -rf test1
    检查软连接的删除不会影响到源文件
    [root@localhost test-dir]# ls -li
    total 4
    33575375 -rw-r--r-- 1 root root 8 Feb 24 01:54 test
    [
    [root@localhost test-dir]# cat test
    123
    456
    生产软连接
    [root@localhost test-dir]# ln -s test test1
    [root@localhost test-dir]# ls -li
    total 4
    33575375 -rw-r--r-- 1 root root 8 Feb 24 01:54 test
    33575376 lrwxrwxrwx 1 root root 4 Feb 24 01:55 test1 -> test
    删除源文件发现软连接文件也会随之源链接的删除而消失
    [root@localhost test-dir]# rm -rf test
    [root@localhost test-dir]#
    [root@localhost test-dir]# ls -li
    total 0
    33575376 lrwxrwxrwx 1 root root 4 Feb 24 01:55 test1 -> test
    [root@localhost test-dir]# cat test1
    cat: test1: No such file or directory

    dd 创建一个大文件
    [root@localhost test-dir]# dd if=/dev/zero of=test-big bs=100M count=2
    2+0 records in
    2+0 records out
    209715200 bytes (210 MB) copied, 5.09727 s, 41.1 MB/s
    [root@localhost test-dir]# du -sh *
    0 test1
    200M test-big
    [root@localhost test-dir]#
    [root@localhost test-dir]# ls -li
    total 204800
    33575376 lrwxrwxrwx 1 root root 4 Feb 24 01:55 test1 -> test
    33575375 -rw-r--r-- 1 root root 209715200 Feb 24 01:57 test-big

    软链接源文件
    [root@localhost test-dir]# ln -s test-big test-big-1
    查看不论源文件多大,软链接大小不变
    [root@localhost test-dir]# ls -li
    total 204800
    33575376 lrwxrwxrwx 1 root root 4 Feb 24 01:55 test1 -> test
    33575375 -rw-r--r-- 1 root root 209715200 Feb 24 01:57 test-big
    33575378 lrwxrwxrwx 1 root root 8 Feb 24 01:57 test-big-1 -> test-big
    [root@localhost test-dir]# du -sh test-big-1
    0 test-big-1
    [root@localhost test-dir]#
    [root@localhost test-dir]# cd ..

    软连接目录成功
    [root@localhost ~]# ln -s test-dir/ test-dir-1

    [root@localhost ~]# ls -li
    total 1440260
    19250 drwxr-xr-x 2 root root 22 Feb 22 23:41 123
    33574979 -rw-------. 1 root root 1418 Feb 8 06:40 anaconda-ks.cfg
    34000838 -rw-r--r-- 1 root root 950009856 Feb 23 02:25 CentOS-7-x86_64-Minimal-1804.iso
    33575377 drwxr-xr-x 2 root root 53 Feb 24 01:57 test-dir
    33575379 lrwxrwxrwx 1 root root 9 Feb 24 01:58 test-dir-1 -> test-dir/
    34000836 -rw-r--r-- 1 root root 524298240 Feb 22 23:34 test.tar
    34000837 -rw-r--r-- 1 root root 508959 Feb 22 23:39 test.tar.gz

     
     
     
     
  • 相关阅读:
    IIS使用十大原则,(IIS过期时间,IIS缓存设置) 【转载】
    微软中国的相关研发团队 交流平台
    Excel文件减肥和加速
    二进制与字符串之间的转换
    调度模式·WorkerChannelRequest
    引用Hibernate的Java桌面程序打包方法
    [收藏]Open CASCADE
    需要天天读月月读年年读的书
    geotools的dotnet版本
    FC4 下ns2.28的安装 (zz)
  • 原文地址:https://www.cnblogs.com/yk0625/p/14442394.html
Copyright © 2020-2023  润新知