likes
comments
collection
share

linux(十七)文件和目录相关命令-软连接、硬链接 ln命令

作者站长头像
站长
· 阅读数 8

接下来,我们来看一下linux中创建软/硬链接的命令ln。

 

这个命令比较有意思,linux的挺多目录都是采用软连接的形式,具体请移步《linux(二)linux系统目录结构》这里边有详细的介绍。

 

软连接:

相当于是windows中的快捷方式。以路径的方式存在。也称为符号链接,有自己的数据块,主要存放了链接其他文件的路径。

可以对目录创建软连接

可以对一个不存在的文件创建软连接

软链接的创建可以跨文件系统

 

硬链接:

以副本的形式存在,但是不占用空间。

不允许给目录创建硬链接

硬链接只有在同一个文件系统中才能创建

 

一:语法

ln (参数)(源文件或目录)(目标文件或目录)

 

二:参数说明

-b 删除,覆盖以前建立的链接

-d 允许超级用户制作目录的硬链接

-f 强制执行

-i 交互模式,文件存在则提示用户是否覆盖

-n 把符号链接视为一般目录

-s 软链接(符号链接)

-v 显示详细的处理过程

 

简单说就是,你要是想创建软连接就必须加上-s,否则剩下的一切操作都是创建的硬链接。

 

为了更好的理解软硬链接到底是什么玩意,我们下边在服务器中创建文件测试一下。

1:文件测试软连接

首先,我们先对/root/目录下的test.sh文件创建一个软连接

cd /opt/test/
ln -s /root/test.sh lntest.sh

 

我们新开一个终端:

root@iZijvdp1z0m5q4Z:/opt/test# ll
total 8
drwxrwxrwx 2 root root 4096 Aug 24 16:59 ./
drwxr-xr-x 4 root root 4096 Aug 24 16:58 ../
lrwxrwxrwx 1 root root    7 Aug 24 16:59 lntest.sh -> /root/test.sh

 

上方显示,lntest.sh是/root/test.sh的软连接

 

接下来,我们修改一下lntest.sh中的内容,看看test.sh文件中的内容是否改变

root@iZijvdp1z0m5q4Z:/opt/test# echo "lntest写入" >> lntest.sh
root@iZijvdp1z0m5q4Z:/opt/test# cat lntest.sh
test写入
lntest写入
root@iZijvdp1z0m5q4Z:/opt/test# cd /root
root@iZijvdp1z0m5q4Z:~# cat test.sh
test写入
lntest写入
root@iZijvdp1z0m5q4Z:~#

 

通过上边的记录,我们可以发现,修改软连接lntest.sh文件,源文件test.sh会跟随改变。

 

下边我们测试一下,修改test.sh,lntest.sh是否会跟着改变。

root@iZijvdp1z0m5q4Z:~# echo "test再次写入" >> test.sh
root@iZijvdp1z0m5q4Z:~# cat test.sh
test写入
lntest写入
test再次写入
root@iZijvdp1z0m5q4Z:~# cd /opt/test/
root@iZijvdp1z0m5q4Z:/opt/test# cat lntest.sh
test写入
lntest写入
test再次写入
root@iZijvdp1z0m5q4Z:/opt/test#

 

通过上边的记录,我们可以发现,修改软连接test.sh文件,软连接文件lntest.sh也会跟随改变。

 

下面我们来测试一下删除,首先我们删除软连接文件,看源文件是否发生变化:

root@iZijvdp1z0m5q4Z:/opt/test# rm -f lntest.sh
root@iZijvdp1z0m5q4Z:/opt/test# ll
total 8
drwxrwxrwx 2 root root 4096 Aug 24 17:24 ./
drwxr-xr-x 4 root root 4096 Aug 24 16:58 ../
root@iZijvdp1z0m5q4Z:/opt/test# cd /root
root@iZijvdp1z0m5q4Z:~# ll | grep test.sh
-rw-r--r--  1 root root    41 Aug 24 17:21 test.sh
root@iZijvdp1z0m5q4Z:~#

 

