Linux的ln命令

Linux的软链接(Symbolic Link,也称符号链接)相当于Window系统中的"快捷方式"。它是一个特殊的文件,其内容是另一个文件或目录的路径。

Linux中使用ln命令来创建软链接,创建软链接时必须加上-s参数

基本语法:

bash 复制代码
ln -s [目标文件或目录的路径,即被链接的文件或目录] [软链接的名称/路径,即要链接去的路径]

-s代表symbolic,如果忘记加-s,系统会默认创建一个硬链接。

省略规则:

当省略软链接路径时,ln会在当前目录下创建一个同名的软链接

bash 复制代码
# 完整写法
ln -s /home/user/文档/周报.docx ./周报.docx

# 省略链接名(效果同上)
ln -s /home/user/文档/周报.docx

效果 :在当前目录创建名为 周报.docx 的软链接,指向 /home/user/文档/周报.docx

bash 复制代码
[root@master etc]# pwd
/etc
[root@master etc]# ls -l yum.conf
-rw-r--r--. 1 root root 970 Apr  2  2020 yum.conf
[root@master etc]# cat yum.conf
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release


#  This is the default, if you make this bigger yum won't see if the metadata
# is newer on the remote and so you'll "gain" the bandwidth of not having to
# download the new metadata and "pay" for it by yum not having correct
# information.
#  It is esp. important, to have correct metadata, for distributions like
# Fedora which don't keep old packages around. If you don't like this checking
# interupting your command line usage, it's much better to have something
# manually check the metadata once an hour (yum-updatesd will do this).
# metadata_expire=90m

# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d

如上,在/etc中有一个yum.conf文件,完整路径是/etc/yum.conf

但这个路径有点长,想给它构建一个快捷方式,放在root的家目录中

bash 复制代码
[root@master ~]# ln -s /etc/yum.conf # 省略第三个参数:要链接去的路径。则会在当前目录创建软链接
[root@master ~]# ls
anaconda-ks.cfg  data1.txt  data3.txt  DIR  file1.txt  hadoop-3.1.4.tar.gz  test.txt  yum.conf
[root@master ~]# ls -l yum.conf
lrwxrwxrwx. 1 root root 13 May 10 10:39 yum.conf -> /etc/yum.conf # 标识符第一位为l,表示软链接,且有箭头->指向源链接的路径
[root@master ~]# cat yum.conf
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release


#  This is the default, if you make this bigger yum won't see if the metadata
# is newer on the remote and so you'll "gain" the bandwidth of not having to
# download the new metadata and "pay" for it by yum not having correct
# information.
#  It is esp. important, to have correct metadata, for distributions like
# Fedora which don't keep old packages around. If you don't like this checking
# interupting your command line usage, it's much better to have something
# manually check the metadata once an hour (yum-updatesd will do this).
# metadata_expire=90m

# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d
[root@master ~]# cat /etc/yum.conf
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release


#  This is the default, if you make this bigger yum won't see if the metadata
# is newer on the remote and so you'll "gain" the bandwidth of not having to
# download the new metadata and "pay" for it by yum not having correct
# information.
#  It is esp. important, to have correct metadata, for distributions like
# Fedora which don't keep old packages around. If you don't like this checking
# interupting your command line usage, it's much better to have something
# manually check the metadata once an hour (yum-updatesd will do this).
# metadata_expire=90m

# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d

那上面是对文件创建软链接,现在来对路径来创建软链接

