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 链接名 只删快捷方式,不影响源文件
理解断链 删掉源文件后访问链接 软链接失效,但可以重新指向
相关推荐
绿豆人1 小时前
操作系统上电后流程
linux·服务器
鹿角片ljp1 小时前
实验室显卡与本机远程连接复盘:直连SSH到ZeroTier
运维·ssh
sbjdhjd2 小时前
企业级 Docker 镜像仓库建设与运维规范
linux·运维·docker·云原生·容器·eureka·开源
TEC_INO2 小时前
Linux_54:RV1126的VI模块讲解
linux·运维·人工智能
kdxiaojie2 小时前
U-Boot分析【学习笔记】(6)
linux·笔记·学习
期待のcode2 小时前
Redis数据类型
运维·数据结构·redis
Tingjct2 小时前
Linux开发工具
linux·运维·服务器
cui_ruicheng2 小时前
Linux线程(三):线程同步、互斥与生产者消费者模型
linux·服务器·开发语言
xingyuzhisuan2 小时前
适合微调Llama 3 70B模型的最低GPU配置推荐
运维·人工智能·算法·llama·gpu算力