linux软链接和硬链接

1.硬链接(hard link)

每个文件的磁盘存储位置都有一个指针指向他这个指针称为inode,每创建一个硬链接都是指向这个inode指针,而不是指向这个文件的物理磁盘位置。

 

 当有多个硬链接指向同一个inode,删除其中一个硬链接文件,他的物理文件其实还没被删除,其他的硬链接文件依然有效,要把它所要的硬链接文件删除才会真正删除其物理文件。

 

创建硬链接

ln 被连接文件 链接文件 

[root@VM-12-12-centos wz]# touch hardlink.txt
[root@VM-12-12-centos wz]# ls
hardlink.txt
[root@VM-12-12-centos wz]# ls -li
total 0
799743 -rw-r--r-- 1 root root 0 Nov  9 16:20 hardlink.txt
[root@VM-12-12-centos wz]# ln hardlink.txt hardlink1.txt 
[root@VM-12-12-centos wz]# ls -li
total 0
799743 -rw-r--r-- 2 root root 0 Nov  9 16:20 hardlink1.txt
799743 -rw-r--r-- 2 root root 0 Nov  9 16:20 hardlink.txt
[root@VM-12-12-centos wz]# echo 'i am hardlink1' > hardlink1.txt
[root@VM-12-12-centos wz]# cat hardlink.txt
i am hardlink1
[root@VM-12-12-centos wz]# rm hardlink.txt
rm: remove regular file ‘hardlink.txt’? y
[root@VM-12-12-centos wz]# ls -li
total 4
799743 -rw-r--r-- 1 root root 15 Nov  9 16:36 hardlink1.txt
[root@VM-12-12-centos wz]# rm hardlink1.txt
rm: remove regular file ‘hardlink1.txt’? y
[root@VM-12-12-centos wz]# ls -li
total 0
[root@VM-12-12-centos wz]# 

 

2.软链接(symbolic link)

软链接相当于Windows系统的快捷方式,删除软链接不会对源文件有影响。删除源文件软链接会失效。软链接相当于是指向硬链接的指针。

创建软链接

ln -s 被连接文件 链接文件

[root@VM-12-12-centos wz]# touch hardlink.txt
[root@VM-12-12-centos wz]# ln -s hardlink.txt sysboliclink.txt
[root@VM-12-12-centos wz]# ls -li
total 0
799742 -rw-r--r-- 1 root root  0 Nov  9 16:46 hardlink.txt
799743 lrwxrwxrwx 1 root root 12 Nov  9 16:47 sysboliclink.txt -> hardlink.txt
[root@VM-12-12-centos wz]# echo 'i am sysbolio link' > sysboliclink.txt
[root@VM-12-12-centos wz]# cat hardlink.txt
i am sysbolio link
[root@VM-12-12-centos wz]# rm hardlink.txt
rm: remove regular file ‘hardlink.txt’? y
[root@VM-12-12-centos wz]# ls -li
total 0
799743 lrwxrwxrwx 1 root root 12 Nov  9 16:47 sysboliclink.txt -> hardlink.txt
[root@VM-12-12-centos wz]# cat sysboliclink.txt
cat: sysboliclink.txt: No such file or directory
[root@VM-12-12-centos wz]#

 

 

3.软链接和硬链接的区别

1.软链接可以作用在目录,而硬链接只能作用在文件

[root@VM-12-12-centos wz]# mkdir dirs
[root@VM-12-12-centos wz]# ls
dirs
[root@VM-12-12-centos wz]# ls -ls
total 4
4 drwxr-xr-x 2 root root 4096 Nov  9 16:52 dirs
[root@VM-12-12-centos wz]# ln -s dirs/ dirsnew
[root@VM-12-12-centos wz]# ls -li
total 4
918429 drwxr-xr-x 2 root root 4096 Nov  9 16:52 dirs
799742 lrwxrwxrwx 1 root root    5 Nov  9 17:25 dirsnew -> dirs/
[root@VM-12-12-centos wz]# mkdir dirsnew/hello
[root@VM-12-12-centos wz]# ls dirsnew/
hello
[root@VM-12-12-centos wz]# ls dirs/
hello
[root@VM-12-12-centos wz]# ln dirs hsrddir
ln: ‘dirs’: hard link not allowed for directory
[root@VM-12-12-centos wz]# 

 

2. 硬链接只能在同一文件系统创建,软链接能在不同文件系统创建。

3.硬链接文件大小和源文件大小相同,软链接大小为指向源文件指针大小。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值