`

Softlinks vs. Hardlinks: A Quick Explanation

 
阅读更多

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.

What is a softlink?

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:

ln -s [target] [link name]

For example, the following command will create a softlink to /usr/bin/firefox called firefox (in my “Desktop” directory):

ln -s /usr/bin/firefox ~/Desktop/firefox

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.

What is a hardlink?

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:

stat /path/to/file

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:

When to use softlinks

There are two major limitations of hardlinks. In these cases, you must use a softlink:

  1. A link across filesystems
  2. Because a hardlink is a direct reference to the underlying filesystem, you can’t hardlink across filesystems.

  3. Linking to a directory
  4. 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:

  1. Performance
  2. 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.

  3. Storage space
  4. 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.

    Was this helpful?

    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!

分享到:
评论

相关推荐

    30天打造更亲和力网站.rar

    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.....................

    70-642 TS: Windows Server 2008 Network Infrastructure, Configuring

    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:

    Test-Driven+Development+By+Example-测试驱动开发【高清+有目录+可搜索+可编辑】

    1.敏捷开发强调测试先行,因为先编写测试代码,就可以有目的的预防某些bug的产生,要知道,治病不如预防!!!所以我个人觉得这个还是比较重要的,文件也不大,只有8M,但是高清。 2.本版本与网上其他资源不同之处...

    Boosting the margin—— a new explanation for the effectiveness of voting methods.pdf

    《Boosting the Margin: A New Explanation for the Effectiveness of Voting Methods》是一篇由Robert E. Schapire、Yoav Freund、Peter Bartlett和Wee Sun Lee共同撰写的论文。该论文深入探讨了在机器学习领域中一...

    广东署山市顺德区均安中学高中英语Unit15Learning重点词和词组导学案无答案北师大版必修5

    17. "explain":动词,表示“解释”,名词为"explanation",如“give an explanation”。 18. "spit":动词,指“吐口水,吐痰”。 19. "add":动词,意为“增加”,名词形式为"addition",形容词为"additional",...

    JProfiler Helper

    ### JProfiler Helper: A Comprehensive Guide to Utilizing JProfiler for Performance Monitoring #### Introduction JProfiler is an advanced performance monitoring and profiling tool designed for Java ...

    OKI 日冲C8600dn用户手册.pdf

    2. 符号notation explanation:在手册中使用了多种符号notation,例如 •、 等,用于区分不同的操作系统和打印机模型。 3. 打印机安装指南:手册提供了详细的打印机安装指南,包括如何安装打印机驱动程序、如何...

    《最新四级词汇汇总》(高清晰)pdf版

    - *例句*: Her explanation was vague and difficult to understand. - *语法*: 形容词,表示不清楚。 37. **vain** [n.]: 徒劳,白费。 - *例句*: All my efforts to convince him were in vain. - *语法*: 既...

    21春北京理工大学《大学英语(3)》在线作业参考答案.docx

    1. "make a good impression":在面试中留下良好的印象是非常重要的,这通常指的是展示出专业、自信和友好的态度。 2. "disgusting smell":令人作呕的气味,可能是在提示学生理解形容词的用法以及如何在语境中选择...

    高中英语模块1 Unit 2 词汇1练习题学生版.doc

    (explanation) 6. 马克斯的姐姐决心帮他清理乱七八糟的房间。(messy) 7. 有“严重作弊行为”的学生将会受到惩罚。(punishment) 三、用方框内所给短语的适当形式完成下列句子: 1. 你应该明天早上交作业。(be ...

    LG-IV5说明书.rar

    7. **Appendix-A.doc**:通常,附录会包含额外的信息,比如故障排查指南、维护建议、保修政策、兼容性列表或其他补充资料,有助于用户在使用过程中遇到问题时自我解决。 总的来说,这个LG-IV5说明书的压缩包提供了...

    探索中考英语三轮复习策略方案.ppt

    5. 派生词错误:如explaination到explanation,inventer到inventor,了解派生词的构成规则。 6. 不规则动词过去分词:如beginning、writed、heared等,记忆不规则变化。 7. 拼写遗漏或多拼:如ordinary、medicine、...

    新课标2021高考英语一轮复习课时作业184_3AtasteofEnglishhumour新人教版必修4

    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....

    AZ-900.136Q.Pdf

    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 ...

    UCIcar.zip

    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 ...

    毕业设计MATLAB_用于求解线性方程的非对称组.zip

    4. **Explanation in Spanish.docx**:这份文档可能是用西班牙语编写的,可能包含了设计的详细解释或者算法的描述,对于理解整个毕业设计的背景和实现过程至关重要。 5. **license.txt**:这通常包含软件的许可协议...

    AWS.CLF.325Q.pdf

    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, ...

    2014届高三英语大一轮复习 Book 4 Unit 2 Working the land单元知识对点练(含解析) 新人教版

    例如:"Thanks to their hard work","Owing to great efforts","Due to a difficult challenge"。 2. 副词的用法:"Therefore" 表示因果关系,意为"因此";"Otherwise" 表示转折,意为"否则";"However" 表示对比...

    java汉字学习网站论文.docx

    4. analyze the requirements of Chinese character learning website and provide a detailed explanation of the system's functionality. 本论文的结果表明,基于Java技术开发的汉字学习网站可以满足用户的需求...

Global site tag (gtag.js) - Google Analytics