ln:创建硬链接和软连接

xiaohai 2021-05-05 16:53:13 1383人围观 标签: Linux 
简介ln命令(理解为英文单词link的缩写)用来为文件创件链接,连接类型分为硬连接和符号连接两种,默认的连接类型是硬连接。如果要创建符号连接必须使用"-s"选项。

说明:

  ln命令(理解为英文单词link的缩写)用来为文件创件链接,连接类型分为硬连接和符号连接两种,默认的连接类型是硬连接。如果要创建符号连接必须使用”-s”选项。

格式:

  ln [option] [source] [target]
  ln [选项] [源文件或目录] [目标文件或目录]

常用参数说明:

  -s:创建软连接(符号链接)

链接知识说明和示例

1、硬链接

  • 硬链接是通过索引节点(Inode)来进行链接的。在Linux系统中所有的文件都有自己的一个独立的inode编号。硬链接生成的是一个普通的文件,目录不允许创建硬链接。硬链接的语法格式:ln source target
  • 硬链接的文件是多个文件使用同一个inode编号
  • 删除硬链接文件,源文件不受影响,删除源文件,硬链接文件也不受影响,所以这样可以作为备份
  • 目录不可以创建硬链接
[root@localhost test]# ln test1.txt abc1.txt #创建硬链接
[root@localhost test]# ll -i
总用量 8
  1410126 -rw-r--r--. 2 root root 4 5月   3 07:06 abc1.txt #是一个普通文件(-类型) inode:1410126与test1.txt一致
  1410131 drwxr-xr-x. 2 root root 6 5月   3 07:00 dir01
 36737691 drwxr-xr-x. 2 root root 6 5月   3 07:00 dir02
 72060197 drwxr-xr-x. 2 root root 6 5月   3 07:00 dir03
101621682 drwxr-xr-x. 2 root root 6 5月   3 07:00 dir04
  1410132 drwxr-xr-x. 2 root root 6 5月   3 07:00 dir05
  1410126 -rw-r--r--. 2 root root 4 5月   3 07:06 test1.txt
  1410127 -rw-r--r--. 1 root root 0 5月   3 07:00 test2.txt
  1410128 -rw-r--r--. 1 root root 0 5月   3 07:00 test3.txt
  1410129 -rw-r--r--. 1 root root 0 5月   3 07:00 test4.txt
  1410130 -rw-r--r--. 1 root root 0 5月   3 07:00 test5.txt
[root@localhost test]# cat test1.txt #查看test1.txt的内容
[root@localhost test]# echo 1 > test1.txt  #写入1到test1.txt
[root@localhost test]# echo test1.txt 
test1.txt
[root@localhost test]# cat abc1.txt #查看硬链接文件abc1.txt
1
[root@localhost test]# echo 2 >> abc1.txt #追加内容到硬链接文件中
[root@localhost test]# cat abc1.txt 
1
2
[root@localhost test]# cat test1.txt  #查看源文件内容
1
2
[root@localhost test]# rm -f abc1.txt  #删除硬链接文件
[root@localhost test]# cat test1.txt  #源文件不受影响
1
2

2、软链接

  • 软链接又称为符号链接。语法格式:ln -s source target,目标文件不能事先存在,软链接生成的是一个符号链接文件(l类型)
  • 可以把软链接理解成window系统的快捷方式
  • 软链接类似于一个文本文件
  • 删除了源文件,那么软链接一般都是白底红字显示
  • 软链接和源文件的inode是不一样的
  • 删掉还是可以使用rm
[root@localhost test]# ln -s test1.txt abc1.txt #创建软链接
[root@localhost test]# ll -i
总用量 4
  1410133 lrwxrwxrwx. 1 root root 9 5月   3 07:14 abc1.txt -> test1.txt #软链接的类型(l类型) inode和源文件也不一致
  1410131 drwxr-xr-x. 2 root root 6 5月   3 07:00 dir01
 36737691 drwxr-xr-x. 2 root root 6 5月   3 07:00 dir02
 72060197 drwxr-xr-x. 2 root root 6 5月   3 07:00 dir03
101621682 drwxr-xr-x. 2 root root 6 5月   3 07:00 dir04
  1410132 drwxr-xr-x. 2 root root 6 5月   3 07:00 dir05
  1410126 -rw-r--r--. 1 root root 4 5月   3 07:06 test1.txt
  1410127 -rw-r--r--. 1 root root 0 5月   3 07:00 test2.txt
  1410128 -rw-r--r--. 1 root root 0 5月   3 07:00 test3.txt
  1410129 -rw-r--r--. 1 root root 0 5月   3 07:00 test4.txt
  1410130 -rw-r--r--. 1 root root 0 5月   3 07:00 test5.txt
