A link in a Linux-based operating system points to a file or a directory. Links allow more than one file name to refer to the same file. There are two types of links, Soft Links and Hard Links. In simple terms, a soft link is an actual link to the original file, while a hard link is a mirror copy of the original file.

If you delete the original file. you can not access its contents via its soft link. On the other hand, if you delete the original file you can still access its contents via its hard link. The hard link has the same inode number and file permissions while the soft link has the different inode number and file permissions.

In this post, we will show you how to create a soft and hard link in Linux.

Create a Soft Link

First, create a directory named softlink with the following command:

mkdir softlink

Next, create a file named normal_link.txt:

cd softlink
echo "This is a softlink" > normal_link.txt

Next, create a soft link of the file normal_link.txt:

ln -s normal_link.txt soft_link.txt

Now, check the content of both files:

cat normal_link.txt

Output:

This is a softlink

And

cat soft_link.txt

Output:

This is a softlink

As you can see, both files have the same content.

Now, check the inode number and permissions of both files:

ls -lia

You should see that both files have different inode numbers and permission.

15866192 drwxrwxr-x  2 vyom vyom 4096 Apr 19 15:10 .
15466497 drwxrwxrwt 13 root root 4096 Apr 19 15:10 ..
15866186 -rw-rw-r--  1 vyom vyom   19 Apr 19 15:09 normal_link.txt
15866189 lrwxrwxrwx  1 vyom vyom   15 Apr 19 15:10 soft_link.txt -> normal_link.txt

Now, remove the original file with the following command:

rm -rf normal_link.txt

Now, check the content of the soft link file:

cat soft_link.txt

Output:

cat: soft_link.txt: No such file or directory

As you can see, there is no such file or directory.

If you want to remove the soft link, run the following command:

unlink soft_link.txt

Create a Hard Link

First, create a directory named hardlink with the following command:

mkdir hardlink

Next, create a file named normal_link.txt with the following command:

cd hardlink
echo "This is a hard link" > normal_link.txt

Next, create a hard link of the file with the following command:

ln normal_link.txt hard_link.txt

Now, check the content of both files:

cat normal_link.txt

Output:

This is a hard link

And

cat hard_link.txt

Output:

This is a hard link

Next, check the inode and permission of both files:

ls -lia

You should see that both files have the same inode and permission:

15866186 drwxrwxr-x  2 vyom vyom 4096 Apr 19 15:16 .
15466497 drwxrwxrwt 14 root root 4096 Apr 19 15:16 ..
15866190 -rw-rw-r--  2 vyom vyom   20 Apr 19 15:16 hard_link.txt
15866190 -rw-rw-r--  2 vyom vyom   20 Apr 19 15:16 normal_link.txt

Now, try to add some content to the original file:

echo "I am adding some content" >> normal_link.txt

Now, check the content of the hard link:

cat hard_link.txt

You should see that the changes we made on the original file are updated in both files.

This is a hard link
I am adding some content

Now, remove the original file:

rm -rf normal_link.txt

Now, check the content of the hard link:

cat hard_link.txt

You should still see the content of the file:

This is a hard link
I am adding some content

If you want to remove the hard link, run the following command:

unlink hard_link.txt

Conclusion

In the above guide, you learned how to create a soft and hard link on Linux. Try creating soft and hard links on your VPS hosting account with Atlantic.Net!