这篇“linux软链接如何创建”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“linux软链接如何创建”文章吧。

在linux中,软连接相当于windows中的快捷方式,以路径的形式存在;在软连接中,文件实际上是一个文本文件,其中包含的有另一文件的位置信息。创建软连接的语法为“ln -s target source”,参数“target”表示目标文件(夹),即被指向的文件(夹),而参数“source”表示当前目录的软连接名,即源文件(夹)。

本教程操作环境: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.创建测试文件及文件夹

[root@server6~]#mkdirtest_chk
[root@server6~]#touchtest_chk/test.txt
[root@server6~]#echo"hellospark">test_chk/test.txt
[root@server6~]#cattest_chk/test.txt
hellospark
[root@server6~]#ll
总用量84
-rw-------.1rootroot12576月1601:17anaconda-ks.cfg
drwxr-xr-x.25rootroot409611月110:28azkabanJob
-rw-r--r--.1rootroot6732211月410:24azkabanJob.zip
drwxr-xr-x.4rootroot377月1311:01hadoop_temp
-rw-r--r--.1rootroot547月414:11HelloLinux.txt
drwxr-xr-x.2rootroot2211月410:41test_chk
-rw-r--r--.1rootroot6710月815:52zookeeper.out
[root@server6~]#ln-stest_chk/test_chk_ln
[root@server6~]#ll
总用量84
-rw-------.1rootroot12576月1601:17anaconda-ks.cfg
drwxr-xr-x.25rootroot409611月110:28azkabanJob
-rw-r--r--.1rootroot6732211月410:24azkabanJob.zip
drwxr-xr-x.4rootroot377月1311:01hadoop_temp
-rw-r--r--.1rootroot547月414:11HelloLinux.txt
drwxr-xr-x.2rootroot2211月410:41test_chk
lrwxrwxrwx.1rootroot911月410:42test_chk_ln->test_chk/
-rw-r--r--.1rootroot6710月815:52zookeeper.out
[root@server6~]#cdtest_chk_ln/
[root@server6test_chk_ln]#ll
总用量4
-rw-r--r--.1rootroot1211月410:41test.txt
[root@server6test_chk_ln]#cattest.txt
hellospark
[root@server6test_chk_ln]#ll
总用量4
-rw-r--r--.1rootroot1211月410:41test.txt
[root@server6test_chk_ln]#cattest.txt
hellospark

注意

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

2、命令示例解释

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

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

linux软链接如何创建

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

linux软链接如何创建
需要注意的是,当前所有目录下的文件都不能重名,因为我之前有一个文件夹是scores,所以这里就简单的命名成了scor

软连接的删除

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

  • 错误示范

[root@server6test_chk_ln]#cd..
[root@server6~]#ll
总用量84
-rw-------.1rootroot12576月1601:17anaconda-ks.cfg
drwxr-xr-x.25rootroot409611月110:28azkabanJob
-rw-r--r--.1rootroot6732211月410:24azkabanJob.zip
drwxr-xr-x.4rootroot377月1311:01hadoop_temp
-rw-r--r--.1rootroot547月414:11HelloLinux.txt
drwxr-xr-x.2rootroot2211月410:41test_chk
lrwxrwxrwx.1rootroot911月410:42test_chk_ln->test_chk/
-rw-r--r--.1rootroot6710月815:52zookeeper.out
[root@server6~]#rm-rf./test_chk_ln/
[root@server6~]#ll
总用量84
-rw-------.1rootroot12576月1601:17anaconda-ks.cfg
drwxr-xr-x.25rootroot409611月110:28azkabanJob
-rw-r--r--.1rootroot6732211月410:24azkabanJob.zip
drwxr-xr-x.4rootroot377月1311:01hadoop_temp
-rw-r--r--.1rootroot547月414:11HelloLinux.txt
drwxr-xr-x.2rootroot611月410:42test_chk
lrwxrwxrwx.1rootroot911月410:42test_chk_ln->test_chk/
-rw-r--r--.1rootroot6710月815:52zookeeper.out
[root@server6~]#cdtest_chk
[root@server6test_chk]#ll
总用量0
[root@server6test_chk]#ll
总用量0

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

  • 正确删除软连接

[root@server6~]#rm-rf./test_chk_ln
[root@server6~]#ll
总用量84
-rw-------.1rootroot12576月1601:17anaconda-ks.cfg
drwxr-xr-x.25rootroot409611月110:28azkabanJob
-rw-r--r--.1rootroot6732211月410:24azkabanJob.zip
drwxr-xr-x.4rootroot377月1311:01hadoop_temp
-rw-r--r--.1rootroot547月414:11HelloLinux.txt
drwxr-xr-x.2rootroot2211月410:44test_chk
-rw-r--r--.1rootroot6710月815:52zookeeper.out
[root@server6~]#cdtest_chk/
[root@server6test_chk]#ll
总用量4
-rw-r--r--.1rootroot1211月410:44test.txt

以上就是关于“linux软链接如何创建”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注行业资讯频道。

推荐内容:linux创建软链接失败是什么原因造成的

点赞(37) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部