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: 161.6K 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

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
Rajeshwari September 28, 2020 - 12:49 pm

good explanation. Easily understandable.

Reply
Roman Incognito November 8, 2020 - 5:48 am

Nice explanation?? It’s not !
Phrasing is confusing.
Hard link is not a mirror copy of original file. Hard link is more like link for the chunk (frame) of data. Hence creating hard link is NOT doubling size. That’s the most important point here. Well, and soft link is a link to the file, NOT link to data on harddrive. And as a result diff. inodes (data block location or data pointers) in case of soft links and identical inodes in case of hard links.

Reply
Gegard September 8, 2021 - 1:14 pm

Thanks, best article for soft / hard links practise

Reply
Alexander December 17, 2021 - 7:01 pm

Excellent! Explanation, very clear!

Reply
Eduard December 30, 2021 - 8:34 pm

Summary: hardlinks are in fact just the principle of pointers/references from program code applied to file system entries on a storage medium.
With pointers or references in code, you can have multiple references to the same variable, the same part of memory with the same data.

More interesting: COW (Copy on write) file systems, like Btrfs or ZFS. They initially follow the principles of hardlinks to save disk space, until you modify one of both file entries. At the moment of modification, an actual copy of the data is automatically made.

Reply
sk December 30, 2021 - 9:13 pm

Thanks Eduard.

Reply
Jonathan Gordon April 8, 2023 - 12:20 am

what happens with a Sym link if the source file is deleted then recreated? does the sym link revive, or must the symlink be recreated?

Reply
sk April 8, 2023 - 8:10 pm

If you recreate the original file, the symlink will work again.

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