欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

符号连接

程序员文章站 2022-06-01 19:47:21
...

符号连接


软链接可以跨分区,硬链接不可以跨分区,因为inode号码由分区指定

软链接 软链接如果源文件被删除掉,软链接文件就失效了

//创建一个文件

[[email protected] test]# touch file1		//创建一个文件

创建链接文件用ln -s 源文件 目标文件

[[email protected] test]# ln -s /test/file1  /test/f1   创建链接文件用ln -s 源文件   目标文件

编辑源文件内容,比如:heqkwjke qweqwe

[[email protected] test]# vi /test/file1		编辑源文件内容,比如:heqkwjke qweqwe

查看

[[email protected] test]# cat f1 				//查看
heqkwjke qweqwe

硬链接 硬链接文件相当于参数,链接数会变,可以通过ls查看链接数的变化,源文件删除可以使用替身文件,文件是一样

不加选项默认表示硬链接

[[email protected] test]# ln /test/file1 /test/l22    不加选项默认表示硬链接
具体讲解:

软链接 符号连接 相当于快捷方式
链接文件损坏对源文件没有任何影响,权限主要看对原文件的权限
软连接的两个文件的inode号不一样,表明是两个不同的文件
软连接只存一个存放源文件的路径
符号链接会有自己的文件inode
硬链接 几个文件同时对数据块有掌管权
不会占用额外的资源(只占用一点数据资源),他们的inode号一样

一、符号链接 symbolic link
[[email protected] ~]# echo 222 > /file222.txt
[[email protected] ~]# ln -s /file222.txt /tmp/s-file222.txt
[[email protected] ~]# ll -i /file222.txt /tmp/s-file222.txt
41 lrwxrwxrwx 1 root root 12 Jan 10 16:19 /tmp/s-file222.txt -> /file222.txt
18 -rw-r–r-- 1 root root 4 Jan 10 16:18 /file222.txt

二、硬链接 必须在同一个分区内部,因为不同分区内inode的划分不一样,可以用于防止误删除
[[email protected] ~]# echo 111 > /file111.txt
[[email protected] ~]# ln /file111.txt /etc
[[email protected] ~]# ln /file111.txt /etc/h-111.txt
[[email protected] ~]# ll -i /file111.txt /etc/file111.txt /etc/h-111.txt
17 -rw-r–r-- 3 root root 4 Jan 10 16:16 /etc/file111.txt 3表示所对应的数据块有几个指向的文件名
17 -rw-r–r-- 3 root root 4 Jan 10 16:16 /etc/h-111.txt
17 -rw-r–r-- 3 root root 4 Jan 10 16:16 /file111.txt

注:硬链接
1- 不能跨分区 :每一个分区的inode都有自己的管理机制,都是从2开始
2. 不支持目录做硬链接
[[email protected] home]# ln /home/ /mnt
ln: ‘/home’: hard link not allowed for directory
ln: “/home/”: 不允许将硬链接指向目录

删除目录软链时:
rm -rf /home/it1000/ 删除目录下的文件
rm -rf /home/it1000 仅删除链接文件本身