Sunday, April 28, 2024
 Popular · Latest · Hot · Upcoming
115
rated 0 times [  115] [ 0]  / answers: 1 / hits: 11680  / 3 Years ago, tue, july 27, 2021, 9:57:13

Wikipedia defines a hard link as:




a directory entry that associates a name with a file on a file system. (A directory is itself a special kind of file that contains a list of such entries.) The term is used in file systems which allow multiple hard links to be created for the same file.




I am wondering if the concept of hard link is equivalent to the Windows concept of Shortcut.



If hard links are not equivalent to shortcuts, then what's the closest Windows feature to hard links?


More From » windows

 Answers
1

No, a hard link is completely different. A soft link is closer to a Windows shortcut (though there are important differences, symbolic links are more similar to windows shortcuts than hard links are). A hard link is a different thing and one you will almost never need.



Briefly, a soft link is created with this command:



ln -s foo bar


If you then run ls -l, you will see:



lrwxrwxrwx 1 terdon terdon 3 Mar 10 15:58 bar -> foo
-rw-r--r-- 2 terdon terdon 0 Mar 10 15:58 foo


The -> means that bar is a link to foo. So, opening bar, with a text editor for example, will actually open the separate file foo. However, deleting bar will just delete the shortcut, it will not affect the file foo.



Hard links, on the other hand, are created with this command:



ln foo bar


If you now run ls -l, there is no indication of any relationship between the files:



-rw-r--r-- 2 terdon terdon 0 Mar 10 15:58 bar
-rw-r--r-- 2 terdon terdon 0 Mar 10 15:58 foo


But—and this is very important—those are actually the same file. Files on Unix file systems are stored using inodes; an inode is basically the way the filesystem maps a file name to a particular location on the physical hard drive. So, hard links are files that point to the same inode as their target. Another way of putting this is that all files are actually hard links pointing to their inodes. Making a hard link to a file just creates a new pointer (file) on the file system that points to the same inode. Each inode can have multiple files pointing to it or one, or none.



To understand this more clearly, use ls -i which shows the inode associated with a file. Let's create a soft link and a hard link and see what happens:



ln -s foo SoftLinkToFoo
ln foo HardLinkToFoo


Now, check their inodes:



enter image description here



As you can see above, both foo and HardLinkToFoo have the same inode (16648029) while SoftLinkToFoo has a different one (16648036).



What happens if we rename foo with mv foo bar?



enter image description here



The red color indicates a broken soft link, one whose target can no longer be found. This is because soft links point to a file's name, not its inode. Note that despite changing the name, the inode remains the same so the hardlink is fine, it still works.



In summary, hard links are actually two manifestations of the same file; they are pointers to the same section of the disk. Soft links are just shortcuts. To take a real world analogy, hardlinks are like two different phone numbers for the same phone line and soft links are like having two different phone lines in the same house.


[#26587] Thursday, July 29, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nquirewha

Total Points: 256
Total Questions: 109
Total Answers: 122

Location: Namibia
Member since Mon, Feb 21, 2022
2 Years ago
;