{"id":10730,"date":"2021-10-05T08:10:16","date_gmt":"2021-10-05T08:10:16","guid":{"rendered":"https:\/\/linuxways.net\/?p=10730"},"modified":"2021-10-05T08:10:16","modified_gmt":"2021-10-05T08:10:16","slug":"hardlink-and-softlink-in-linux-uses-and-examples","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/hardlink-and-softlink-in-linux-uses-and-examples\/","title":{"rendered":"Hardlink and Softlink in Linux &#8211; Uses and Examples"},"content":{"rendered":"<p>Hard links and softlinks are two different types of links that are very useful on Linux. Here, let\u2019s discuss them with examples.<\/p>\n<h2>Hard Link:<\/h2>\n<p>Hard link shares the same inodes as the original file. It is the mirror copy of the original file. It is only useful for files as it cannot create links for directories. If you change the content to either original or hard link file then changes occur on both. The main fact about hard link is that they do not get deleted even by deleting the original file.<\/p>\n<h2>Soft Link:<\/h2>\n<p>Soft link is also known as symbolic link. It does not share the same inode as the original file. It is the definite link to the original file so if you remove the original file then it also gets removed. It can create links for both files and directories.<\/p>\n<h2>Keys difference between Hard Link and Soft Link<\/h2>\n<table>\n<thead>\n<tr>\n<th>Hard Link<\/th>\n<th>Soft Link<\/th>\n<\/tr>\n<tr>\n<th>Even if the original file gets deleted, it does not get deleted.<\/th>\n<th>If the original file gets deleted, then it will also get deleted.<\/th>\n<\/tr>\n<tr>\n<th>Shares same inode number<\/th>\n<th>Shares different inode number<\/th>\n<\/tr>\n<tr>\n<th>Mirror copy of the original file<\/th>\n<th>Definite link to the original file<\/th>\n<\/tr>\n<tr>\n<th>Can link only files<\/th>\n<th>Can link both files and directories<\/th>\n<\/tr>\n<\/thead>\n<\/table>\n<h2>Use cases of Hard Link on Linux<\/h2>\n<p>Let\u2019s create a file named linuxways.txt and create a hard link to this file.<\/p>\n<pre>$ sudo mkdir hardlink<\/pre>\n<pre>$ cd hardlink<\/pre>\n<pre>$ sudo touch linuxways.txt<\/pre>\n<pre>$ sudo ln linuxways.txt hardlinktest.txt<\/pre>\n<pre>$ls -li<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"648\" height=\"266\" class=\"wp-image-10731\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-709.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-709.png 648w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-709-300x123.png 300w\" sizes=\"auto, (max-width: 648px) 100vw, 648px\" \/><\/p>\n<p>Inode numbers for both hardlinktest.txt and linuxways.txt are the same i.e 393248 and same file permissions (-rw-r&#8211;r&#8211;). Hard link file does not get deleted even if the original file gets deleted.<\/p>\n<pre>$ sudo rm linuxways.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"534\" height=\"24\" class=\"wp-image-10732\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-710.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-710.png 534w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-710-300x13.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-710-501x24.png 501w\" sizes=\"auto, (max-width: 534px) 100vw, 534px\" \/><\/p>\n<p>Hard Link file still exists with the same content.<\/p>\n<pre>$ ls -li<\/pre>\n<pre>$ cat hardlinktest.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"645\" height=\"117\" class=\"wp-image-10733\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-711.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-711.png 645w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-711-300x54.png 300w\" sizes=\"auto, (max-width: 645px) 100vw, 645px\" \/><\/p>\n<p>It proves a hard link file does not get deleted even after deleting the original file. If the hard link file needs to be deleted then you must delete it with the following command:<\/p>\n<pre>$ sudo rm hardlinkfile.txt<\/pre>\n<h3>Use cases of Soft Link on Linux<\/h3>\n<p>Let\u2019s create a file named linuxways.txt and create a soft link to this file.<\/p>\n<pre>$ sudo mkdir softlink<\/pre>\n<pre>$ cd softlink<\/pre>\n<pre>$ sudo touch linuxways.txt<\/pre>\n<pre>$ sudo ln -s linuxways.txt softlinktest.txt<\/pre>\n<pre>$ ls -l<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"721\" height=\"270\" class=\"wp-image-10734\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-712.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-712.png 721w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-712-300x112.png 300w\" sizes=\"auto, (max-width: 721px) 100vw, 721px\" \/><\/p>\n<p>Here, inode numbers are different for the original and soft link file, also you can see different file permissions. If you delete the original file then the soft link file also gets deleted as it is the actual copy of the original file.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"659\" height=\"178\" class=\"wp-image-10735\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-713.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-713.png 659w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-713-300x81.png 300w\" sizes=\"auto, (max-width: 659px) 100vw, 659px\" \/><\/p>\n<h3>Conclusion:<\/h3>\n<p>This is the way you create hard links and soft links also known as symbolic links. You must get the basic difference between them and the process of creating such links now. Thank you!<\/p>","protected":false},"excerpt":{"rendered":"<p>Hard links and softlinks are two different types of links that are very useful on Linux. Here, let\u2019s discuss them with examples. Hard Link: Hard link shares the&hellip;<\/p>","protected":false},"author":1,"featured_media":10923,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,5,83,165,2],"tags":[585,35,586],"class_list":["post-10730","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-debian","category-mint","category-opensuse","category-red-hat","category-ubuntu","tag-hardlink","tag-linux","tag-softlink"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/10730","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=10730"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/10730\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/10923"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=10730"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=10730"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=10730"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}