[root@localhost test]# cat abc1.txt 
1
2
[root@localhost test]# ln -s test2.txt dir01 #如果链接到存在的目录下,需要使用绝对路径,否查看会找不到对应的文件
[root@localhost test]# ll -i
总用量 4
  1410133 lrwxrwxrwx. 1 root root  9 5月   3 07:14 abc1.txt -> test1.txt
  1410131 drwxr-xr-x. 2 root root 22 5月   3 07:14 dir01
 36737691 drwxr-xr-x. 2 root root  6 5月   3 07:00 dir02
 72060197 drwxr-xr-x. 2 root root  6 5月   3 07:00 dir03
101621682 drwxr-xr-x. 2 root root  6 5月   3 07:00 dir04
  1410132 drwxr-xr-x. 2 root root  6 5月   3 07:00 dir05
  1410126 -rw-r--r--. 1 root root  4 5月   3 07:06 test1.txt
  1410127 -rw-r--r--. 1 root root  0 5月   3 07:00 test2.txt
  1410128 -rw-r--r--. 1 root root  0 5月   3 07:00 test3.txt
  1410129 -rw-r--r--. 1 root root  0 5月   3 07:00 test4.txt
  1410130 -rw-r--r--. 1 root root  0 5月   3 07:00 test5.txt
[root@localhost test]# ll -i dir01
总用量 0
1410134 lrwxrwxrwx. 1 root root 9 5月   3 07:14 test2.txt -> test2.txt #这里会进行提醒
[root@localhost test]# echo 3 >> test1.txt #追加数据到源文件
[root@localhost test]# cat test1.txt 
1
2
3
[root@localhost test]# cat abc1.txt 
1
2
3
[root@localhost test]# echo 4 >> abc1.txt #追加数据到目标文件
[root@localhost test]# cat abc1.txt 
1
2
3
4
[root@localhost test]# cat test1.txt 
1
2
3
4
[root@localhost test]# rm -f abc1.txt  #删除
[root@localhost test]# ll -i
总用量 4
  1410131 drwxr-xr-x. 2 root root 6 5月   3 07:16 dir01
 36737691 drwxr-xr-x. 2 root root 6 5月   3 07:00 dir02
 72060197 drwxr-xr-x. 2 root root 6 5月   3 07:00 dir03
101621682 drwxr-xr-x. 2 root root 6 5月   3 07:00 dir04
  1410132 drwxr-xr-x. 2 root root 6 5月   3 07:00 dir05
  1410126 -rw-r--r--. 1 root root 8 5月   3 07:17 test1.txt
  1410127 -rw-r--r--. 1 root root 0 5月   3 07:00 test2.txt
  1410128 -rw-r--r--. 1 root root 0 5月   3 07:00 test3.txt
  1410129 -rw-r--r--. 1 root root 0 5月   3 07:00 test4.txt
  1410130 -rw-r--r--. 1 root root 0 5月   3 07:00 test5.txt

[root@localhost test]# touch test1.txt  #重新新建源文件
[root@localhost test]# ln -s test1.txt abc1.txt #重新创建软链接
[root@localhost test]# rm -f test1.txt #删除源文件,那么软链接文件会提示
[root@localhost test]# ll
总用量 0
lrwxrwxrwx. 1 root root 9 5月   3 07:18 abc1.txt -> test1.txt
drwxr-xr-x. 2 root root 6 5月   3 07:16 dir01
drwxr-xr-x. 2 root root 6 5月   3 07:00 dir02
drwxr-xr-x. 2 root root 6 5月   3 07:00 dir03
drwxr-xr-x. 2 root root 6 5月   3 07:00 dir04
drwxr-xr-x. 2 root root 6 5月   3 07:00 dir05
-rw-r--r--. 1 root root 0 5月   3 07:00 test2.txt
-rw-r--r--. 1 root root 0 5月   3 07:00 test3.txt
-rw-r--r--. 1 root root 0 5月   3 07:00 test4.txt
-rw-r--r--. 1 root root 0 5月   3 07:00 test5.txt

提示

在软件的安装的时候,我们最好把每个版本分开安装,然后建立软链接指向固定的文件,这样方便其他人员查看和程序升级而不做任何修改。
如:

#如果我们安装了nginx
/apps/nginx1.19.1 #安装路径
ln -s /app/nginx1.19.1 /app/nginx #这样我们在每次升级软件后都建立软链接指向/app/nginx