Linux之文件链接 ln

一、软连接
1.简介
软链接/symbolic link又称符号链接.
2.创建软链接的命令
ln -s 文件名 软链接文件名
ln link
-s symbolic
3.示例
(1)创建一个文件,并输入内容并查看文件内容。
[root@localhost ~]# echo 333333 > /file3
[root@localhost ~]# cat /file3
333333
(2)创建软链接。
[root@localhost ~]# ln -s /file3 /home/file33
[root@localhost ~]# ln -s /file3 /home/file44
(3)查看软链接文件内容,与源文件内容一致。
[root@localhost ~]# cat /home/file33
333333
[root@localhost ~]# cat /home/file44
333333
(4)观察软链接文件,inode与源文件不一样。
[root@localhost ~]# ll -i /file3 /home/file33 /home/file44
15016 -rw-r–r--. 1 root root 7 12月 6 18:23 /file3
51753096 lrwxrwxrwx. 1 root root 6 12月 6 18:24 /home/file33 -> /file3
51753098 lrwxrwxrwx. 1 root root 6 12月 6 18:24 /home/file44 -> /file3
(5) 删除源文件,软链接闪烁,不可用。
[root@localhost ~]# rm -rf /file3
[root@localhost ~]# cat /home/file33
cat: /home/file33: 没有那个文件或目录
[root@localhost ~]# cat /home/file44
cat: /home/file44: 没有那个文件或目录
4.总结
(1)软连接记录的只是源文件的绝对路径。
(2)软连接像快捷方式,可以对文件和目录做软链接;
(3)软连接失去源文件不可用;
(4)软链接inode与源文件不同,相当于重新创建了一个文件。
二、硬链接
1、简介
硬链接命令:
ln 源文件名 链接文件名
2、示例
(1)创建一个文件,并输入内容并查看文件内容。
[root@localhost ~]# echo 222 > /file2
[root@localhost ~]# cat /file2
222
(2)创建硬链接
[root@localhost ~]# ln /file2 /file222
[root@localhost ~]# ln /file2 /home/file22
(3)查看软链接文件内容,与源文件内容一致。
[root@localhost ~]# cat /file222
222
[root@localhost ~]# cat /home/file22
222
(4)观察硬链接文件,inode与源文件相同。
[root@localhost ~]# ls -i /file2 /file222 /home/file22
77462 /file2 77462 /file222 77462 /home/file22
[root@localhost ~]# ll -i /file2 /file222 /home/file22
77462 -rw-r–r--. 3 root root 4 12月 6 18:38 /file2
77462 -rw-r–r--. 3 root root 4 12月 6 18:38 /file222
77462 -rw-r–r--. 3 root root 4 12月 6 18:38 /home/file22
3 指硬链接加源文件的总数。
(5) 删除源文件,硬链接仍然可以用。
[root@localhost ~]# rm -rf /file2
[root@localhost ~]# cat /file222
222
[root@localhost ~]# cat /home/file22
222
3、总结
(1)硬链接只能在同分区做。
[root@localhost ~]# ln /file2 /mnt/sdk1/file2-h1
ln: 无法创建硬链接"/mnt/sdk1/file2-h1" => “/file2”: 无效的跨设备连接
(2)硬链接只能针对文件做,不能对目录做。
[root@localhost ~]# ln /home /file0
ln: “/home”: 不允许将硬链接指向目录
(3)硬链接删除源文件,依然可以用;
(4)硬链接与源文件的inode相同,相当于给文件新加一个文件名。文件备份作用,不占空间。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值