linux ln 软链接和硬链接

1 硬链接:

创建硬链接文件的时候,并没有分配一个新的文件,当删除一个硬链接文件的时候,仅仅删除链接文件本身,对原始文件并没有作用,而删除原始文件的时候也不会将物理文件删除,这就好像指针的作用,硬链接文件 和原始文件其实具有相同的地位,每增加一个硬链接文件,就会将文件链接数目加1,删除硬链接文件或者源文件时候,就会自动的减1

硬链接的缺点:
a 不用许给目录创建硬链接;
b 只有在同一文件系统中的文件之间才可以创建硬链接;


2 软链接:创建软链接文件的时候,会分配一个新的文件,可以创建一个不指向任何文件的断链文件;当删除软链接文件的时候,仅仅删除软链接文件,对原始文件没有影响,而删除原始文件的时候,就会产生断链的情况;
可以在不同的文件系统,不同机器,不同网络之间创建软链接文件;可以创建目录的软链接文件。


下面的例子:

[root@localhost tmp]# ls -li test.txt                    ---------------------------->创建了一个test.txt

883 -rw-r--r-- 1 root root 12  7月 16 11:04 test.txt   ---------------------->节点inode是883
[root@localhost tmp]#


[root@localhost tmp]# ln test.txt test_hard.txt ----------------------------->创建一个硬链接
[root@localhost tmp]# ls -li test*
883 -rw-r--r-- 2 root root 12  7月 16 11:04 test_hard.txt ----------------->硬链接的inode和源文件一样都是883
883 -rw-r--r-- 2 root root 12  7月 16 11:04 test.txt

[root@localhost tmp]# ln -s test.txt test_soft.txt --------------------------->创建一个软链接
[root@localhost tmp]#
[root@localhost tmp]# ls -li test*
883 -rw-r--r-- 2 root root 12  7月 16 11:04 test_hard.txt
884 lrwxrwxrwx 1 root root  8  7月 16 11:09 test_soft.txt -> test.txt ----->软链接的inode是884
883 -rw-r--r-- 2 root root 12  7月 16 11:04 test.txt


[root@localhost tmp]# ln test.txt test_hard1.txt  ----------------------->创建第二个硬链接
[root@localhost tmp]#
[root@localhost tmp]#
[root@localhost tmp]# ls -li test*
883 -rw-r--r-- 3 root root 12  7月 16 11:04 test_hard1.txt
883 -rw-r--r-- 3 root root 12  7月 16 11:04 test_hard.txt
884 lrwxrwxrwx 1 root root  8  7月 16 11:09 test_soft.txt -> test.txt
883 -rw-r--r-- 3 root root 12  7月 16 11:04 test.txt
[root@localhost tmp]# cat test_hard1.txt  ---------------------------------->查看硬链接文件的内容
dfafasfasdf
[root@localhost tmp]# cat test_soft.txt      ---------------------------------->查看软链接文件的内容
dfafasfasdf
[root@localhost tmp]# rm test.txt  ---------------------------------->删除源文件
rm:是否删除普通文件 "test.txt"?y
[root@localhost tmp]#
[root@localhost tmp]#
[root@localhost tmp]# ls -li test*
883 -rw-r--r-- 2 root root 12  7月 16 11:04 test_hard1.txt
883 -rw-r--r-- 2 root root 12  7月 16 11:04 test_hard.txt
884 lrwxrwxrwx 1 root root  8  7月 16 11:09 test_soft.txt -> test.txt ------->此时,在linux下,提示这个链接失效
[root@localhost tmp]# cat test_hard1.txt --------------------------------------->查看硬链接文件内容
dfafasfasdf
[root@localhost tmp]# cat test_soft.txt    --------------------------------------->查看软链接文件内容
cat: test_soft.txt: 没有那个文件或目录
[root@localhost tmp]#



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值