在linux中,链接文件分为硬链接和软链接文件两种,其中硬链接通过ln source_file dist_file建立,软链接通过ln -s source_file dist_file建立。
软硬链接的区别:硬链接相当于一个文件两个名称,而软链接相当于创建指向源的快捷方式
注意:源文件是已经存在的文件,目标文件是要创建的文件
软链接实验:[[email protected] ~]# ll
总用量 22024
-rw-------. 1 root root 1311 9月 29 04:14 anaconda-ks.cfg
drwxr-xr-x. 18 501 501 4096 10月 30 21:29 Python-3.6.1
-rw-r--r--. 1 root root 22540566 3月 21 2017 Python-3.6.1.tgz
[[email protected] ~]# ln -s anaconda-ks.cfg anaconda-ks.cfg.ln
[[email protected] ~]# ll
总用量 22024
-rw-------. 1 root root 1311 9月 29 04:14 anaconda-ks.cfg
lrwxrwxrwx. 1 root root 15 11月 16 23:13 anaconda-ks.cfg.ln -> anaconda-ks.cfg
drwxr-xr-x. 18 501 501 4096 10月 30 21:29 Python-3.6.1
-rw-r--r--. 1 root root 22540566 3月 21 2017 Python-3.6.1.tgz
[[email protected] ~]#
软链接可以通过ls -l命令看了软链接文件,标识是-> 表示目标指向源文件,跟ln定义的时候相反。
硬链接实验:[[email protected] ~]# ll
总用量 22024
-rw-------. 1 root root 1311 9月 29 04:14 anaconda-ks.cfg
lrwxrwxrwx. 1 root root 15 11月 16 23:13 anaconda-ks.cfg.ln -> anaconda-ks.cfg
drwxr-xr-x. 18 501 501 4096 10月 30 21:29 Python-3.6.1
-rw-r--r--. 1 root root 22540566 3月 21 2017 Python-3.6.1.tgz
[[email protected] ~]# ln anaconda-ks.cfg anaconda-ks.cfg.hln
[[email protected] ~]# ls -l
总用量 22028
-rw-------. 2 root root 1311 9月 29 04:14 anaconda-ks.cfg
-rw-------. 2 root root 1311 9月 29 04:14 anaconda-ks.cfg.hln
lrwxrwxrwx. 1 root root 15 11月 16 23:13 anaconda-ks.cfg.ln -> anaconda-ks.cfg
drwxr-xr-x. 18 501 501 4096 10月 30 21:29 Python-3.6.1
-rw-r--r--. 1 root root 22540566 3月 21 2017 Python-3.6.1.tgz
[[email protected] ~]#
可以看到不能通过ls -l看到anaconda-ks.cfg的硬链接指向哪个文件,只能看到硬链接计数变成了2.
我们可以通过inode来找到anaconda-ks.cfg的另外一个硬链接文件。[email protected] ~]# ls -il
总用量 22028
33582147 -rw-------. 2 root root 1311 9月 29 04:14 anaconda-ks.cfg
33582147 -rw-------. 2 root root 1311 9月 29 04:14 anaconda-ks.cfg.hln
33582167 lrwxrwxrwx. 1 root root 15 11月 16 23:13 anaconda-ks.cfg.ln -> anaconda-ks.cfg
50716171 drwxr-xr-x. 18 501 501 4096 10月 30 21:29 Python-3.6.1
34101767 -rw-r--r--. 1 root root 22540566 3月 21 2017 Python-3.6.1.tgz
[[email protected] ~]# find / -inum 33582147
/root/anaconda-ks.cfg
/root/anaconda-ks.cfg.hln
[[email protected] ~]#
注意:软链接能够跨越文件系统(分区),硬链接不可以。
原文:http://liuqun.blog.51cto.com/3544993/1982654