linux 硬链接与软连接

什么是硬链接,什么是软连接
 
硬链接特征:
    硬链接是有着相同inode号仅文件名不同的文件,因此硬链接存在以下几点特性
        *文件有相同的inode及data block
        *只能对以存在的文件进行创建
        *不能交叉文件系统进行硬链接的创建
        *不能对目录进行创建,只能对文件进行创建
        *删除一个硬链接文件不影响其他相同inode 号的文件
   测试
        1.    有相同的inode及 data block
    
        [root@misw12c oracle] # link test.dat test.dat.link    ##创建硬链接
            [root@misw12c oracle] # stat test.dat                    ##查看源文件
              File : ` test.dat '
              Size: 6               Blocks: 8          IO Block: 4096   regular file
            Device: 803h/2051d      Inode: 12          Links: 2
            Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
            Access: 2013-09-09 22:28:55.959255998 +0800
            Modify: 2013-09-09 23:43:41.834592083 +0800
            Change: 2013-09-09 23:44:43.259595001 +0800
            [root@misw12c oracle]# stat test.dat.link             ##查看硬链接
              File: `test.dat.link'
              Size : 6               Blocks : 8          IO Block : 4096   regular file
            Device : 803h / 2051d      Inode : 12          Links : 2
            Access : ( 0644 / -rw -r --r --)  Uid : (     0 /    root)   Gid : (     0 /    root)
            Access : 2013 - 09 - 09 22 : 28 : 55. 959255998 + 0800
            Modify : 2013 - 09 - 09 23 : 43 : 41. 834592083 + 0800
            Change : 2013 - 09 - 09 23 : 44 : 43. 259595001 + 0800
            [root@misw12c oracle] #
            [root@misw12c oracle] # echo ''>test.dat      ##清空表
            [root@misw12c oracle] # stat test.dat           ##源文件
              File : ` test.dat '
              Size: 1               Blocks: 8          IO Block: 4096   regular file
            Device: 803h/2051d      Inode: 12          Links: 2
            Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
            Access: 2013-09-09 22:28:55.959255998 +0800
            Modify: 2013-09-09 23:45:39.880602457 +0800
            Change: 2013-09-09 23:45:39.880602457 +0800
            [root@misw12c oracle]# stat test.dat.link    ##硬链接文件
              File: `test.dat.link'
              Size : 1               Blocks : 8          IO Block : 4096   regular file
            Device : 803h / 2051d      Inode : 12          Links : 2
            Access : ( 0644 / -rw -r --r --)  Uid : (     0 /    root)   Gid : (     0 /    root)
            Access : 2013 - 09 - 09 22 : 28 : 55. 959255998 + 0800
            Modify : 2013 - 09 - 09 23 : 45 : 39. 880602457 + 0800
            Change : 2013 - 09 - 09 23 : 45 : 39. 880602457 + 0800
        2.只能对以存在的文件进行创建
            [root@misw12c oracle] # link test.d test.da
            link : cannot create link ` test.da ' to `test.d' : No such file or directory
        3.不能交叉文件系统进行硬链接的创建
            [root@misw12c oracle] # link test.dat /tmp/test.dat
            link : cannot create link ` /tmp / test.dat ' to `test.dat' : Invalid cross -device link
        4.不能对目录进行创建,只能对文件进行创建
            [root@misw12c oracle] # file product
            product : directory
            [root@misw12c oracle] # link product /tmp/product
            link : cannot create link ` /tmp /product ' to `product' : Invalid cross -device link
        5.删除一个硬链接文件不影响其他相同inode 号的文件
            [root@misw12c oracle] # file test.dat.link
             test.dat.link : ASCII text
            [root@misw12c oracle] # stat test.dat.link
              File : ` test.dat.link '
              Size: 16              Blocks: 8          IO Block: 4096   regular file
            Device: 803h/2051d      Inode: 12          Links: 2
            Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
            Access: 2013-09-09 23:51:20.864610649 +0800
            Modify: 2013-09-09 23:51:11.936609085 +0800
            Change: 2013-09-09 23:51:11.936609085 +0800
            [root@misw12c oracle]# stat test.dat
              File: `test.dat'
              Size : 16              Blocks : 8          IO Block : 4096   regular file
            Device : 803h / 2051d      Inode : 12          Links : 2
            Access : ( 0644 / -rw -r --r --)  Uid : (     0 /    root)   Gid : (     0 /    root)
            Access : 2013 - 09 - 09 23 : 51 : 20. 864610649 + 0800
            Modify : 2013 - 09 - 09 23 : 51 : 11. 936609085 + 0800
            Change : 2013 - 09 - 09 23 : 51 : 11. 936609085 + 0800
            [root@misw12c oracle] # cat test.dat.link
            aaaaaaaaaaaaaaa
            [root@misw12c oracle] # cat test.dat
            aaaaaaaaaaaaaaa
            [root@misw12c oracle] # rm -f test.dat.link
            [root@misw12c oracle] # cat test.dat
            aaaaaaaaaaaaaaa
            [root@misw12c oracle] #
