当前位置:首页 > 编程笔记 > 正文
已解决

linux软链接和硬链接

来自网友在路上 140840提问 提问时间:2023-11-09 19:34:11阅读次数: 40

最佳答案 问答题库408位专家为你答疑解惑

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.硬链接文件大小和源文件大小相同,软链接大小为指向源文件指针大小。

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"linux软链接和硬链接":http://eshow365.cn/6-36461-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!