I got this question in an email the other day, and I realized it’s something many people might be unfamiliar with. In Linux, there are two types of file links, hardlinks and softlinks . Here, I’ll give a quick explanation of what these types of links are, and when you should use them.
Softlinks, also called symlinks, are the easiest to understand, especially because you’ve probably already used them. A softlink fills the same role as a Windows shortcut. Simply put, a softlink is a file that points to another file. When you create a softlink, you are creating a new file that exists only as a pointer to a file elsewhere on the system.
All links are created with the ln
command, using the -s
flag if you want a softlink (if you don’t use -s
, you’ll get a hardlink, which I’ll talk about in a second). The syntax of the command is:
For example, the following command will create a softlink to /usr/bin/firefox called firefox (in my “Desktop” directory):
You can see the softlink’s target by using the ls -l
command. You can also use this command to detect broken softlinks:
Now that we understand softlinks, let’s talk about hardlinks.
Whereas a softlink is a new file that points to an already-existing file, a hardlink is another instance of the original file . A diagram is the easiest way to explain what that means:
You can click on either of the images for a larger version. What it explains is that, when you create a hardlink, you are creating another pointer to the data location on disk, not a pointer to the existing file. That means that editing a hard link of a file is equivalent to editing the original instance of the file.
To drive the point home: a softlink is a shortcut to an existing file, whereas a hardlink is a reference to a location on disk (or, more accurately, a location in the filesystem). This means that the concept of a shortcut, a link pointing to another file, doesn’t make sense for hardlinks. But, what does make sense is asking how many references exist to a given location on disk (how many hardlinks exist for a file), which you can see by running the ‘stat’ command:
So for example, in this screenshot I’ve created a file and then built three hardlinks to it. When I run the ‘stat’ command on any of the files, it will show that there is a reference count of 4:
There are two major limitations of hardlinks. In these cases, you must use a softlink:
- A link across filesystems
- Linking to a directory
Because a hardlink is a direct reference to the underlying filesystem, you can’t hardlink across filesystems.
You can’t use a hardlink to link to a directory.
In the cases listed above, you must use a softlink because a hardlink simply won’t work. There are also cases where softlinks are preferable to hardlinks, even though either will work. For example, you’d probably want to pick a softlink when you want to create a shortcut especially when the destination of that shortcut might change in the future. For example, if you have a launcher to a beta version of an application, you may wish to use a softlink so you can easily change the target of the link between versions.
In most of the remaining cases (creating a link to a file on the same filesystem), hardlinks can be preferable for the following major reasons:
- Performance
- Storage space
There is a slight performance boost to be gained from using hardlinks. This is because since a hardlink references a spot on disk, rather than referencing another another file (which then references the actual spot on disk you want), there is one less disk seek involved in hardlinking.
Hardlinks don’t take up any additional space, since they are simply a reference to an already existing space of disk. Creating a softlink is creating a new file, and will consume a small amount (usually 4KB) of space on your filesystem.
Additionally, hardlinks are preferable because a softlink is vulnerable to losing the ‘master’ instance of a file (the file to which all the softlinks point). If the original file is deleted, then all softlinks to it become invalid. With a hardlink, you can delete any of the instances, including the original, and as long as at least one instance exists the file will remain on the system.
I hope this information was helpful and easy to understand. If you have any questions or comments, either about this topic or other topics you’d like to see explained on TechThrob, please comment below!
相关推荐
How to do it: detailed explanation..............55 Further reading...............57 Day 27: Using real headers......58 Who benefits?.................58 How to do it: Movable Type.....................
Windows Server 2008 Exam A QUESTION 1 Your network contains 100 servers that run Windows Server 2008 R2. A server named Server1 is deployed on the network. Server1 will be used ...Explanation/Reference:
1.敏捷开发强调测试先行,因为先编写测试代码,就可以有目的的预防某些bug的产生,要知道,治病不如预防!!!所以我个人觉得这个还是比较重要的,文件也不大,只有8M,但是高清。 2.本版本与网上其他资源不同之处...
《Boosting the Margin: A New Explanation for the Effectiveness of Voting Methods》是一篇由Robert E. Schapire、Yoav Freund、Peter Bartlett和Wee Sun Lee共同撰写的论文。该论文深入探讨了在机器学习领域中一...
17. "explain":动词,表示“解释”,名词为"explanation",如“give an explanation”。 18. "spit":动词,指“吐口水,吐痰”。 19. "add":动词,意为“增加”,名词形式为"addition",形容词为"additional",...
### JProfiler Helper: A Comprehensive Guide to Utilizing JProfiler for Performance Monitoring #### Introduction JProfiler is an advanced performance monitoring and profiling tool designed for Java ...
2. 符号notation explanation:在手册中使用了多种符号notation,例如 •、 等,用于区分不同的操作系统和打印机模型。 3. 打印机安装指南:手册提供了详细的打印机安装指南,包括如何安装打印机驱动程序、如何...
- *例句*: Her explanation was vague and difficult to understand. - *语法*: 形容词,表示不清楚。 37. **vain** [n.]: 徒劳,白费。 - *例句*: All my efforts to convince him were in vain. - *语法*: 既...
1. "make a good impression":在面试中留下良好的印象是非常重要的,这通常指的是展示出专业、自信和友好的态度。 2. "disgusting smell":令人作呕的气味,可能是在提示学生理解形容词的用法以及如何在语境中选择...
(explanation) 6. 马克斯的姐姐决心帮他清理乱七八糟的房间。(messy) 7. 有“严重作弊行为”的学生将会受到惩罚。(punishment) 三、用方框内所给短语的适当形式完成下列句子: 1. 你应该明天早上交作业。(be ...
7. **Appendix-A.doc**:通常,附录会包含额外的信息,比如故障排查指南、维护建议、保修政策、兼容性列表或其他补充资料,有助于用户在使用过程中遇到问题时自我解决。 总的来说,这个LG-IV5说明书的压缩包提供了...
5. 派生词错误:如explaination到explanation,inventer到inventor,了解派生词的构成规则。 6. 不规则动词过去分词:如beginning、writed、heared等,记忆不规则变化。 7. 拼写遗漏或多拼:如ordinary、medicine、...
15. 名词形式:explanation - "He wanted to get a satisfactory explanation, but was refused.","explanation"是名词,表示解释。 16. 固定搭配:in a mess - "His room is in a mess and needs to be tidied....
Platform as a Service (PaaS): Provides a platform for developers to build, test, deploy, and manage applications without having to worry about underlying infrastructure. PaaS offerings often include ...
Rajkovic: Knowledge acquisition and explanation for multi-attribute decision making. In 8th Intl Workshop on Expert Systems and their Applications, Avignon, France. pages 59-78, 1988. Within ...
4. **Explanation in Spanish.docx**:这份文档可能是用西班牙语编写的,可能包含了设计的详细解释或者算法的描述,对于理解整个毕业设计的背景和实现过程至关重要。 5. **license.txt**:这通常包含软件的许可协议...
When configuring AWS Config in a region, users can choose to record all supported resources across all services within that region or select specific types of resources to monitor. Additionally, ...
例如:"Thanks to their hard work","Owing to great efforts","Due to a difficult challenge"。 2. 副词的用法:"Therefore" 表示因果关系,意为"因此";"Otherwise" 表示转折,意为"否则";"However" 表示对比...
4. analyze the requirements of Chinese character learning website and provide a detailed explanation of the system's functionality. 本论文的结果表明,基于Java技术开发的汉字学习网站可以满足用户的需求...