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

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