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 链接名 只删快捷方式,不影响源文件
理解断链 删掉源文件后访问链接 软链接失效,但可以重新指向
相关推荐
zzzzzz3109 小时前
9K Star 炸裂开源!这个 C 语言写的代码知识图谱,把 Linux 内核索引压缩到了 3 分钟
linux·服务器·sql
XIAOHEZIcode9 小时前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
用户0328472220701 天前
如何搭建本地yum源(上)
运维
A小辣椒2 天前
TShark:Wireshark CLI 功能
linux
A小辣椒2 天前
TShark:基础知识
linux
AlfredZhao2 天前
OCI 明明分配了 200G 系统盘,为什么 df 只看到 30G?
linux·oci
AlfredZhao3 天前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
用户9718356334663 天前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
猪脚踏浪3 天前
linux 拷贝文件或目录到指定的位置
linux
大树884 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai