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 链接名 只删快捷方式,不影响源文件
理解断链 删掉源文件后访问链接 软链接失效,但可以重新指向
相关推荐
GeW6 分钟前
当Linux内核开始“确定性”,制造高端化才有了底层语法
linux
星恒随风1 小时前
MySQL 从零入门(三):表的约束详解
linux·服务器·数据库·笔记·mysql
Alluxio2 小时前
Alluxio + OCI:为 AI 工作负载实现亚毫秒访问延迟与可扩展吞吐
运维·数据库·人工智能·分布式·缓存·ai·语言模型
StarsGreen4 小时前
libva在intel平台上的前端流程简介
linux
坤坤子吖6 小时前
Linux网络编程:自定义应用层协议与 JSON 序列化
linux·服务器·网络·c++·笔记·json
Sayai6 小时前
Neo4j 图数据质量检查实战:环路检测、孤立节点与连通分量,两条 Cypher 引出的数据体检清单(含变长路径性能陷阱)
运维·neo4j·图数据库·数据质量·cypher
青瓦梦滋7 小时前
Redis主从复制
服务器·数据库·redis·缓存·bootstrap
Elastic 中国社区官方博客7 小时前
更少的位数,相同的召回率:DiskBBQ 更保守的自动校准扫描
大数据·运维·开发语言·人工智能·elasticsearch·搜索引擎
方便面不加香菜7 小时前
Docker Volume -- 管理卷Volume详解
运维·docker·容器
沫璃染墨8 小时前
从零入门计算机网络系列(十八):TCP连接管理详解——从三次握手与四次挥手到TIME_WAIT机制
运维·服务器·网络·c++·网络协议·tcp/ip·tcp