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 链接名 只删快捷方式,不影响源文件
理解断链 删掉源文件后访问链接 软链接失效,但可以重新指向
相关推荐
tg_xianheyun2 小时前
CDN节点分布如何影响网页加载速度和用户体验
服务器·cdn加速·全球访问优化
lsh曙光2 小时前
延时at指令和定时cron指令
linux·服务器·网络
圆山猫3 小时前
[Virtualization](四):Linux KVM/RISC-V 的 vCPU 运行路径
java·linux·risc-v
布鲁飞丝3 小时前
vivo Pulsar 万亿级消息处理实践()-Ansible运维部署
运维·ansible
XR1234567883 小时前
企业全光网络架构选型技术白皮书:从物理层到运维层的全链路分析
运维·网络·架构
似的8353 小时前
一步一步学习使用FireMonkey动画() 使用TAnimator类创建动画
linux·学习·nginx
HiDev_3 小时前
【非标自动化】2、认识元器件(直线模组)
运维·自动化
夏殇之殁4 小时前
包中创建自定义列表项。 . 使用自定义列表项进行数据绑定 . 将天气预报数据保存到本地内存表,通过LiveBindings进行显示。 ...
服务器·前端·javascript
龙仔7254 小时前
人大金仓 KingbaseES V8 只读账号创建完整运维笔记
运维·笔记·sql·人大金仓
我不管我就要叫小猪5 小时前
嵌入式Linux----网络通信
linux·运维·服务器