惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
The Blog of Author Tim Ferriss
S
Securelist
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Tor Project blog
S
Schneier on Security
Google DeepMind News
Google DeepMind News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Blog — PlanetScale
Blog — PlanetScale
AWS News Blog
AWS News Blog
F
Fortinet All Blogs
C
Cisco Blogs
L
LangChain Blog
MongoDB | Blog
MongoDB | Blog
D
Docker
T
The Exploit Database - CXSecurity.com
The GitHub Blog
The GitHub Blog
月光博客
月光博客
量子位
A
Arctic Wolf
博客园 - 叶小钗
L
LINUX DO - 热门话题
J
Java Code Geeks
The Hacker News
The Hacker News
Security Latest
Security Latest
V
Vulnerabilities – Threatpost
M
MIT News - Artificial intelligence
博客园 - 【当耐特】
T
Tenable Blog
Microsoft Security Blog
Microsoft Security Blog
Recent Announcements
Recent Announcements
Simon Willison's Weblog
Simon Willison's Weblog
B
Blog
阮一峰的网络日志
阮一峰的网络日志
A
About on SuperTechFans
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Threat Research - Cisco Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Y
Y Combinator Blog
PCI Perspectives
PCI Perspectives
Hacker News: Ask HN
Hacker News: Ask HN
P
Palo Alto Networks Blog
小众软件
小众软件
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
O
OpenAI News
P
Privacy International News Feed
P
Privacy & Cybersecurity Law Blog
N
Netflix TechBlog - Medium

Hsu Yeung 的博客

三圣花市 | Hsu Yeung 的博客 网站支持 Live Photo 图片展示 | Hsu Yeung 的博客 梦 | Hsu Yeung 的博客 周末午餐 | Hsu Yeung 的博客 张家界 | Hsu Yeung 的博客 话剧《寻她芳踪·张爱玲》 | Hsu Yeung 的博客 博客自动生成文章目录 | Hsu Yeung 的博客 灭螂行动! | Hsu Yeung 的博客 张靓颖成都演唱会 | Hsu Yeung 的博客 最近对博客做的一些微调总结 | Hsu Yeung 的博客 Windows 上安装 Lua | Hsu Yeung 的博客 网站支持展示 B 站 iframe 视频 SpringBoot 中使用 RedisTemplate 存取整数值的坑 | Hsu Yeung 的博客 SQL JOIN 中 ON 与 WHERE 的区别 值得记录的一些工作近况 | Hsu Yeung 的博客 周传雄成都演唱会 | Hsu Yeung 的博客 并行操作导致获取数据库连接超时 | Hsu Yeung 的博客 风信子 | Hsu Yeung 的博客 Linux 安装并配置 Nginx | Hsu Yeung 的博客 使用 logrotate 切割 nginx 日志 郁金香土培结果 | Hsu Yeung 的博客 MySQL LEFT JOIN 右表有多条数据但只取最新的一条 | Hsu Yeung 的博客 获取数据库锁等待超时问题 | Hsu Yeung 的博客 从零开始挑选相机 | Hsu Yeung 的博客 郁金香 | Hsu Yeung 的博客 我的第一束花 | Hsu Yeung 的博客 快乐的一周 | Hsu Yeung 的博客 重庆,来了 | Hsu Yeung 的博客 散步 | Hsu Yeung 的博客 Git 工作区、暂存区、版本库之间的关系 | Hsu Yeung 的博客 Collectors.toMap() 操作 value 为 null 的情况 AOP 记录请求参数时序列化异常问题 | Hsu Yeung 的博客 第一次参加演唱会 | Hsu Yeung 的博客 MySQL FIND_IN_SET 函数的使用 | Hsu Yeung 的博客 自动拆箱机制导致编译失败 | Hsu Yeung 的博客 最近 | Hsu Yeung 的博客 夜爬龙泉山 | Hsu Yeung 的博客 在 Windows 上安装多个版本 JDK 并切换使用的版本 安装 Windows Terminal 和 gsudo 记录 意外的奖励 | Hsu Yeung 的博客 记第一次修热水器 | Hsu Yeung 的博客 在 Windows 电脑上调试 iPhone Safari 浏览器的网页 给网站加上 RSS | Hsu Yeung 的博客 打雷了 | Hsu Yeung 的博客 豉汁蒸排骨 | Hsu Yeung 的博客 我的 2022 年 | Hsu Yeung 的博客 如何使用 Git 给代码打 tag | Hsu Yeung 的博客 记一次有趣的上班经历 | Hsu Yeung 的博客 我记得 | Hsu Yeung 的博客 Spring 新增或修改请求 header 参数 | Hsu Yeung 的博客 Spring 事务传播机制 | Hsu Yeung 的博客 在家自制“周黑鸭” | Hsu Yeung 的博客 晚餐 | Hsu Yeung 的博客 如何解决逻辑删除与数据库唯一约束冲突 | Hsu Yeung 的博客 [转]漫画赏析:Linux 内核到底长啥样 | Hsu Yeung 的博客 数据库逻辑设计之三大范式通俗理解 | Hsu Yeung 的博客 计算机中的大小端是指什么 | Hsu Yeung 的博客 MySQL 学习笔记 | Hsu Yeung 的博客 [转]《玉子市场》的人物魅力 | Hsu Yeung 的博客 Linux 安装 Redis | Hsu Yeung 的博客 Linux 安装 MySQL | Hsu Yeung 的博客 Linux 安装 JDK | Hsu Yeung 的博客 [转]Thymeleaf 表达式工具类 | Hsu Yeung 的博客 Shell 基础入门介绍 | Hsu Yeung 的博客 MySQL 将某些字段的值排在最前或最后 | Hsu Yeung 的博客 数据结构与算法学习笔记 | Hsu Yeung 的博客 Visual Studio Code 如何编写运行 C 程序?
Linux 中的软链接和硬链接 | Hsu Yeung 的博客
2020-07-05 · via Hsu Yeung 的博客

在Linux文件中有软链接(也叫做符号链接)硬链接两种链接方式。

那么这两种链接方式是什么以及有什么区别呢?

1. 结论

这里先给出结论,下面再用具体的例子来进行说明:

软链接:相当于创建一个快捷方式,链接文件会占用一个新的inode值,修改链接等同修改源文件,反之一样,删除源文件链接文件失效,删除链接文件则不影响源文件。

相关命令:

# 创建软链接
ln -s filename soft-link-file

硬链接:就相当于文件备份,但是此文件与源文件使用通同一个 inode 值并不占用任何空间,修改源文件或者链接文件都会同步修改,删除掉源文件对链接文件没有影响,反之也是一样。

相关命令:

# 创建硬链接
ln filename hard-link-file

2. 验证

ls -l
# total 0

# 创建一个文件,并向该文件中覆盖内容“something content”
touch filename && echo "something content" > filename

# 查看文件内容
cat filename 
# something content

# 查看文件列表并显示 inode 值
ls -li
# total 4
# 33574982 -rw-r--r--. 1 root root 18 Aug 18 06:20 filename

# 创建一个硬链接
ln filename hard-link-file
ls -li
# total 8
# 33574982 -rw-r--r--. 2 root root 18 Aug 18 06:20 filename
# 33574982 -rw-r--r--. 2 root root 18 Aug 18 06:20 hard-link-file	# 可以看出inode 值是一样的,因此并不会开辟新的存储空间

# 接下来向硬链接文件中追加内容,发现源文件中的内容也追加了
echo "appended content..." >> hard-link-file 
cat filename 
# something content
# appended content...

# 接下来创建一个软链接
ln -s filename soft-link-file
ls -li
# total 8
# 33574982 -rw-r--r--. 2 root root 38 Aug 18 06:26 filename
# 33574982 -rw-r--r--. 2 root root 38 Aug 18 06:26 hard-link-file

# 这里软链接文件的 inode 值与源文件不一样了,并且它的文件属性上也有一个 l 的 标志,这就说明它与之前我们创建的两个文件根本不是一个类型
# 33575005 lrwxrwxrwx. 1 root root  8 Aug 18 06:27 soft-link-file -> filename

# 向软链接文件中追加值,发现源文件依旧被修改了
echo "soft content..." >> filename 
cat filename 
# something content
# appended content...
# soft content...

# 接下来删除源文件看看硬链接和软连接会有什么变化
rm -f filename 
ls -li
# total 4
# 33574982 -rw-r--r--. 1 root root 54 Aug 18 06:30 hard-link-file
# 33575005 lrwxrwxrwx. 1 root root  8 Aug 18 06:27 soft-link-file -> filename
# 
# 硬链接文件内容依旧存在,因为它 inode 所指向的区块由于有一个硬链接在指向它,所以这个区块仍然有效,并且可以访问到。
cat hard-link-file 
# something content
# appended content...
# soft content...

# 但是软链接文件查看报错了,显示不存在该文件或目录
# 这是因为软链接的 inode 所指向的内容实际上是保存了一个绝对路径,当用户访问这个文件时,系统会自动将其替换成其所指的文件路径,然而这个文件已经被删除了,所以自然就会显示无法找到该文件了。
cat soft-link-file 
# cat: soft-link-file: No such file or directory

# 接下来我们再向软链接中写入一些内容看看会发生什么事情
echo "new content..." >> soft-link-file 
ls -li
# total 8
# 33825197 -rw-r--r--. 1 root root 15 Aug 18 06:36 filename
# 33574982 -rw-r--r--. 1 root root 54 Aug 18 06:30 hard-link-file
# 33575005 lrwxrwxrwx. 1 root root  8 Aug 18 06:27 soft-link-file -> filename
cat filename 
# new content...
cat hard-link-file 
# something content
# appended content...
# soft content...
# 查看上面的输出,神奇的事情发生了,删掉的 filename 文件居然“回来了”
# 但是这并不是真的回来了,只不过因为写入访问软链接时,系统自动将其路径替换
# 为其所代表的绝对路径,并直接访问那个路径了,但是这个文件并不存在,所以重新创建了一个,
# 看前面的 inode 值就可以知道,这个 filename 已经不是之前那个 filename 了。
# 并且硬链接文件中内容并没有受到影响,也证实了这一猜想!

3. 总结

  • 硬链接: 与普通文件没什么不同,inode 都指向同一个文件在硬盘中的区块
  • 软链接: 保存了其代表的文件的绝对路径,是另外一种文件,在硬盘上有独立的区块,访问时替换自身路径。