在linux中,链接文件分为硬链接和软链接文件两种,其中硬链接通过ln source_file dist_file建立,软链接通过ln -s source_file dist_file建立。

软硬链接的区别:硬链接相当于一个文件两个名称,而软链接相当于创建指向源的快捷方式

注意:源文件是已经存在的文件,目标文件是要创建的文件

软链接实验:

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@openstack ~] # 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
[root@openstack ~] # ln -s anaconda-ks.cfg anaconda-ks.cfg.ln 
[root@openstack ~] # 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
[root@openstack ~] #

软链接可以通过ls -l命令看了软链接文件,标识是-> 表示目标指向源文件,跟ln定义的时候相反。

硬链接实验:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@openstack ~] # 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
[root@openstack ~] # ln anaconda-ks.cfg anaconda-ks.cfg.hln
[root@openstack ~] # 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
[root@openstack ~] #

可以看到不能通过ls -l看到anaconda-ks.cfg的硬链接指向哪个文件,只能看到硬链接计数变成了2.

我们可以通过inode来找到anaconda-ks.cfg的另外一个硬链接文件。

1
2
3
4
5
6
7
8
9
10
11
root@openstack ~] # 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
[root@openstack ~] # find / -inum 33582147
/root/anaconda-ks .cfg
/root/anaconda-ks .cfg.hln
[root@openstack ~] #

注意:软链接能够跨越文件系统(分区),硬链接不可以。














本文转自lq201151CTO博客,原文链接:http://blog.51cto.com/liuqun/1982654 ,如需转载请自行联系原作者


Logo

更多推荐