软连接特征:
    软连接数据块中存放的内容是另一个文件路径名的指向,软连接有自己的inode号以及数据块
        *软连接有自己的文件属性及权限等;
        *可对不存在的文件和目录创建软连接
        *软连接可交叉文件系统
        *软连接可对文件或目录创建
        *创建软连接时,链接计数i_nlink 不会增加;
        *删除软连接不影响被指向的文件,但若被指向的原文件被删除,则该软连接为死链接
    测试
        1.软连接有自己的文件属性及权限等(ps:经过测试软连接虽然有自己的权限,但无法变更权限,而源文件变更权限不对软连接造成影响)
            [root@misw12c oracle] # ls -al | grep sort_link
             -rw -r --r --.   1 root   root          8 Sep 10 00 : 10 sort_link
            [root@misw12c oracle] # ls -s sort_link sort_link.link
             ls : cannot access sort_link.link : No such file or directory
             4 sort_link
            [root@misw12c oracle] # ln -s sort_link sort_link.link
            [root@misw12c oracle] # ls -al | grep sort
             -rw -r --r --.   1 root   root          8 Sep 10 00 : 10 sort_link
            lrwxrwxrwx.   1 root   root          9 Sep 10 00 : 10 sort_link.link - > sort_link
            [root@misw12c oracle] # chmod 664 sort_link
            [root@misw12c oracle] # ls -l | grep sort
             -rw -rw -r --.   1 root   root          8 Sep 10 00 : 10 sort_link
            lrwxrwxrwx.   1 root   root          9 Sep 10 00 : 10 sort_link.link - > sort_link
        2.可对不存在的文件和目录创建软连接
            [root@misw12c oracle] # ls -al                        ##查看目录
            total 40
            drwxrwxr -x.   6 oracle oinstall   4096 Sep 10 00 : 10 .
            dr -xr -xr -x. 28 root   root       4096 Sep   9 22 : 36 ..
            drwxrwxr -x.   2 oracle oinstall 16384 Aug 16 21 : 34 lost +found
            drwxrwx -- -.   5 oracle oinstall   4096 Aug 17 22 : 03 oraInventory
            drwxrwxr -x. 10 oracle oinstall   4096 Aug 17 22 : 08 product
             -rw --w --w -.   1 root   root          8 Sep 10 00 : 10 sort_link
            lrwxrwxrwx.   1 root   root          9 Sep 10 00 : 10 sort_link.link - > sort_link
            drwxrwxr -x.   6 oracle oinstall   4096 Aug 22 15 : 54 src
            [root@misw12c oracle] # ln -s not_found not.link     ##创建不存在的文件软连接
            [root@misw12c oracle] # ls -al | grep not            ##创建成功
            lrwxrwxrwx.   1 root   root          9 Sep 10 00 : 18 not.link - > not_found
            [root@misw12c oracle] # cat not.link                 ##无法读取文件
             cat : not.link : No such file or directory
            [root@misw12c oracle] # stat not.link                ##使用stat 可以查看文件属性
              File : ` not.link ' -> `not_found'
              Size : 9               Blocks : 0          IO Block : 4096   symbolic link
            Device : 803h / 2051d      Inode : 14          Links : 1
            Access : ( 0777 /lrwxrwxrwx)  Uid : (     0 /    root)   Gid : (     0 /    root)
            Access : 2013 - 09 - 10 00 : 18 : 22. 293675539 + 0800
            Modify : 2013 - 09 - 10 00 : 18 : 19. 142679586 + 0800
            Change : 2013 - 09 - 10 00 : 18 : 19. 142679586 + 0800
        3.软连接可交叉文件系统
            [root@misw12c oracle] # pwd
             /oracle
            [root@misw12c oracle] # ln -s sort_link /tmp/sort_link.link
            [root@misw12c oracle] # ls -l /tmp | grep sort
            lrwxrwxrwx. 1 root root      9 Sep 10 00 : 20 sort_link.link - > sort_link
            [root@misw12c oracle] # ls -l | grep sort
             -rw --w --w -.   1 root   root          8 Sep 10 00 : 10 sort_link
            lrwxrwxrwx.   1 root   root          9 Sep 10 00 : 10 sort_link.link - > sort_link
        4.软连接可对文件或目录创建
            [root@misw12c oracle] # file product
            product : directory
            [root@misw12c oracle] # file sort_link
            sort_link : ASCII text
            [root@misw12c oracle] # ln -s product /tmp/product.link
            [root@misw12c oracle] # ls -l /tmp | grep product
            lrwxrwxrwx. 1 root root      7 Sep 10 00 : 21 product.link - > product
        5.创建软连接时,链接计数i_nlink 不会增加;
            [root@misw12c oracle] # echo 'aaaa'>link_s            #创建文件
            [root@misw12c oracle] # stat link_s
              File : `link_s '
              Size: 5               Blocks: 8          IO Block: 4096   regular file
            Device: 803h/2051d      Inode: 12          Links: 1
            Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
            Access: 2013-09-10 00:24:06.530732057 +0800
            Modify: 2013-09-10 00:24:06.530732057 +0800
            Change: 2013-09-10 00:24:06.530732057 +0800
            [root@misw12c oracle]# ln -s link_s link_s.link      ##创建软连接
            [root@misw12c oracle]# stat link_s                   ##查看(links 为1  ,证明软连接不增加links数)
              File: `link_s'
              Size : 5               Blocks : 8          IO Block : 4096   regular file
            Device : 803h / 2051d      Inode : 12          Links : 1
            Access : ( 0644 / -rw -r --r --)  Uid : (     0 /    root)   Gid : (     0 /    root)
            Access : 2013 - 09 - 10 00 : 24 : 06. 530732057 + 0800
            Modify : 2013 - 09 - 10 00 : 24 : 06. 530732057 + 0800
            Change : 2013 - 09 - 10 00 : 24 : 06. 530732057 + 0800
            [root@misw12c oracle] # link link_s link_k            ##创建硬链接
            [root@misw12c oracle] # stat link_s                     ##查看(links 为2 ,说明硬链接增加links 数)
              File : `link_s '
              Size: 5               Blocks: 8          IO Block: 4096   regular file
            Device: 803h/2051d      Inode: 12          Links: 2
            Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
            Access: 2013-09-10 00:24:06.530732057 +0800
            Modify: 2013-09-10 00:24:06.530732057 +0800
            Change: 2013-09-10 00:24:39.847681150 +0800
        6.删除软连接不影响被指向的文件,但若被指向的原文件被删除,则该软连接为死链接
            [root@misw12c oracle] # ls -al | grep link
            lrwxrwxrwx.   1 root   root          6 Sep 10 00 : 26 link_a - > link_s
             -rw -r --r --.   1 root   root          5 Sep 10 00 : 24 link_s
            [root@misw12c oracle] # stat link_a         ##文件属性
              File : `link_a ' -> `link_s'
              Size : 6               Blocks : 0          IO Block : 4096   symbolic link
            Device : 803h / 2051d      Inode : 14          Links : 1
            Access : ( 0777 /lrwxrwxrwx)  Uid : (     0 /    root)   Gid : (     0 /    root)
            Access : 2013 - 09 - 10 00 : 27 : 20. 718675016 + 0800
            Modify : 2013 - 09 - 10 00 : 26 : 58. 327686892 + 0800
            Change : 2013 - 09 - 10 00 : 26 : 58. 327686892 + 0800
            [root@misw12c oracle] # stat link_s         ##软连接文件属性
              File : `link_s '
              Size: 5               Blocks: 8          IO Block: 4096   regular file
            Device: 803h/2051d      Inode: 12          Links: 1
            Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
            Access: 2013-09-10 00:24:06.530732057 +0800
            Modify: 2013-09-10 00:24:06.530732057 +0800
            Change: 2013-09-10 00:27:05.371709433 +0800
            [root@misw12c oracle]# cat link_a           
            aaaa
            [root@misw12c oracle]# cat link_s
            aaaa
            [root@misw12c oracle]# rm -f link_s         ##删除源文件
            [root@misw12c oracle]# ls -l | grep link    ##软连接还存在但已经是死链接
            lrwxrwxrwx.  1 root   root         6 Sep 10 00:26 link_a -> link_s
            [root@misw12c oracle]# cat link_a           ##cat软连接无法读取
            cat: link_a: No such file or directory
            [root@misw12c oracle]# echo 'bbbbb '>link_s   ##重新创建link_s 源文件
            [root@misw12c oracle]# cat link_a            ##文件内容改变
            bbbbb
            [root@misw12c oracle]#
 
使用find 来查找软连接与硬链接
    1.查找路径在/home下的文件data.txt 的软连接
        find /home -lname data.txt
    2.查找路径 /home 有相同inode 的所有硬链接
        find /home -samefile /home /old.file
    3.列出/home下所有软连接文件
        find /home -type l - ls
查看系统的inode值
    [root@misw12c oracle] # dumpe2fs -h /dev/sda1
    dumpe2fs 1. 41. 12 ( 17 -May - 2010)
    Filesystem volume name :    <none >
    Last mounted on :           /boot
    Filesystem UUID :          f81f7297 - 1b91 - 4f15 -acaa - 5668e276883a
    Filesystem magic number :  0xEF53
    Filesystem revision #:    1 (dynamic)
    Filesystem features :      has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize
    Filesystem flags :         signed_directory_hash
    Default mount options :    user_xattr acl
    Filesystem state :         clean
    Errors behavior :          Continue
    Filesystem OS type :       Linux
    Inode count :               51200
    Block count :               204800
    Reserved block count :      10240
    Free blocks :               147096
    Free inodes :               51156
    First block :               1
    Block size :                1024
    Fragment size :             1024
    Reserved GDT blocks :       256
    Blocks per group :          8192
    Fragments per group :       8192
    Inodes per group :          2048
    Inode blocks per group :    256
    Flex block group size :     16
    Filesystem created :       Fri Aug 16 21 : 34 : 07 2013
    Last mount time :          Mon Sep   9 22 : 36 : 55 2013
    Last write time :          Mon Sep   9 22 : 36 : 55 2013
    Mount count :               7
    Maximum mount count :       - 1
    Last checked :             Fri Aug 16 21 : 34 : 07 2013
    Check interval :            0 ( <none >)
    Lifetime writes :           55 MB
    Reserved blocks uid :       0 (user root)
    Reserved blocks gid :       0 (group root)
    First inode :               11
    Inode size :                128
    Journal inode :             8
    Default directory hash :   half_md4
    Directory Hash Seed :      f0e50be4 - 5870 - 404f - 886e - 8303a0e5b807
    Journal backup :           inode blocks
    Journal features :         (none)
    Journal size :              4096k
    Journal length :            4096
    Journal sequence :         0x0000002e
    Journal start :             0
总结:
    相关命令:
        ln,link,stat,file,find

转载于:https://my.oschina.net/miswdba/blog/160243

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值