Liunx的软链接和硬链接

 ln 命令

命令名称: ln
英文原意: make links between file
所在路径: /bin/ln
执行权限:所有用户。
功能描述:在文件之间建立链接。
 ln 命令的基本格式如下:

[root@localhost ~]# ln [选项] 源文件 目标文件
选项:
-s: 建立软链接文件。 如果不加“-s”选项,则建立硬链接文件
-f: 强制。如果目标文件已经存在,则删除目标文件后再建立链接文件

创建硬链接:

 [root@topcheer opt]# ln 1.txt /tmp/2.txt

  [root@topcheer opt]# ls -il 1.txt /tmp/2.txt
  37789174 -rw-r--r-- 2 root root 22 11月 15 14:33 1.txt
  37789174 -rw-r--r-- 2 root root 22 11月 15 14:33 /tmp/2.txt

[root@topcheer /]# cd /opt
[root@topcheer opt]# echo sssssss >> 1.txt
[root@topcheer opt]# cat 1.txt
11
22
33
44
55

EE77

sssssss
[root@topcheer opt]# cat /tmp/2.txt
11
22
33
44
55

EE77

sssssss
[root@topcheer opt]# 

创建软链接:

[root@topcheer tmp]# ln -s /opt/aaa /tmp/bbb
[root@topcheer tmp]# cat /tmp/bbb
tttt
[root@topcheer tmp]# cat /opt/aaa
tttt
[root@topcheer tmp]#
[root@topcheer opt]# chmod 400 aaa
[root@topcheer opt]# ll
总用量 361736
-rw-r--r--  2 root root        30 11月 19 22:28 1.txt
-rw-r--r--  1 root root         0 11月 19 22:47 aa
-r--------  1 root root         5 11月 19 22:48 aaa
drwxr-xr-x  9 root root       201 10月 23 00:00 gitlab
-rw-r--r--  1 root root 185646832 10月 24 23:52 jdk-8u181-linux-x64.tar.gz
-rw-r--r--  1 root root 150843392 9月  21 10:46 nginx.tar
drwxr-xr-x. 2 root root         6 10月 31 2018 rh
drwxr-xr-x  5 root root        55 9月  18 20:10 seata
-rw-r--r--  1 root root  33917798 11月 15 13:47 seata-server-0.8.1.zip
-rw-r--r--  1 root root         0 11月 19 22:48 tttt
[root@topcheer opt]# cat aaa
tttt
[root@topcheer opt]# su - wgr
[wgr@topcheer opt]$ cat aaa
cat: aaa: 权限不够
[wgr@topcheer opt]$ cd /tmp
[wgr@topcheer tmp]$ ll
总用量 1684
-rw-r--r-- 2 root root     30 11月 19 22:28 2.txt
lrwxrwxrwx 1 root root      8 11月 19 22:53 bbb -> /opt/aaa
drwxr-xr-x 2 root root     33 11月 15 13:56 hsperfdata_root
drwx------ 2 wgr  wgr      25 10月 24 21:22 ssh-RilUh9pJfVfg
drwx------ 3 root root     17 10月 24 21:22 systemd-private-40089c3bfa6648cca4f31bee3934047e-bolt.service-yPqrwK
drwx------ 3 root root     17 10月 24 21:22 systemd-private-40089c3bfa6648cca4f31bee3934047e-colord.service-KDQPF3
drwx------ 3 root root     17 10月 24 21:22 systemd-private-40089c3bfa6648cca4f31bee3934047e-cups.service-4OZmlm
drwx------ 3 root root     17 10月 24 21:22 systemd-private-40089c3bfa6648cca4f31bee3934047e-fwupd.service-nO4Lra
drwx------ 3 root root     17 10月 24 21:22 systemd-private-40089c3bfa6648cca4f31bee3934047e-rtkit-daemon.service-fJbDai
drwx------ 2 root root      6 11月 12 20:41 vmware-root_8744-2823883985
-rw------- 1 root root 340976 10月 25 06:26 yum_save_tx.2019-10-25.06-26.pvUE4I.yumtx
-rw------- 1 root root 340976 10月 26 06:20 yum_save_tx.2019-10-26.06-20.5BbH15.yumtx
-rw------- 1 root root 341450 11月  4 06:31 yum_save_tx.2019-11-04.06-31.WCUPBG.yumtx
-rw------- 1 root root 341450 11月 15 13:50 yum_save_tx.2019-11-15.13-50.Q0ypWO.yumtx
-rw------- 1 root root 341450 11月 19 21:13 yum_save_tx.2019-11-19.21-13.vM9rwR.yumtx
[wgr@topcheer tmp]$ cat bbb
cat: bbb: 权限不够
[wgr@topcheer tmp]$硬链接与软连接的特征

硬链接特征:
   源文件和硬链接文件拥有相同的 Inode 和 Block
   修改任意一个文件,另一个都改变
   删除任意一个文件,另一个都能使用
   硬链接标记不清,很难确认硬链接文件位置,不建议使用
   硬链接不能链接目录
   硬链接不能跨分区
软链接特征:
   软链接和源文件拥有不同的 Inode 和 Block
   两个文件修改任意一个,另一个都改变
   删除软链接,源文件不受影响;删除源文件,软链接不能使用
   链接没有实际数据,只保存源文件的 Inode,不论源文件多大,软链接大小不变
   软链接的权限是最大权限 lrwxrwxrwx.,但是由于没有实际数据,最终访问时需要参考源文件权限
   软链接可以链接目录
   软链接可以跨分区
   软链接特征明显,建议使用软连接 

原文地址:https://www.cnblogs.com/dalianpai/p/11893372.html