Home Linux Commands Explaining Soft Link And Hard Link In Linux With Examples

Explaining Soft Link And Hard Link In Linux With Examples

Symblolic Link And Hard Link Explained With Examples

By sk
Published: Updated: 163K views

A symbolic or soft link is an actual link to the original file, whereas a hard link is a mirror copy of the original file. If you delete the original file, the soft link has no value, because it points to a non-existent file.

But in the case of hard link, it is entirely opposite. Even if you delete the original file, the hard link will still has the data of the original file. Because hard link acts as a mirror copy of the original file.

In a nutshell, a soft link

  • can cross the file system,
  • allows you to link between directories,
  • has different inode number and file permissions than original file,
  • permissions will not be updated,
  • has only the path of the original file, not the contents.

A hard Link

  • can't cross the file system boundaries (i.e. A hardlink can only work on the same filesystem),
  • can't link directories,
  • has the same inode number and permissions of original file,
  • permissions will be updated if we change the permissions of source file,
  • has the actual contents of original file, so that you still can view the contents, even if the original file moved or removed.

Still don't get it? Well, allow me to show you some practical examples.

Let us create an empty directory called "test".

$ mkdir test

Change to the "test" directory:

$ cd test

Now, create a new file called source.file with some data as shown below.

$ echo "Welcome to OSTechNix" >source.file

Let us view the data of the source.file.

$ cat source.file
Welcome to OSTechNix

Well, the source.file has been created.

Now, create the a symbolic or soft link to the source.file.

To do so, run:

$ ln -s source.file softlink.file

Let us compare the data of both source.file and softlink.file.

$ cat source.file 
Welcome to OSTechNix
$ cat softlink.file 
Welcome to OSTechNix
View symlink data
View symlink data

As you see in the above output, softlink.file displays the same data as source.file.

Let us check the inodes and permissions of softlink.file and source.file.

$ ls -lia

Sample output:

total 12
11665675 drwxrwxr-x  2 sk sk 4096 Oct 17 11:39 .
 4325378 drwxr-xr-x 37 sk sk 4096 Oct 17 11:39 ..
11665731 lrwxrwxrwx  1 sk sk   11 Oct 17 11:39 softlink.file -> source.file
11665692 -rw-rw-r--  1 sk sk   21 Oct 17 11:39 source.file
check the inodes and permissions of symbolic link
Check the inodes and permissions of symbolic link

As we see in the above screenshot, the inode number (11665731 vs 11665692) and file permissions (lrwxrwxrwx vs -rw-r--r--) are different, even though the softlink.file has same contents as source.file. Hence, it is proved that soft link don't share the same inode number and permissions of original file.

Now, remove the original file (i.e source.file) and see what happens.

$ rm source.file

Check contents of the softlink.file using command:

$ cat softlink.file

Sample output:

cat: softlink.file: No such file or directory
check symbolic link contents
Check symbolic link contents

As you see above, there is no such file or directory called softlink.file after we removed the original file (i.e source.file).

So, now we understand that soft link is just a link that points to the original file. The softlink is like a shortcut to a file. If you remove the file, the shortcut is useless.

As you already know, if you remove the soft link, the original file will still be available.

Create a file called source.file with some contents as shown below.

$ echo "Welcome to OSTechNix" >source.file

Let us verify the contents of the file.

$ cat source.file
Welcome to OSTechNix

The source.file has been created now.

Now, let us create the hard link to the source.file as shown below.

$ ln source.file hardlink.file
create hard link
Create hard link

Check the contents of hardlink.file:

$ cat hardlink.file
Welcome to OSTechNix

You see the hardlink.file displays the same data as source.file.

Let us check the inode and permissions of hardlink.file and source.file.

$ ls -lia

Sample output:

total 16
11665675 drwxrwxr-x 2 sk sk 4096 Oct 17 11:58 .
4325378 drwxr-xr-x 37 sk sk 4096 Oct 17 11:39 ..
11665692 -rw-rw-r-- 2 sk sk 21 Oct 17 11:57 hardlink.file
11665692 -rw-rw-r-- 2 sk sk 21 Oct 17 11:57 source.file
check the inodes and permissions of hard link
Check the inodes and permissions of hard link

Now, we see that both hardlink.file and source.file have the same the inodes number (11665692) and file permissions (-rw-r--r--). Hence, it is proved that hard link file shares the same inodes number and permissions of original file.

Heads Up: If we change the permissions on source.file, the same permission will be applied to the hardlink.file as well.

Now, remove the original file (i.e source.file) and see what happens.

$ rm source.file

Check contents of hardlink.file using command:

$ cat hardlink.file

Sample output:

check hard link contents
Check hard link contents

As you see above, even if I deleted the source file, I can view contents of the hardlink.file. Hence, it is proved that Hard link shares the same inode number, the permissions and data of the original file.

You might be wondering why would we create a hard link while we can easily copy/paste the original file? Creating a hard link to a file is different than copying it.

If you copy a file, it will just duplicate the content. So if you modify the content of a one file (either original or hard link), it has no effect on the other one.

However if you create a hard link to a file and change the content of either of the files, the change will be seen on both.

Let us have a look at the source.file.

$ cat source.file 
Welcome to OSTechNix

The source file has a single line that says - Welcome to OSTechNix.

Append a new line, for example "Welcome to Linux" in source.file or hardlink.file.

$ echo "Welcome to Linux" >>source.file

Now check contents of both files.

$ cat hardlink.file 
Welcome to OSTechNix
Welcome to Linux
$ cat source.file 
Welcome to OSTechNix
Welcome to Linux
Update contents of hard link
Update contents of hard link

See? The changes we just made on source.file are updated in both files. Meaning - both files (source and hard link) synchronizes.

Whatever changes you do in any file will be reflected on other one. If you normally copy/paste the file, you will not see any new changes in other file.

For more details, check the man pages.

$ man ln

Conclusion

In this guide, we have discussed what is soft link and hard link in Linux, how to create softlink and hardlink with example commands and finally we explained the difference between hardlink and normal copied file.

Hope you got a basic idea of how to use symbolic or soft link and hard link in Linux.

You May Also Like

34 comments

akshay September 27, 2019 - 12:01 am

nice explanation

Reply
Calator prin Romania October 23, 2019 - 3:17 am

Hmm it seems like your blog ate my first comment (it was super long) so I guess I’ll just sum it up what I wrote and
say, I’m thoroughly enjoying your blog. I as well am
an aspiring blog blogger but I’m still new
to everything. Do you have any tips for novice blog
writers? I’d certainly appreciate it. – Calator prin Romania

Reply
Jenn November 28, 2019 - 6:33 am

This help me a lot, thanks <3

Reply
Yen Lung December 1, 2019 - 7:26 am

Clearly and nicely explanation.

Reply
Hantong Liu December 30, 2019 - 8:57 am

Good!

Reply
Pallavi Ghorpade January 21, 2020 - 7:37 am

Very much easy explanation to understand the concept clearly. Thank you so much

Reply
ac-optimus January 26, 2020 - 3:48 pm

this was really awesome, thanks!

Reply
A.T. February 20, 2020 - 12:00 am

It’s very helpful for beginners. Merci !

Reply
Gabriel March 23, 2020 - 5:47 pm

I am a beginner in Linux (I use Ubuntu on a VM) and this was a very clear explanation for me.
Now I really understand the differences between hard link and soft link. I have to go back to the terminal to test all new knowledges.
Thank you for your time!

Reply
Chirag Garg April 2, 2020 - 10:22 am

You have cleared every single doubt, Awesome 🙂

Reply
1 2 3

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More