那说明,删除软连接对源文件没有影响。下面我们测试一下,删除源文件对软连接是否有影响:

root@iZijvdp1z0m5q4Z:~# cd /opt/test/
root@iZijvdp1z0m5q4Z:/opt/test# ln -s /root/test.sh lntest.sh
root@iZijvdp1z0m5q4Z:/opt/test# rm -f /root/test.sh
root@iZijvdp1z0m5q4Z:/opt/test# cd /root
root@iZijvdp1z0m5q4Z:~# ll | grep test.sh
root@iZijvdp1z0m5q4Z:~# cd /opt/test/
root@iZijvdp1z0m5q4Z:/opt/test# ll
total 8
drwxrwxrwx 2 root root 4096 Aug 24 17:25 ./
drwxr-xr-x 4 root root 4096 Aug 24 16:58 ../
lrwxrwxrwx 1 root root   13 Aug 24 17:25 lntest.sh -> /root/test.sh
root@iZijvdp1z0m5q4Z:/opt/test# cat lntest.sh
cat: lntest.sh: No such file or directory
root@iZijvdp1z0m5q4Z:/opt/test#

 

软连接文件还在,但是我们尝试读取文件内容的时候,系统提示,没有这个文件。

 

这就说明,软连接完全就是快捷方式。

 

2:目录测试软连接

接下来,我们将root目录软连接至/opt/test目录

root@iZijvdp1z0m5q4Z:/opt/test# ln -s /root ./lnroot
root@iZijvdp1z0m5q4Z:/opt/test# ll
total 12
drwxrwxrwx 3 root root 4096 Aug 24 17:44 ./
drwxr-xr-x 4 root root 4096 Aug 24 16:58 ../
drwxr-xr-x 2 root root 4096 Aug 24 17:43 dir/
lrwxrwxrwx 1 root root    5 Aug 24 17:44 lnroot -> /root/
lrwxrwxrwx 1 root root   13 Aug 24 17:25 lntest.sh -> /root/test.sh
-rw-r--r-- 1 root root    0 Aug 24 17:43 test

 

还有一个小细节,这里提醒大家一下,通过上边的操作记录,我们可以发现,通过ll命令展示出来的目录,第一个字是d的就是代表目录,第一个字母是l的,就是代表软连接,第一个字母是-的,就是代表文件。

 

那其实,目录的操作大概跟文件就差不多了,但是吧,由于我这里是给root目录创建的软连接,所以,删除,我就不能测试了。理解万岁……

 

修改软连接lnroot目录下的test.sh文件:

root@iZijvdp1z0m5q4Z:/opt/test# cd lnroot
root@iZijvdp1z0m5q4Z:/opt/test/lnroot# echo "测试软连接目录lnroot写入" >> test.sh
root@iZijvdp1z0m5q4Z:/opt/test/lnroot# cat test.sh
测试软连接目录lnroot写入
root@iZijvdp1z0m5q4Z:/opt/test/lnroot# cat /root/test.sh
测试软连接目录lnroot写入
root@iZijvdp1z0m5q4Z:/opt/test/lnroot#

通过上边的记录,我们可以发现,修改软连接目录lnroot目录下test.sh文件,源目录root下test.sh也会跟随改变。

 

测试一下删除,当然,我这里是不能删除源目录root的

root@iZijvdp1z0m5q4Z:/opt/test/lnroot# cd ..
root@iZijvdp1z0m5q4Z:/opt/test# rm -f ./lnroot
root@iZijvdp1z0m5q4Z:/opt/test# ll
total 12
drwxrwxrwx 3 root root 4096 Aug 24 17:56 ./
drwxr-xr-x 4 root root 4096 Aug 24 16:58 ../
drwxr-xr-x 2 root root 4096 Aug 24 17:43 dir/
lrwxrwxrwx 1 root root   13 Aug 24 17:25 lntest.sh -> /root/test.sh
-rw-r--r-- 1 root root    0 Aug 24 17:43 test
root@iZijvdp1z0m5q4Z:/opt/test# cd /root
root@iZijvdp1z0m5q4Z:~# ll
total 252
drwx------ 13 root root  4096 Aug 24 17:53 ./
-rw-r--r--  1 root root    34 Aug 24 17:53 test.sh
root@iZijvdp1z0m5q4Z:~#

 

