Thibaud explains… Bash#2 — hard links vs symlinks

Hard links vs Symbolic links. Who’s never wondered about this question at least once? How’s that? Everyone, you say? Nonsense!

The files stored on this poor NAS drive won’t be available until further notice.

The Linux File System

The concept of files is pretty much obvious for most of the people who will read this article. They’re buckets of information, stored on a physical media (either spinning or nor spinning), and mounted in a specific place on the linux file system.

Quick recap about the Linux FS: Files are uniquely attributed an inode, which is an identification number that leads to the exact specific part of the storage medium where the actual bits are written. This inode (and physical copy of the file) contain everything except the file name and its path on the file system.

Hard links

So we’ve got a file with no name and no path to be found in. That’s the job for the hard link! The hard link… links (duh!) a name and a place to an inode. Boom, your file is available!

You can create mutliple hard links for a file, that will indistinguishably lead to the same inode and the same content. Deleting one of those file won’t affect the other hard links, since, in essence, they’re the same file. You can think of it as someone with multiple telephones in his pocket. If that guy turns one of his phones off, you can still call him if you know his other numbers.

Symbolic links

On the contrary, symbolic (or soft) links, function more like shortcuts, or aliases, to an original file. They don’t link to the individual file inode, they link to the hard link of the file itself. Delete the original file, and the symbolic links now points to somthing that no longer exists. In our previous metaphor, it would be like our friend having multiple phone numbers all redirecting to his one cell phone. If his battery dies, you won’t be able to reach him, no matter which number you try, since they’re all “virtual” shortcuts for his single phone. If his phone’s dead, then you’re out of luck.

Student at Holberton School France