我是靠谱客的博主 顺利小蚂蚁,这篇文章主要介绍linux 软链接是什么,现在分享给大家,希望可以做个参考。

本教程操作环境:linux7.3系统、Dell G3电脑。

Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link),又称软链接。

Linux软连接

相当于windows中的快捷方式,由于软连接所创建的文件为一个独立的新的文件,所以会占用掉indoe与block

它实际上是一个特殊的文件。在软连接中,文件实际上是一个文本文件,其中包含的有另一文件的位置信息。

  • 软链接,以路径的形式存在。类似于Windows操作系统中的快捷方式

  • 软链接可以 跨文件系统 ,硬链接不可以

  • 软链接可以对一个不存在的文件名进行链接

  • 软链接可以对目录进行链接

1、创建语法

ln -s target source

解释下:

ln -s:表示创建一个软连接;

target:表示目标文件(夹)【即被指向的文件(夹)】

source:表示当前目录的软连接名。【源文件(夹)】

2 具体示例

  • step 1.创建测试文件及文件夹
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@server6 ~]# mkdir test_chk [root@server6 ~]# touch test_chk/test.txt [root@server6 ~]# echo "hello spark" > test_chk/test.txt [root@server6 ~]# cat test_chk/test.txt hello spark [root@server6 ~]# ll 总用量 84 -rw-------. 1 root root 1257 6月 16 01:17 anaconda-ks.cfg drwxr-xr-x. 25 root root 4096 11月 1 10:28 azkabanJob -rw-r--r--. 1 root root 67322 11月 4 10:24 azkabanJob.zip drwxr-xr-x. 4 root root 37 7月 13 11:01 hadoop_temp -rw-r--r--. 1 root root 54 7月 4 14:11 HelloLinux.txt drwxr-xr-x. 2 root root 22 11月 4 10:41 test_chk -rw-r--r--. 1 root root 67 10月 8 15:52 zookeeper.out
登录后复制
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@server6 ~]# ln -s test_chk/ test_chk_ln [root@server6 ~]# ll 总用量 84 -rw-------. 1 root root 1257 6月 16 01:17 anaconda-ks.cfg drwxr-xr-x. 25 root root 4096 11月 1 10:28 azkabanJob -rw-r--r--. 1 root root 67322 11月 4 10:24 azkabanJob.zip drwxr-xr-x. 4 root root 37 7月 13 11:01 hadoop_temp -rw-r--r--. 1 root root 54 7月 4 14:11 HelloLinux.txt drwxr-xr-x. 2 root root 22 11月 4 10:41 test_chk lrwxrwxrwx. 1 root root 9 11月 4 10:42 test_chk_ln -> test_chk/ -rw-r--r--. 1 root root 67 10月 8 15:52 zookeeper.out [root@server6 ~]# cd test_chk_ln/ [root@server6 test_chk_ln]# ll 总用量 4 -rw-r--r--. 1 root root 12 11月 4 10:41 test.txt [root@server6 test_chk_ln]# cat test.txt hello spark [root@server6 test_chk_ln]# ll 总用量 4 -rw-r--r--. 1 root root 12 11月 4 10:41 test.txt [root@server6 test_chk_ln]# cat test.txt hello spark
登录后复制

注意

1、创建软连接时,不用创建文件夹。

2、命令示例解释

执行的命令是: ln -s /storage/lawson/scores scor

其含义就是:将scor指向 /storage/lawson/scores/目录下

在这里插入图片描述

这里是当前的scor 指向 /storage/lawson/scores 中。这里显示红色,是因为/storage/lawson/scores这个目录不存在,如果创建该目录,那就可以得到蓝色的显示了。

在这里插入图片描述
需要注意的是,当前所有目录下的文件都不能重名,因为我之前有一个文件夹是scores,所以这里就简单的命名成了scor

软连接的删除

rm -rf ./test_chk_ln/ 会删除文件夹下的所有内容,但是没有删除这个链接;
rm -rf ./test_chk_ln 则是仅删除这个软链接,不会删除下面的内容。

  • 错误示范
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@server6 test_chk_ln]# cd .. [root@server6 ~]# ll 总用量 84 -rw-------. 1 root root 1257 6月 16 01:17 anaconda-ks.cfg drwxr-xr-x. 25 root root 4096 11月 1 10:28 azkabanJob -rw-r--r--. 1 root root 67322 11月 4 10:24 azkabanJob.zip drwxr-xr-x. 4 root root 37 7月 13 11:01 hadoop_temp -rw-r--r--. 1 root root 54 7月 4 14:11 HelloLinux.txt drwxr-xr-x. 2 root root 22 11月 4 10:41 test_chk lrwxrwxrwx. 1 root root 9 11月 4 10:42 test_chk_ln -> test_chk/ -rw-r--r--. 1 root root 67 10月 8 15:52 zookeeper.out [root@server6 ~]# rm -rf ./test_chk_ln/ [root@server6 ~]# ll 总用量 84 -rw-------. 1 root root 1257 6月 16 01:17 anaconda-ks.cfg drwxr-xr-x. 25 root root 4096 11月 1 10:28 azkabanJob -rw-r--r--. 1 root root 67322 11月 4 10:24 azkabanJob.zip drwxr-xr-x. 4 root root 37 7月 13 11:01 hadoop_temp -rw-r--r--. 1 root root 54 7月 4 14:11 HelloLinux.txt drwxr-xr-x. 2 root root 6 11月 4 10:42 test_chk lrwxrwxrwx. 1 root root 9 11月 4 10:42 test_chk_ln -> test_chk/ -rw-r--r--. 1 root root 67 10月 8 15:52 zookeeper.out [root@server6 ~]# cd test_chk [root@server6 test_chk]# ll 总用量 0 [root@server6 test_chk]# ll 总用量 0
登录后复制

可以发现该文件夹下的内容都被删了。。。

  • 正确删除软连接
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@server6 ~]# rm -rf ./test_chk_ln [root@server6 ~]# ll 总用量 84 -rw-------. 1 root root 1257 6月 16 01:17 anaconda-ks.cfg drwxr-xr-x. 25 root root 4096 11月 1 10:28 azkabanJob -rw-r--r--. 1 root root 67322 11月 4 10:24 azkabanJob.zip drwxr-xr-x. 4 root root 37 7月 13 11:01 hadoop_temp -rw-r--r--. 1 root root 54 7月 4 14:11 HelloLinux.txt drwxr-xr-x. 2 root root 22 11月 4 10:44 test_chk -rw-r--r--. 1 root root 67 10月 8 15:52 zookeeper.out [root@server6 ~]# cd test_chk/ [root@server6 test_chk]# ll 总用量 4 -rw-r--r--. 1 root root 12 11月 4 10:44 test.txt
登录后复制

相关推荐:《Linux视频教程》

以上就是linux 软链接是什么的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是顺利小蚂蚁最近收集整理的关于linux 软链接是什么的全部内容,更多相关linux内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(130)

评论列表共有 0 条评论

立即
投稿
返回
顶部