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

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

接下来,我们来看一下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软/硬链接命令的简单尝试。

 

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


目录
打赏
0
0
0
0
16
分享
相关文章
|
24天前
|
Linux系统之whereis命令的基本使用
Linux系统之whereis命令的基本使用
58 23
Linux系统之whereis命令的基本使用
Linux|Transfer.sh 轻松实现文件共享
Linux|Transfer.sh 轻松实现文件共享
20 2
Linux|Transfer.sh 轻松实现文件共享
|
10天前
|
Linux中yum、rpm、apt-get、wget的区别,yum、rpm、apt-get常用命令,CentOS、Ubuntu中安装wget
通过本文,我们详细了解了 `yum`、`rpm`、`apt-get`和 `wget`的区别、常用命令以及在CentOS和Ubuntu中安装 `wget`的方法。`yum`和 `apt-get`是高层次的包管理器,分别用于RPM系和Debian系发行版,能够自动解决依赖问题;而 `rpm`是低层次的包管理工具,适合处理单个包;`wget`则是一个功能强大的下载工具,适用于各种下载任务。在实际使用中,根据系统类型和任务需求选择合适的工具,可以大大提高工作效率和系统管理的便利性。
72 25
【Linux】进程IO|系统调用|open|write|文件描述符fd|封装|理解一切皆文件
本文详细介绍了Linux中的进程IO与系统调用,包括 `open`、`write`、`read`和 `close`函数及其用法,解释了文件描述符(fd)的概念,并深入探讨了Linux中的“一切皆文件”思想。这种设计极大地简化了系统编程,使得处理不同类型的IO设备变得更加一致和简单。通过本文的学习,您应该能够更好地理解和应用Linux中的进程IO操作,提高系统编程的效率和能力。
67 34
|
15天前
|
Linux文件与目录的日常
目录的切换 一般使用(”pwd“)显示当前所在的目录 比如:当前目录是在home下面的,与用户名相同的文件夹,可以使用(”cd“)命令来切换目录; 进入下载目录(”cd home/a/下载“)这种从给目录开头的一长串路经”叫做绝对路径“; 进入图片目录(”cd .. /图片/“)".."代表当前路径的上级路径,相对于当前的目录而言的”叫做相对路径“,(”.“)代表当前路径; 如果,想快速切换,上一个所在目录可以(”cd - / cd..“); 如果,想快速切换,追原始的目录可以(”cd --“); 查看目录及文件
36 14
|
9天前
|
Linux查看内存命令
1. free free命令是最常用的查看内存使用情况的命令。它显示系统的总内存、已使用内存、空闲内存和交换内存的总量。 free -h • -h 选项:以易读的格式(如GB、MB)显示内存大小。 输出示例: total used free shared buff/cache available Mem: 15Gi 4.7Gi 4.1Gi 288Mi 6.6Gi 9.9Gi Swap: 2.0Gi 0B 2.0Gi • to
23 2
|
10天前
|
Linux 将所有文件和目录名重命名为小写
Linux 将所有文件和目录名重命名为小写
23 3
深入解析:Linux网络配置工具ifconfig与ip命令的全面对比
虽然 `ifconfig`作为一个经典的网络配置工具,简单易用,但其功能已经不能满足现代网络配置的需求。相比之下,`ip`命令不仅功能全面,而且提供了一致且简洁的语法,适用于各种网络配置场景。因此,在实际使用中,推荐逐步过渡到 `ip`命令,以更好地适应现代网络管理需求。
49 11
|
3月前
|
golang编译成Linux可运行文件
本文介绍了如何在 Linux 上编译和运行 Golang 程序,涵盖了本地编译和交叉编译的步骤。通过这些步骤,您可以轻松地将 Golang 程序编译成适合 Linux 平台的可执行文件,并在目标服务器上运行。掌握这些技巧,可以提高开发和部署 Golang 应用的效率。
345 14
|
3月前
|
linux积累-core文件是干啥的
核心文件是Linux系统在程序崩溃时生成的重要调试文件,通过分析核心文件,开发者可以找到程序崩溃的原因并进行调试和修复。本文详细介绍了核心文件的生成、配置、查看和分析方法
192 6
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等