大概就是这么个情况。

 

3:文件测试硬链接

接下来,我们来测试一下硬链接。

root@iZijvdp1z0m5q4Z:~# mkdir -p /opt/ying
root@iZijvdp1z0m5q4Z:~# cd /opt/ying/
root@iZijvdp1z0m5q4Z:/opt/ying# ln /root/test.sh yingTest.sh
root@iZijvdp1z0m5q4Z:/opt/ying# ll
total 12
drwxr-xr-x 2 root root 4096 Aug 25 09:43 ./
drwxr-xr-x 5 root root 4096 Aug 25 09:42 ../
-rw-r--r-- 2 root root   34 Aug 24 17:53 yingTest.sh

 

从上边的操作记录我们可以看出,硬链接与软连接是不一样的,软连接第一个字母是l,而硬链接第一个字母是-

 

修改一下硬链接文件yingTest.sh看源文件是否改变。

root@iZijvdp1z0m5q4Z:/opt/ying# echo "yingTest.sh写入" >> yingTest.sh
root@iZijvdp1z0m5q4Z:/opt/ying# cat yingTest.sh
测试软连接目录lnroot写入
yingTest.sh写入
root@iZijvdp1z0m5q4Z:/opt/ying# cat /root/test.sh
测试软连接目录lnroot写入
yingTest.sh写入
root@iZijvdp1z0m5q4Z:/opt/ying#

 

通过上边的记录,我们可以发现,修改硬连接yingTest.sh文件,源文件test.sh也会跟随改变。

root@iZijvdp1z0m5q4Z:/opt/ying# echo "test.sh写入" >> /root/test.sh
root@iZijvdp1z0m5q4Z:/opt/ying# cat /root/test.sh
测试软连接目录lnroot写入
yingTest.sh写入
test.sh写入
root@iZijvdp1z0m5q4Z:/opt/ying# cat yingTest.sh
测试软连接目录lnroot写入
yingTest.sh写入
test.sh写入
root@iZijvdp1z0m5q4Z:/opt/ying#

 

通过上边的记录,我们可以发现,修改源文件test.sh文件,硬连接yingTest.sh也会跟随改变。

 

接着测试一下删除:

删除硬链接文件:

root@iZijvdp1z0m5q4Z:/opt/ying# rm -f yingTest.sh
root@iZijvdp1z0m5q4Z:/opt/ying# ll
total 8
drwxr-xr-x 2 root root 4096 Aug 25 09:56 ./
drwxr-xr-x 5 root root 4096 Aug 25 09:42 ../
root@iZijvdp1z0m5q4Z:/opt/ying# cat /root/test.sh
测试软连接目录lnroot写入
yingTest.sh写入
test.sh写入

 

很明显源文件是没有改变的。

 

删除源连接文件:

root@iZijvdp1z0m5q4Z:/opt/ying# ln /root/test.sh yingTest.sh
root@iZijvdp1z0m5q4Z:/opt/ying# rm -f /root/test.sh
root@iZijvdp1z0m5q4Z:/opt/ying# cat yingTest.sh
测试软连接目录lnroot写入
yingTest.sh写入
test.sh写入
root@iZijvdp1z0m5q4Z:/opt/ying#

 

通过上边的记录,我们可以发现,原链接文件test.sh文件删除之后,对硬链接yingTest.sh文件是没有影响的。

 

这也是软连接与硬链接最大的区别。

 

以上大概就是对linux软/硬链接命令的简单尝试。

 

有好的建议,请在下方输入你的评论。