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

Explaining Soft Link And Hard Link In Linux With Examples

By sk
2313 Views

What is Soft Link And Hard Link In Linux?

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.

Creating Soft Link or Symbolic Link

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

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

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

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.


Suggested read:


Creating Hard Link

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

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

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

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.

Note: 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

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.

So, what is the difference between Hard link and the normal copied 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 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

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

That's all for now. Hope you got a basic idea about symbolic or soft link and hard link.

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

26 comments

Kanika September 7, 2018 - 2:09 pm

Very clearly explained.

Reply
Mike September 9, 2018 - 3:34 pm

Great explanation!!! Thanks

Reply
chindesh November 30, 2018 - 5:09 pm

Simple and clean. Thank you!

Reply
Mohammed Firdosh Nasim January 23, 2019 - 6:45 pm

Nice explanation. Requesting you to update the following “file permissions (lrwxrwxrwx vs lrwxrwxrwx)” in soft link section to in “file permissions (lrwxrwxrwx vs -rw-r–r–)” .

Reply
sk January 24, 2019 - 12:26 pm

Good catch. Corrected now. Thanks for pointing it out.

Reply
Oliver Etchebarne February 3, 2019 - 10:04 am

Excellent explanation! Just a thought: a “mirror copy” implies that both source.file and hardlink.file use different disk space, thus occupping twice as much space (“a copy”), which is wrong.

Both file names are just different entries in the directory listing to the same “data”, same disk space (identified by an inode), and Linux have a “link count” of file names in every inode. It only erases the inode data when the link count reaches zero.

¡Saludos!

Reply
Sandeep April 30, 2019 - 8:11 am

Great explanation, just one questions, can you please explain that why do we need to create Soft Link or Hard Link, what is the purpose of creating these links. Can you please let me know one scenario for this? Thanks in advance

Reply
Hema June 4, 2019 - 8:08 pm

My question is, if we update the content in softlink will it reflect to the original file as well?

Reply
sk June 4, 2019 - 8:26 pm

Check it yourself and see if it works. Open the shortcut file in a text editor, add some lines, save and close the file. Now check the contents of source file and symlink.

Reply
Tin July 9, 2019 - 1:43 pm

Something worth noting is that *editing* a hard linked file will still edit *all* linked files, since they’re actually the exact same file.

Reply
Ghalib July 21, 2019 - 1:05 pm

what about the size in soft link and hard link takes in time of creation.

Reply
rajadurai m August 24, 2019 - 7:06 pm

it is very useful to understand the beginners. Thank you

Reply
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
iProcessor April 27, 2020 - 5:29 pm

Under the “Creating Hard Link” section, thanks to change the word “softlink.file” into “hardlink.file” instead for the statement=>”Let us check the inode and permissions of softlink.file and source.file.” as I believe it was a typo error.

Thanks 🙂

Reply
sk April 27, 2020 - 6:21 pm

Good catch. Fixed it now. Thank you.

Reply
Idris May 17, 2020 - 7:16 pm

Excllent explanation and very usefull content.

Thank you

Reply

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. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More