What is a hard link in Linux?

9

1

I have searched around and am trying to understand the difference between a hard link and symbolic link (soft link).

I found this link is quite useful. But I am still not very clear. I understand soft link is not a copy of original file, but is a hard link a copy or not?

icn

Posted 2011-06-20T03:36:39.147

Reputation: 375

Answers

15

To use an analogy from mail delivery, a symbolic link is something like a forwarding address... when something tries to open a symbolic link, it opens the "file" (not literally a file, though) stored there, and sees that it should instead look at a file with a different name, so it opens the other file instead.

A hard link is more like having two addresses for the same place. (Of course this isn't really possible in the physical world). When something tries to read either file name (address), they get the same physical file (location).

So a hard link is not a copy, because the file is only stored once (but with multiple names). But it behaves very much like a copy, because you can access the same information from two file names.

Flimzy

Posted 2011-06-20T03:36:39.147

Reputation: 4 168

+1 for a very nice analogy, which would have simplified things for me when I was trying to get my head around this. – John Gardeniers – 2011-06-20T04:17:53.260

1+1, but two minor details: 1) It is possible to have multiple addresses for the same place in the real world, just not common. 2) Perhaps the most important difference between a hard link and a copy is that, if you have /foo and /bar, changing /foo will also change /bar if they're hardlinks (because they're two names for the same file), but not if they're copies (because they're two completely independent files). – Dave Sherohman – 2011-06-20T10:09:24.797

3

No. A (second and all subsequent) hard link to a file is a pointer to the same inodes on disk.

Ignacio Vazquez-Abrams

Posted 2011-06-20T03:36:39.147

Reputation: 100 516

3

When you make a hard link, it means the same file shows up in two different places. A file is really only a reference to a set of data blocks on disks, so a hard link adds another reference. Files are rarely truly deleted; the references are just removed so it's impossible to access the data. That's why when you delete a file you made as a hard link, the other original file stays, and vice versa.

On the other hand, a symlink is a direction to access a file with a certain name. It tells any programs, "If you want to use this file, it's over there". This is why if you delete the target of the symlink, the symlink breaks. And if you delete the link, the target is unaffected.

Michael Lowman

Posted 2011-06-20T03:36:39.147

Reputation: 768

2

A file in *nix system has two parts. one the data portion and other is inode. inode stores meta (information relevant to the data like location where in HDD it is stored) information. The hard link makes creates a exact copy of this inode. While sym link creates a inode which points to the inode of the actual inode. So inode data in hardlink is same as target but different data in sym link. So to put the long story short, sym links and hard links differ by their content of inode data not the file data. This is my understanding of difference between sym link and hard link. I had a tough time initially understanding this as this is something more developer thingy and i am not a developer. Oh knowledgeable ones, please correct me if i am wrong.

bagavadhar

Posted 2011-06-20T03:36:39.147

Reputation: 275

3A "directory" in *nix is a list of file names and inode numbers. Hard links are two directory entries that have the same inode number, and hence refer to the exact same inode, not to copies of the inode. A symlink is a directory entry that points to another directory entry (by name), not to any particular inode. (Ok, it actually has an inode, but that inode refers to the directory entry, not to file data.) – Randy Orrison – 2011-06-20T09:07:09.247