A A
RSS

Softlinks vs. Hardlinks: A Quick Explanation

Sun, Sep 26, 2010

Tweet this!

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!

Like this post?

37 Responses to “Softlinks vs. Hardlinks: A Quick Explanation”

  1. Duane says:

    Great explanation! You answered all of my immediate questions about hard/soft links.

  2. Nicu says:

    Thank you !
    Now I understand hardlinks much better.

    • Jonathan DePrizio says:

      I’m glad you found it useful, Nicu! I hope you find other content on TechThrob to be as informative. Thanks for visiting!

  3. Sidharth says:

    Nicely explained and diagrammed explanation. What would be some of the advantages of Hardlinks? You talked about their limitations. Are some advantages, apart from, I guess, performance?

    • Jonathan DePrizio says:

      Hi Sidharth,

      I’ve added some information about the advantages of hardlinks. The primary advantages are performance and space saving on disk.

      Thanks for reading and thanks for your comment!

  4. Mike says:

    So the softlink points to the original file and the hardlink points directly to de data on the disk.

    But where is the difference in using soft or hard links?
    In the end you are editing the same file, correct?

    • Jonathan DePrizio says:

      Hi Mike,

      I’ve expanded the section concerning when using a hardlink is more advantageous than using a softlink, and vice-versa. I hope you find the information useful.

      Thanks for reading!

  5. Stuart says:

    While this is an interesting article that clearly states the differences between soft and hard linking, I think it lacks certain details that would make it truly useful – for example, how hardlink deletions are handled, providing pros/cons as to when soft and hard linking should be used and why – hardlinking, speed, deleting multiple linked source still leaves other hardlinks available, less disk space used etc: softlinking, works across file systems, can link to directories, increased disk usage etc.

    With so many useful soft/hardlink articles out there already, it’s difficult to see why anyone needed a new one that lacks key information such as this.

    • Jonathan DePrizio says:

      Hi Stuart,

      Thank you for reading and for leaving your comment. I’ve went back and strengthened the section of the article you mentioned, adding information where appropriate.

      Again, thank you for leaving the comment. I hope you find your way back to TechThrob in the future!

      Thanks,
      Jonathan

  6. fstephens says:

    Good explanation, I was never clear on the advantages of hard links.
    Thanks!

  7. Uwe Brauer says:

    Version control system.

    I think it is worth pointing out that hard and softlinks
    behave differently if the original file is under version control (RCS for example).
    A softlink will be respected by the version control system
    while a hardlink will not. That is changes you make in the hardlink cannnot be checked in!

    Uwe Brauer

  8. fortech says:

    Great article.
    It seems to me its got everything in there to help one decide if she/he should better use a soft or an hardlink.
    Thanks for the good work.

  9. naveen says:

    super!!!

    simple but full of info..

    thanks!!!!

  10. Evan says:

    Late feedback:

    Thanks. A timely and helpful article.

    1) Would explain upfront that ‘symlink’ is another name for softlink, and perhaps why there are two names. The terminal screenshot uses the term ‘symlink’, so could be confusing for us newbies.

    2) A hardlink reference must take up some space somewhere, if not as a file. How does that work?

    3) Is there ever any risk of deleting the actual data, when deleting either a soft or hard link?

    • Jonathan DePrizio says:

      Hi Evan, thanks for the feedback!

      For your first point: thanks! I will modify the article to point this out. Good point!

      Softlinks are brand new, individual files, so they take up the minimum space that a new file would take (usually 4KB). On the other hand, hardlinks are just filesystem metadata, and don’t take up additional space. Hardlinks aren’t new files, they are just another record in the filesystem saying that “filename ‘somelink’ points to this already-used location on disk.”)

      For your third question, whether there is a risk of deleting the original data: generally no. When you delete a softlink, the original file is preserved. When you delete a hardlink (as shown in one of the screenshots), the reference counter is decremented. If you delete the hardlink that was the last pointer to the data, then yes, it will be deleted.

      However, there is a risk of overwriting the data, of course. For example, if you use a secure deletion tool that opens the file and writes over the contents, then it will delete the original data. Good question!

      Hope that answered your questions. If not, or if you have other questions, please ask!

      Thanks again.

      • Evan says:

        Really appreciate your politeness, responsiveness, and willingness to adjust your article. It seems quite complete for me now, and I’m sure will help a lot of people on an ongoing basis. These kinds of precise explanations (with pictures) of what experienced users take for granted, are very helpful to new comers to Linux. IMHO Thanks again.

        • Jonathan DePrizio says:

          Thanks for reading and commenting Evan. I’m glad that you found the information useful, and I’m really glad that you commented. I really appreciate your asking questions, since it helps to clarify information for not only you but for other readers.

          Hope you come back soon!

      • Fred says:

        How can a hard link not take up disk space? Obviously, the name of the link and the value of inode number need bits to store, and those bits will add to the length of the directory wherein the hard link lives, which is stored on the hard disk. Certainly, a hard link is stored much more efficiently than a soft link, but it still needs space.

        As for secure deletion, I just tried the srm command (secure remove) on a file with multiple hard links and on a soft link. For the file with multiple hard links, it deleted the hard link I gave it and also warned me that the file remained intact under its remaining link(s). For the soft link, it just deleted the link without warning me that the target file was left unwiped and untouched.

  11. zedtux says:

    I had no idea that soft and hard link exists… and now it’s something I will try to use !

    Thanks for your post !

  12. Great! I did not understand much about it.

  13. thudfoo says:

    At least through Windows XP, I recall that a Windows Shortcut is used to run an executable file, specifing the path, arguments, working directory, and is not an alias in the sense that a softlink is.

    • Jonathan DePrizio says:

      Hi thudfoo,

      At least in Windows 7, you can create shortcuts to anything, whether it be an executable or some other type of file (document, image, etc…). I’m not sure about XP, but I think it’s the same thing.

      Thanks for reading and commenting!
      Jonathan

  14. Thank you. Simply explained. Cleared up many years of misunderstanding. Greetings from Culcheth, England.

  15. Ram says:

    Thakns I understood very much

  16. Acho says:

    Hello:

    Which is the difference between:

    ln -s mydirectory myshorcut

    and a script (executable) with:

    cd mydirectory
    ?

    Thanks!

  17. JMo says:

    Great explanation and diagrams, I understand it this better than other sites I’ve been to.

    Can you explain the short links in terms of whether they will work if they were in the same file system?

  18. ivan90112 says:

    Thank you! A very useful explanation.

    If I right-click and select “Make Link”, does that mean I create a hardlink?

  19. Pragadeesh Sekar says:

    thank you
    this article helps me in a better way to know the links available in unix.

  20. mayautobot says:

    Marvellous article, explaining about links.

  21. Srinivas says:

    Hi

    can somebody tell me why we wont be able to create hard links to folders? what is the exact technical reason behind it?

    Srinivas

  22. sai says:

    Very good explanation…..

  23. Ragesh Hajela says:

    Very Good Explanation.

  24. Kirk says:

    good explanation, great diagram there. i think you should definitely add a bit about what happens when you delete the file linked by symlinks vs hardlinks. that is an interesting contrast that most students need to know about.
    Thanks again!
    -Kirk

  25. nitin says:

    very nice explanation………

    thanks.

  26. krishna says:

    Very helpful!!! Thank you.

  27. madhur says:

    nicely explained

  28. Amoor says:

    great explanation!

Leave a Reply

Weekly Poll

What's the best Linux distribution for desktops?

View Results

Loading ... Loading ...

Search TechThrob

Advertisement