Hard Links VS Soft Links in Linux: What’s the Difference
From concepts to operations

If you are a backend or DevOps developer, there are two special concepts you’re supposed to know of Linux/UNIX filesystems: hard links and soft links (or called symbolic links). Because sometimes it may lead to wrong operations for files on a Linux system if you don’t know the difference between them.
Hard Links
A Linux system uses an inode to store the metadata of a file and every inode has an inode number as its identifier. It’s possible that multiple filenames point to the same inode number.
This mechanism has advantages:
- A file can be accessed with different filenames.
- Modifications of a file by one of its filenames will affect all filenames.
- Deleting one filename does not affect access to other filenames.
Therefore, it’s clear what a hard link is. A hard link is a link pointing to an existing file’s inode. One or more hard links for an existing file can be created.
How to create a hard link
We can use the ln command to create a hard link.
For example, there is a file called test.txt on my system and I created a hard link for it called test2.txt:
ln test.txt test2.txtThen, we can use the ls -i command to check their inode numbers:

As shown above, the test2.txt has the same inode number with the test.txt.
Hard links, by the way, cannot be created for directories and files on a different filesystem, cause different filesystems manage their own inodes.
Soft/Symbolic Links
A soft link is a pointer to a file or directory.
Unlike a hard link, a soft link has its own inode and inode number. But it points to another existing file’s content.
This means that a soft link depends on its “host file” to exist. If its host file is deleted, opening the soft link will report an error: “No such file or directory”. (This is totally different with hard links.)
How to create a soft link
We can create a soft link by the ln command with the help of a -s option.
ln -s test.txt test3.txtThe above command creates a soft link for my file test.txt.
Now, my original file test.txt has a hard link and a soft link.
Let’s input the ls -li command to check out the details of it:

As shown above, the test.txt and test2.txt have the same inode number(8800836), but the inode number of the test3.txt is 8800838. And obviously, the results told us it’s a soft link by displaying it as test3.txt->test.txt.
Since it’s a pointer and there’s nothing about inodes, a soft link can point to a file or a directory on a different filesystem or partition.
Thanks for reading. If you like it, please follow me and become a Medium member to enjoy more great articles. 🙂
Relative articles:
