Symbolic Links vs Hard Links in Linux
Linux offers various methods for referencing files and directories through other files and directories, without creating copies of the original. If you have color coding...

Linux offers various methods for referencing files and directories through other files and directories, without creating copies of the original. If you have color coding enabled in your bash terminal, you might have seen some folders full of files of a different color from regular files. There’s a good chance that these are all symbolic links, where they’re not the actual files, but merely pointers to another file. This allows the collation of lots of references to files from disparate parts of the system into a single folder for organizational purposes. The other alternative is hard links.
Hard links can seem a bit more niche, and most tutorials focus on symbolic links as the solution to everything, so it may seem that hard links are more taboo. But they have their legitimate uses, and I’ll explore them here.
#Symbolic Links
Symbolic links are files that look like regular files, but are actually pointers to another file. They’re not the target file itself, which means you can safely delete them without impacting the original file. This is in contrast to hard links, which can have consequences for file deletion. You can also create symbolic links to directories – something you can’t do with hard links.
Let’s look at an example of creating a symbolic link. In this example, I’ve already created a new directory and a test file called “original_file.txt”:

Original File Contents
Now we’re going to create a symbolic link to this file. We use the following command:
ln -s original_file.txt my_symlink.txt
This creates a symlink called “my_symlink.txt” to a file called “original_file.txt”. Note that I haven’t provided the full path to “original_file.txt”, that’s why this is a relative symlink. More on that below. The output of the above command is as follows:

Symlink Created
You can see that we now have a new file called “my_symlink.txt”. We can now reference the original file through this symlink. Let’s say I want to add a new line to the original file. I can use:
echo "A second line" >> my_symlink.txt
And this is the output:

Adding Content through the Symlink
So when we access the symlink, we’re accessing the original file.
Dangers of Relative Links – Can’t Move Them!
While creating the above symlink, we used:
ln -s original_file.txt my_symlink.txt
Note that we didn’t specify the full path of the target file. If we had, the command would have been:
ln -s /home/wptweaksnet/symlink_demo/original_file.txt my_symlink.txt
But because we used a relative path – just “original_file.txt” – the symlink will work only when the path relative to the symlink works. If we move the link to another directory where the path doesn’t resolve, the link breaks. Here’s an example:
In this example, I’ve created a new directory called “sub” and moved the earlier symlink to it. But when I try and access it, I get a “No such file or directory” message. And when we list the contents of the directory, we see why. It’s not that the symlink doesn’t exist – as you can see, it does. But the target to which it links doesn’t exist anymore because it was specified as a relative, and not an absolute path. So the symlink points to a target that it expects to find it its own directory.
Now let’s do this again. Instead, we’ll use:
ln -s /home/wptweaksnet/symlink_demo/original_file.txt my_symlink.txt
And repeat the experiment. Here’s what happens:

Recreating with an Absolute Link
In this modified example, I’ve created the same link – but this time I’ve used the full path of the “original_file.txt”, instead of a relative link. Now, when I transfer the symlink to the subdirectory, I’m still able to access the original file through the symlink because the absolute path is reachable from anywhere on the system.
#Hard Links
In contrast to symbolic links, hard links are not pointers to a file’s path name. Rather, they are a pointer to the file itself. Or more specifically, it’s “inode”. An inode is an entry in the OS that essentially is the file. So when you create a pointer to the inode, it doesn’t matter if you move the pointer around – it still points to the same file.
To see the inode numbers, we use a command like this:
ls -i
This gives the following output:

iNode Numbers
Here you can see that the file “original_file.txt” has an inode number of 50996444 and the symbolic link “my_symlink.txt” has the inode number of 50918203. They’re different. Now let’s create a hard link like this:
ln original_file.txt my_hardlink.txt
Note that the only difference between creating a hard link and a symbolic link is the “-s” flag, which you need to use for symbolic links. Here’s the output, followed by the “ls -i” command again:

Hardlinks have the Same iNode Number
We now see that the hard link “my_hardlink.txt” has the same inode number as “original_file.txt”. You can also see that, unlike the symbolic link, it’s not displayed in a separate color. Instead, it looks just like a regular file. That’s because it is a regular file! A hardlink is indistinguishable from the original.
Deleting a File with Hard Links
One consequence of creating a hard link is that you can no longer delete a file unless you delete all the hard links (including the original). Since each hard link is a pointer to an inode, deleting a hard link doesn’t delete the inode unless something known as the “inode reference counter” reaches zero.
You can get the inode reference counter using the following command:
stat -c %h original_file.txt
And this gives the following:

iNode Pointer Counter
When this counter reaches “0”, the file is deleted.
#Benefits of Using a Hard Link
While the benefits of a symbolic link are easy to see, those of a hard link are a bit more obscure. Nonetheless, there are solid use cases where a hard link makes sense. Here are some of them.
Incremental Backups
Backups consume a lot of space. If you have a backup that runs every day, there’s a good chance that most of the files will be unchanged. Making copy after copy of these files every day is a very poor use of storage space. Instead, we use hard links. When the backup system detects that a file hasn’t changed since the last backup, it creates a hard link in the new backup instead of a completely new file. It’s still the same file (unlike a symbolic link), but it doesn’t use any additional space.
Allowing Moving or Deleting the Original File
If you create a hard link to a file, you can rename, delete, or move the original without consequences and not break the hard link – unlike with symlinks. Package managers like Debian exploit this to keep references to configuration files in a single location, without worrying about the references breaking if the original files are renamed or deleted.
There are other use cases as well, but they become increasingly complicated as they’re for advanced use cases. Still necessary, but you’re unlikely to encounter those situations as a regular Linux administrator.
#Conclusion
Most of the time, a symbolic link will be good enough for your needs. They’re safer to use and easier to understand. Using hard links only when you need the special features, like being able to delete the original – otherwise, it can cause weird behavior that’s difficult to debug.


