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.pdf,比较好的一篇文章,大家需要的可以看看
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 ...
Title: Software Testing: A Craftsman’s Approach, 4th Edition Author: Paul C. Jorgensen Length: 494 pages Edition: 4 Language: English Publisher: Auerbach Publications Publication Date: 2013-10-18 ...
NO.1 The botnet report displays a confidence score of 1 to 5 indicating the likelihood of a botnet infection. Which three sources are used by the firewall as the basis of this score? (Choose three.) A...
Code explanation.rar
a technical explanation of every option for every tool in every situation, but rather a concise explanation of those parts of Photoshop you’re most likely to need. If you happen to be a medical ...
Quick Reference ; ;;;;;;;;;;;;;;;;;;; ; The following are all the settings which are different in either the production ; or development versions of the INIs with respect to PHP's default behavior. ;...
Try the following: Refresh page: Search for the page again by clicking the Refresh button...Access from a link: If there is a link to the page you are looking for, try accessing the page from that link.
dcc explanation: you are trying to return a pointer to the local variable 'counter'. You can not do this because counter will not exist after the function returns. See more informa
│ directory or file explanation │ └────────────────────────────────────────────────────┘ ppl.cv ├── .vscode Visual code settings ├── ...
Attention is not Explanation.pdf ACL2019论文原文 注意力可否作为模型的可解释性
Explanation: The answer is "abc", with the length of 3. 示例 2: Input: "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. 示例 3: Input: "pwwkew" Output: 3 Explanation: The ...
We will explore a case study on packaging and shipping an LLVM toolchain with CMake including an in-depth explanation of many of the new features of the LLVM CMake build system. Lastly it will ...
Attention is not not Explanation.pdf ACL2019论文原文,反驳论文《Attention is not Explanation》的论文
There is currently no single work thatprovides an objective, technical explanation of virtualization technologies.This book examines new storage virtualization technologies that automate datastorage ...
Please refer to the book for a full explanation of the implementation. Comments within the code also provide additional information. The state machine language uses a few custom keywords that ...
VoIPDemo is intended as a sample to show how to use the RTC API for Windows CE, but also as a working application for demonstration and use (taking note of the limitations described below). ...