Filesystems – Difference Between a Hard Link and a File

filesfilesystemshard linksymlink

A hard link is defined as a pointer to an inode. A soft link, also known as a symbolic link, is defined as an independent file pointing to another link without the restrictions of hard links.

What is the difference between a file and a hard link? A hard link points to an inode, so what is a file? The inode entry itself? Or an inode with a hard link?

Let's say I create a file with touch. Then an inode entry is created in the inode table. And I create a hard link, which has the same inode number as the file. So did I create a new file? Or is the file just defined as an inode?

Best Answer

The very short answer is:

  • a file is an anonymous blob of data
  • a hardlink is a name for a file
  • a symbolic link is a special file whose content is a pathname

Unix files and directories work exactly like files and directories in the real world (and not like folders in the real world); Unix filesystems are (conceptually) structured like this:

  • a file is an anonymous blob of data; it doesn't have a name, only a number (inode)
  • a directory is a special kind of file which contains a mapping of names to files (more specifically inodes); since a directory is just a file, directories can have entries for directories, that's how recursion is implemented (note that when Unix filesystems were introduced, this was not at all obvious, a lot of operating systems didn't allow directories to contain directories back then)
  • these directory entries are called hardlinks
  • a symbolic link is another special kind of file, whose content is a pathname; this pathname is interpreted as the name of another file
  • other kinds of special files are: sockets, fifos, block devices, character devices

Keeping this metaphor in mind, and specifically keeping in mind that Unix directories work like real-world directories and not like real-world folders explains many of the "oddities" that newcomers often encounter, like: why can I delete a file I don't have write access to? Well, for one, you're not deleting the file, you are deleting one of many possible names for the file, and in order to do that, you only need write access to the directory, not the file. Just like in the real world.

Or, why can I have dangling symlinks? Well, the symlink simply contains a pathname. There is nothing that says that there actually has to be a file with that name.

My question is simply what is the difference of a file and a hard link ?

The difference between a file and a hard link is the same as the difference between you and the line with your name in the phone book.

Hard link is pointing to an inode, so what is a file ? Inode entry itself ? Or an Inode with a hard link ?

A file is an anonymous piece of data. That's it. A file is not an inode, a file has an inode, just like you are not a Social Security Number, you have a SSN.

A hard link is a name for a file. A file can have many names.

Let's say, I create a file with touch, then an Inode entry is created in the Inode Table.

Yes.

And I create a hard link, which has the same Inode number with the file.

No. A hard link doesn't have an inode number, since it's not a file. Only files have inode numbers.

The hardlink associates a name with an inode number.

So did I create a new file ?

Yes.

Or the file is just defined as an Inode ?

No. The file has an inode, it isn't an inode.

Related Question