bash 复制代码
[root@master ~]# cd /etc/yum
[root@master yum]# ls
fssnap.d  pluginconf.d  protected.d  vars  version-groups.conf
[root@master yum]# ln -s /etc/yum ~/link-yum
[root@master yum]# ls ~/
anaconda-ks.cfg  data1.txt  data3.txt  DIR  file1.txt  hadoop-3.1.4.tar.gz  link-yum  test.txt  yum.conf
[root@master yum]# cd ~
[root@master ~]# ls
anaconda-ks.cfg  data1.txt  data3.txt  DIR  file1.txt  hadoop-3.1.4.tar.gz  link-yum  test.txt  yum.conf
[root@master ~]# ls -l link-yum
lrwxrwxrwx. 1 root root 8 May 10 10:45 link-yum -> /etc/yum
[root@master ~]# ls link-yum
fssnap.d  pluginconf.d  protected.d  vars  version-groups.conf
[root@master ~]# ls /etc/yum
fssnap.d  pluginconf.d  protected.d  vars  version-groups.conf
bash 复制代码
rain@LAPTOP-IIB6PKR5:/etc$ la -l apt
total 28
drwxr-xr-x 2 root root 4096 May  7 07:54 apt.conf.d
drwxr-xr-x 2 root root 4096 Apr  1  2024 auth.conf.d
drwxr-xr-x 2 root root 4096 Apr  1  2024 keyrings
drwxr-xr-x 2 root root 4096 Feb 10 08:56 preferences.d
-rw-r--r-- 1 root root  270 Feb 10 08:56 sources.list
drwxr-xr-x 2 root root 4096 Feb 10 08:56 sources.list.d
drwxr-xr-x 2 root root 4096 Feb 10 08:55 trusted.gpg.d
rain@LAPTOP-IIB6PKR5:/etc$ ln -s /etc/apt ~/link-apt
rain@LAPTOP-IIB6PKR5:/etc$ ls ~
Projects  Tests  fnm_setup  hy2  link-apt  miniforge3  miniforge_setup
rain@LAPTOP-IIB6PKR5:/etc$ cd
rain@LAPTOP-IIB6PKR5:~$ ls
Projects  Tests  fnm_setup  hy2  link-apt  miniforge3  miniforge_setup
rain@LAPTOP-IIB6PKR5:~$ ls -l link-apt
lrwxrwxrwx 1 rain rain 8 May 10 10:49 link-apt -> /etc/apt
rain@LAPTOP-IIB6PKR5:~$ ls link-apt
apt.conf.d  auth.conf.d  keyrings  preferences.d  sources.list  sources.list.d  trusted.gpg.d
rain@LAPTOP-IIB6PKR5:~$ cd link-apt
rain@LAPTOP-IIB6PKR5:~/link-apt$ ls
apt.conf.d  auth.conf.d  keyrings  preferences.d  sources.list  sources.list.d  trusted.gpg.d

ln常用命令

操作 命令 实际意义
创建软链接 ln -s 真实文件 链接名 给文件起一个"快捷方式"
查看链接 ls -l 链接名 看箭头知道指向谁
切换指向 ln -sf 新目标 链接名 瞬间更换配置环境
删除链接 rm 链接名 只删快捷方式,不影响源文件
理解断链 删掉源文件后访问链接 软链接失效,但可以重新指向
相关推荐
艾伦_耶格宇2 分钟前
【DOCKER容器实战】-2MySQL
运维·docker·容器
Brilliantwxx9 分钟前
42525
运维
小鱼仙官10 分钟前
Ubuntu C++ NTP校时程序
linux·c++·ubuntu
RisunJan11 分钟前
Linux命令-vgdisplay(显示卷组详细属性)
linux·运维·数据库
深念Y36 分钟前
RIO-UL00(EMUI 4.1 / Android 6.0.1 / arm64)开机自启动 sshd
android·linux·华为·安卓·chroot·sshd·emui
Escalating_xu40 分钟前
【Linux线程】线程控制全解析:终止、join/detach、cancel、线程栈与 NPTL(下篇)
java·linux·运维
老当益壮梁奶奶1 小时前
Linux软件编程学习笔记(二)Linux文件IO编程入门:从fopen到fgetc
linux·c语言·c++·笔记·学习
sunoo-2291 小时前
C 语言文件 IO 全攻略:从基础函数到实战踩坑(BMP 读取 + 词典查询)
linux·c语言·前端·笔记·vscode·学习
白猫不黑1 小时前
从运维到网络安全:转行优势、路径与薪资前景全解析
运维·网络·web安全·网络安全·信息安全·渗透测试
草莓熊Lotso1 小时前
【Linux网络加餐】手动部署:SSH 与 Web 服务实战 + 底层原理全解析
linux·运维·网络·人工智能·python·langchain·ssh