Filesystems Hard Link – Why Are Hard Links Only Valid Within the Same Filesystem?

filesystemshard link

I am reading this intro to the command line by Mark Bates.

In the first chapter, he mentions that hard links cannot span file systems.

An important thing to note about hard links is that they only work on the current file system. You can not create a hard link to a file on a different file system. To do that you need to use symbolic links, Section 1.4.3.

I only know of one filesystem. The one starting from root (/). This statement that hard links cannot span over file systems doesn't make sense to me.

The Wikipedia article on Unix file systems is not helpful either.

Best Answer

Hopefully I can answer this in a way that makes sense for you. A file system in Linux, is generally made up of a partition that is formatted in one of various ways (gotta love choice!) that you store your files on. Be that your system files, or your personal files... they are all stored on a file system. This part you seem to understand.

But what if you partition your hard drive to have more than one partition (think Apple Pie cut up into pieces), or add an additional hard drive (perhaps a USB stick?). For the sake of argument, they all have file systems on them as well.

When you look at the files on your computer, you're seeing a visual representation of data on your partition's file system. Each file name corresponds to what is called an inode, which is where your data, behind the scenes, really lives. A hard link lets you have multiple "file names" (for lack of a better description) that point to the same inode. This only works if those hard links are on the same file system. A symbolic link instead points to the "file name", which then is linked to the inode holding your data. Forgive my crude artwork but hopefully this explains better.

image.jpg             image2.jpg
          \           /
           [your data]

here, image.jpg, and image2.jpg both point directly to your data. They are both hardlinks. However...

image.jpg    <-----------  image2.jpg
           \ 
             [your data]

In this (crude) example, image2.jpg doesn't point to your data, it points to the image.jpg... which is a link to your data.

Symbolic links can work across file system boundaries (assuming that file system is attached and mounted, like your usb stick). However a hard link cannot. It knows nothing about what is on your other file system, or where your data there is stored.

Hopefully this helps make better sense.

Related Question