{"id":1233,"date":"2010-09-28T12:13:58","date_gmt":"2010-09-28T05:13:58","guid":{"rendered":"http:\/\/www.jfdesignnet.com\/?p=1233"},"modified":"2010-09-28T12:13:58","modified_gmt":"2010-09-28T05:13:58","slug":"softlinks-vs-hardlinks","status":"publish","type":"post","link":"https:\/\/www.jfdesignnet.com\/?p=1233","title":{"rendered":"Softlinks vs Hardlinks"},"content":{"rendered":"<p style=\"text-align: justify;\">Many times I have to explain this two kind of file links in linux, <strong>softlinks<\/strong> and <strong>hardlinks<\/strong> over and over again, and I realized it is something many people might be unfamiliar with, especially users migration from Winblows. I think it would be nice if I explain this here and just give the link for those who need. Here, I\u2019ll give a quick explanation of what these types of links are, and when you should use them.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>What is Softlink ?<\/strong><\/span><\/p>\n<p style=\"text-align: justify;\">Softlinks are the easiest to understand, especially because you\u2019ve  probably already used them.  A softlink fills the same role as a Winblows  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.<\/p>\n<p style=\"text-align: justify;\">All links are created with the <code>ln<\/code> command, using the <code>-s<\/code> flag if you want a softlink (if you don\u2019t use <code>-s<\/code>, you\u2019ll get a hardlink, which I\u2019ll talk about in a second).  The syntax of the command is :<\/p>\n<p style=\"text-align: justify;\">{code}ln -s [target] [link name]{\/code}<\/p>\n<p style=\"text-align: justify;\">For example, the following command will create a softlink to \/usr\/bin\/dolphin called dolphin (in my \u201cDesktop\u201d directory) :<\/p>\n<p style=\"text-align: justify;\">{code}ln -s \/usr\/bin\/dolphin ~\/Desktop\/dolphin{\/code}<\/p>\n<p style=\"text-align: justify;\">You can see the softlink\u2019s target by using the <code>ls -l<\/code> command.  You can also use this command to detect broken softlinks :<\/p>\n<p style=\"text-align: left;\"><a href=\"http:\/\/www.jfdesignnet.com\/wp-content\/uploads\/2010\/09\/softlink-examples.jpg\" rel=\"lightbox[1233]\"><img decoding=\"async\" loading=\"lazy\" class=\"size-medium wp-image-1234 aligncenter\" title=\"softlink-examples\" src=\"http:\/\/www.jfdesignnet.com\/wp-content\/uploads\/2010\/09\/softlink-examples-300x85.jpg\" alt=\"\" width=\"300\" height=\"85\" \/><\/a><br \/>\nNow that we understand softlinks, let\u2019s talk about hardlinks.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>What is Hardlink ?<\/strong><\/span><\/p>\n<p style=\"text-align: justify;\">Whereas a softlink is a new file that points to an already-existing file, a hardlink is <em>another instance of the original file<\/em>.  A diagram is the easiest way to explain what that means :<\/p>\n<p style=\"text-align: justify;\"><a href=\"http:\/\/www.jfdesignnet.com\/wp-content\/uploads\/2010\/09\/softlink.png\" rel=\"lightbox[1233]\"><img decoding=\"async\" loading=\"lazy\" class=\"size-medium wp-image-1241 alignnone\" title=\"softlink\" src=\"http:\/\/www.jfdesignnet.com\/wp-content\/uploads\/2010\/09\/softlink-231x300.png\" alt=\"\" width=\"231\" height=\"300\" \/><\/a><a href=\"http:\/\/www.jfdesignnet.com\/wp-content\/uploads\/2010\/09\/hardlink.png\" rel=\"lightbox[1233]\"><img decoding=\"async\" loading=\"lazy\" class=\"size-medium wp-image-1242 alignnone\" title=\"hardlink\" src=\"http:\/\/www.jfdesignnet.com\/wp-content\/uploads\/2010\/09\/hardlink-231x300.png\" alt=\"\" width=\"231\" height=\"300\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">\n<p style=\"text-align: justify;\">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.<\/p>\n<p style=\"text-align: justify;\">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\u2019t 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 \u2018stat\u2019 command :<\/p>\n<p>{code}stat \/path\/to\/file{\/code}<\/p>\n<p style=\"text-align: justify;\">So for example, in this screenshot I\u2019ve created a file and then built  three hardlinks to it.  When I run the \u2018stat\u2019 command on any of the  files, it will show that there is a reference count of 4 :<\/p>\n<p><a href=\"http:\/\/www.jfdesignnet.com\/wp-content\/uploads\/2010\/09\/hardlink-examples.jpg\" rel=\"lightbox[1233]\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-medium wp-image-1238\" title=\"hardlink-examples\" src=\"http:\/\/www.jfdesignnet.com\/wp-content\/uploads\/2010\/09\/hardlink-examples-300x199.jpg\" alt=\"\" width=\"300\" height=\"199\" \/><\/a>I think you now understand what the hardlink is.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>When to use Softlink ?<\/strong><\/span><\/p>\n<p>There are two major limitations of hardlinks.  In these cases, you <em>must<\/em> use a softlink :<\/p>\n<p><strong>A link across filesystems<\/strong><br \/>\nBecause a hardlink is a direct reference to the underlying filesystem, you can\u2019t hardlink across filesystems.<\/p>\n<p><strong>Linking to a directory<\/strong><br \/>\nYou can\u2019t use a hardlink to link to a directory.<\/p>\n<p style=\"text-align: justify;\">In the cases listed above, you <em>must<\/em> use a softlink because a  hardlink simply won\u2019t work.  There are also cases where softlinks are  preferable to hardlinks, even though either will work.  For example,  you\u2019d probably want to pick a softlink when you want to create a <em>shortcut<\/em> 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.<\/p>\n<p style=\"text-align: justify;\">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 :<\/p>\n<p><strong>1. Performance<\/strong><\/p>\n<p style=\"text-align: justify;\">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.<\/p>\n<p><strong>2. Storage space<\/strong><\/p>\n<p style=\"text-align: justify;\">Hardlinks don\u2019t 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.<\/p>\n<p style=\"text-align: justify;\">Additionally, hardlinks are preferable because a softlink is  vulnerable to losing the \u2018master\u2019 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.<\/p>\n<p>I hope this information was helpful and easy to understand.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many times I have to explain this two kind of file links in linux, softlinks and hardlinks over and over again, and I realized it is something many people might be unfamiliar with, especially users migration from Winblows. I think it would be nice if I explain this here and just give the link for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1238,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[110,224],"_links":{"self":[{"href":"https:\/\/www.jfdesignnet.com\/index.php?rest_route=\/wp\/v2\/posts\/1233"}],"collection":[{"href":"https:\/\/www.jfdesignnet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jfdesignnet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jfdesignnet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jfdesignnet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1233"}],"version-history":[{"count":0,"href":"https:\/\/www.jfdesignnet.com\/index.php?rest_route=\/wp\/v2\/posts\/1233\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jfdesignnet.com\/index.php?rest_route=\/wp\/v2\/media\/1238"}],"wp:attachment":[{"href":"https:\/\/www.jfdesignnet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jfdesignnet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jfdesignnet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}