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软/硬链接命令的简单尝试。

 

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


目录
相关文章
|
2天前
|
Linux
Linux 常用文件查看命令
`cat` 命令用于连接文件并打印到标准输出,适用于快速查看和合并文本文件内容。常用示例包括:`cat file1.txt` 查看单个文件,`cat file1.txt file2.txt` 合并多个文件,`cat > filename` 创建新文件,`cat >> filename` 追加内容。`more` 和 `less` 命令用于分页查看文件,`tail` 命令则用于查看文件末尾内容,支持实时追踪日志更新,如 `tail -f file.log`。
20 5
Linux 常用文件查看命令
|
8天前
|
Linux
Linux od命令
本文详细介绍了Linux中的 `od`命令,包括其基本语法、常用选项和示例。通过这些内容,你可以灵活地使用 `od`命令查看文件内容,提高分析和调试效率。确保理解每一个选项和示例的实现细节,应用到实际工作中时能有效地处理各种文件查看需求。
43 19
|
14天前
|
存储 Linux Shell
Linux|Transfer.sh 轻松实现文件共享
Linux|Transfer.sh 轻松实现文件共享
27 2
Linux|Transfer.sh 轻松实现文件共享
|
2天前
|
Linux Windows
Linux 软链接与硬链接的创建及区别
本内容介绍了Linux系统中软链接与硬链接的创建、删除及两者的区别。软链接类似于Windows快捷方式,指向目标文件路径,若原文件被删除则失效,可跨文件系统创建;命令为`ln -s [目标文件路径] [链接文件名]`。硬链接指向相同数据块,删除原文件后仍有效,但不可跨文件系统;命令为`ln [目标文件路径] [链接文件名]`。删除软链接可使用`rm [软链接文件名]`。两者主要差异在于实现机制、依赖关系及适用范围。
26 9
|
19天前
|
缓存 Ubuntu Linux
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`则是一个功能强大的下载工具,适用于各种下载任务。在实际使用中,根据系统类型和任务需求选择合适的工具,可以大大提高工作效率和系统管理的便利性。
107 25
|
1天前
|
Linux
Linux目录删除指南:彻底解决“Is a directory”错误
在 Linux 系统中遇到 `cannot remove 'xxx': Is a directory` 错误,是因为删除目录时未使用正确参数。解决方法包括:1) 使用 `rmdir` 删除空目录或 `rm -r` 删除非空目录;2) 检查并调整目录权限(如通过 `sudo` 提权);3) 处理特殊场景,例如文件属性异常、特殊字符或进程占用;4) 替代方法如 `find -delete` 或文件系统修复。操作前建议备份数据,并启用防误删功能(如 `alias rm='rm -i'`)。掌握 `rm` 和 `rmdir` 的区别是关键。
13 1
|
7天前
|
Linux 网络安全 虚拟化
linux怎么把文件传到docker里面
在现代应用开发中,Docker作为流行的虚拟化工具,广泛应用于微服务架构。文件传输到Docker容器是常见需求。常用方法包括:1) `docker cp`命令直接复制文件;2) 使用`-v`选项挂载宿主机目录,实现数据持久化和实时同步;3) 通过SCP/FTP协议传输文件;4) 在Dockerfile中构建镜像时添加文件。选择合适的方法并确保网络安全是关键。
89 1
|
23天前
|
Linux
Linux文件与目录的日常
目录的切换 一般使用(”pwd“)显示当前所在的目录 比如:当前目录是在home下面的,与用户名相同的文件夹,可以使用(”cd“)命令来切换目录; 进入下载目录(”cd home/a/下载“)这种从给目录开头的一长串路经”叫做绝对路径“; 进入图片目录(”cd .. /图片/“)".."代表当前路径的上级路径,相对于当前的目录而言的”叫做相对路径“,(”.“)代表当前路径; 如果,想快速切换,上一个所在目录可以(”cd - / cd..“); 如果,想快速切换,追原始的目录可以(”cd --“); 查看目录及文件
38 14
|
17天前
|
缓存 Linux
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
31 2
|
18天前
|
Linux Shell
Linux 将所有文件和目录名重命名为小写
Linux 将所有文件和目录名重命名为小写
25 3