RedHat 9下创建硬链接和软链接文件

RedHat 9下创建硬链接和软链接文件

 

    1、链接文件分为硬链接和软链接

    硬链接创建一个文件的备份,这两个文件指向同一个物理磁盘空间。这两个文件的内容是一模一样的。修改任意其中一个文件的内容,另一个文件的内容会自动更新到一样。

    软链接则类似Windows下创建快捷方式。

    2、硬链接示例(命令:ln 源文件 链接文件名)

    (1)创建一个硬链接文件

[root@localhost root]# ls
anaconda-ks.cfg  install.log  install.log.syslog  test.txt
[root@localhost root]# cat test.txt
This is test for linking file.
[root@localhost root]# ln test.txt /home/yoyo/hard_link_to_test
[root@localhost root]# cat /home/yoyo/hard_link_to_test
This is test for linking file.
    (2)修改源文件,硬链接文件内容也随之变化

[root@localhost root]# vi test.txt
[root@localhost root]# cat test.txt
This is test for hard-linking file.
[root@localhost root]# cat /home/yoyo/hard_link_to_test
This is test for hard-linking file.
[root@localhost root]# ls -l /home/yoyo/hard_link_to_test
-rw-r--r--    3 root     root           39 Mar  3 13:42 /home/yoyo/hard_link_to_test

    注:硬链接文件,没有l标志

    (3)修改硬链接文件,源文件的内容也会随之变化

[root@localhost root]# vi /home/yoyo/hard_link_to_test
[root@localhost root]# cat /home/yoyo/hard_link_to_test
This is a test for hard-linking files.
[root@localhost root]# cat test.txt
This is a test for hard-linking files.

    3、软链接示例(命令:ln -s 源文件 链接文件名)

    (1)创建一个软链接文件

[root@localhost root]# cat test.txt
This is a test for hard-linking files.
[root@localhost root]# ln -s test.txt /home/yoyo/soft_link_to_test
[root@localhost root]# cat /home/yoyo/soft_link_to_test
cat: /home/yoyo/soft_link_to_test: No such file or directory
[root@localhost root]# ls -l /home/yoyo/soft_link_to_test
lrwxrwxrwx    1 root     root            8 Mar  3 13:44 /home/yoyo/soft_link_to_test -> test.txt

    注:软链接文件,有l标志

    (2)软链接出错的问题

[root@localhost root]# pwd
/root
[root@localhost root]# ln -s test.txt soft_link
[root@localhost root]# cat soft_link
This is a test for hard-linking files.
[root@localhost root]# ln -s test.txt /home/soft_link
[root@localhost root]# cat /home/soft_link
cat: /home/soft_link: No such file or directory
[root@localhost root]# cd /home
[root@localhost home]# cat soft_link
cat: soft_link: No such file or directory
[root@localhost home]# ln -s /root/test.txt /home/new_soft_link
[root@localhost home]# cat /home/new_soft_link
This is a test for hard-linking files.
[root@localhost home]# cd ~
[root@localhost root]# ln -s ./test.txt /home/new_new_soft_link
[root@localhost root]# cat /home/new_new_soft_link
cat: /home/new_new_soft_link: No such file or directory

    注:源文件需要写绝对路径,如果源文件和软链接文件在同一个目录下,则不需要。

    (3)删除源文件,则软链接文件指向出错

[root@localhost root]# mv /home/test.txt /root/
[root@localhost root]# ls test.txt
test.txt
[root@localhost root]# cat /home/new_soft_link
This is a test for hard-linking files.
    其实,在图形界面很容易看出哪些软链接文件指向出错,红色反显的文件就是软链接出错的文件。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值