If a hard link is created to a file and then the original file is deleted

To understand the difference between hard and symbolic links, we must first understand what they are, why they exist, and how to create them.

If a hard link is created to a file and then the original file is deleted
View symlink data

As you see in the above output,

$ cat source.file
Welcome to OSTechNix
1 displays the same data as
$ echo "Welcome to OSTechNix" >source.file
7.

Let us check the inodes and permissions of

$ cat source.file
Welcome to OSTechNix
1 and
$ echo "Welcome to OSTechNix" >source.file
7.

$ 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

If a hard link is created to a file and then the original file is deleted
Check the inodes and permissions of symbolic link

As we see in the above screenshot, the inode number (

$ cat source.file
Welcome to OSTechNix
6 vs
$ cat source.file
Welcome to OSTechNix
7) and file permissions (
$ cat source.file
Welcome to OSTechNix
8 vs
$ cat source.file
Welcome to OSTechNix
9) are different, even though the
$ cat source.file
Welcome to OSTechNix
1 has same contents as
$ echo "Welcome to OSTechNix" >source.file
7. 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

$ echo "Welcome to OSTechNix" >source.file
7) and see what happens.

$ rm source.file

Check contents of the

$ cat source.file
Welcome to OSTechNix
1 using command:

$ cd test
0

Sample output:

$ cd test
1

If a hard link is created to a file and then the original file is deleted
Check symbolic link contents

As you see above, there is no such file or directory called

$ cat source.file
Welcome to OSTechNix
1 after we removed the original file (i.e
$ echo "Welcome to OSTechNix" >source.file
7).

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:

  • How To List Symlinks On Linux
  • How To Find Broken Symlinks And Delete Them On Linux

Create a file called

$ echo "Welcome to OSTechNix" >source.file
7 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

$ echo "Welcome to OSTechNix" >source.file
7 has been created now.

Now, let us create the hard link to the

$ echo "Welcome to OSTechNix" >source.file
7 as shown below.

$ cd test
4

If a hard link is created to a file and then the original file is deleted
Create hard link

Check the contents of

$ ln -s source.file softlink.file
9:

$ cd test
5

You see the 

$ ln -s source.file softlink.file
9 displays the same data as source.file.

Let us check the inode and permissions of

$ ln -s source.file softlink.file
9 and
$ echo "Welcome to OSTechNix" >source.file
7.

$ ls -lia

Sample output:

$ cd test
7

If a hard link is created to a file and then the original file is deleted
Check the inodes and permissions of hard link

Now, we see that both

$ ln -s source.file softlink.file
9 and
$ echo "Welcome to OSTechNix" >source.file
7 have the same the inodes number (
$ cat source.file
Welcome to OSTechNix
7) and file permissions (
$ cat source.file
Welcome to OSTechNix
9). 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

$ echo "Welcome to OSTechNix" >source.file
7, the same permission will be applied to the
$ ln -s source.file softlink.file
9 as well.

Now, remove the original file (i.e

$ echo "Welcome to OSTechNix" >source.file
7) and see what happens.

$ rm source.file

Check contents of

$ ln -s source.file softlink.file
9 using command:

$ cd test
9

Sample output:

If a hard link is created to a file and then the original file is deleted
Check hard link contents

As you see above, even if I deleted the source file, I can view contents of the

$ ln -s source.file softlink.file
9. 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.

$ echo "Welcome to OSTechNix" >source.file
0

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

Append a new line, for example "Welcome to Linux" in

$ echo "Welcome to OSTechNix" >source.file
7 or
$ ln -s source.file softlink.file
9.

$ echo "Welcome to OSTechNix" >source.file
1

Now check contents of both files.

$ echo "Welcome to OSTechNix" >source.file
2
$ echo "Welcome to OSTechNix" >source.file
3

If a hard link is created to a file and then the original file is deleted
Update contents of hard link

See? The changes we just made on

$ echo "Welcome to OSTechNix" >source.file
7 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.

$ echo "Welcome to OSTechNix" >source.file
4

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.

BASHCLICommand linehard linkLinuxLinux basicsLinux commandsLinux howtoLinux tipsln commandsoft linksymbolic linkUnixUnix basics

What happens if you delete a hardlinked file?

If the hard link gets deleted, nothing happens to the original file, as it still has its data in the hard drive. Vice versa, if the original file gets deleted, the hard link still refers to the location of the data in the hard drive, therefore the information is still saved.
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.
A hard link will never point to a deleted file. A hard link is like a pointer to the actual file data. And the pointer is called "inode" in file system terminology. So, in other words, creating a hard link is creating another inode or a pointer to a file.
Deleting the hard link does not delete the file it is hardlinked to and the file that was linked to remains where it is. The key is that creating the text file also adds a hard link.