What is Hardlink and Softlink


Before diving into Hard Links and Soft Links, we first need to understand what is a Link?

In Linux, a file has two parts:

  1. Data part.
  2. Filename part.

Data part of the file is directly linked to the inode. What is inode? Well it is something that stores all the information about the file like where it is stored, what are the permissions of the data etc.
Referencing the file with inode is done by the Filename.
The contents of file is pointed out by the Link.

So now that we know how a file is stored and what links are, what is HardLink and SoftLink? To explain things better, we’ll use two files, File RW and File CW.

Hard Links:

We know file has filename which is used to refer and Data part which maps it to the location on drive. If there are two files in which the data part of these files point to a single location on the disk, they’re Hardlinked to each other. That means, when two separate files RW and CW point to the same address on the hard-disk, RW and CW are hard linked to each other.
That means, if you change data of RW, CW will be affected (and vice-versa) because both point to the same location and when you change data of file, you’re actually changing the data on the address of hard-disk to which it refers.
BUT if you delete RW, CW won’t be deleted because its pointer is still pointing to the address. So basically you have a backup copy of the file.

Hardlink image

Hardlink image

Summary:

  1. Two hardlinked files point to the same spot on hard disk.
  2. If you edit one file, changes will be made to the other one too.
  3. If you delete a file, other one will still exist and have the same content as the deleted file (like a backup file).
  4. Advantage: You have a backup file which is always updated.
  5. Disadvantage: Two files of same contents same size. If one file is of size 2 GB, other will also be of 2 GB. Hence duplicate files, will take more space.

Soft Links:

Soft links are also called as Symbolic links. When the data part of CW has a path to RW, then we say file CW is soft-linked with file RW. That means whatever the contents of RW is, will also be the contents of CW. In short, CW is nothing but a pointer to RW.
However, if you delete RW (to which CW is pointing), CW will be useless. CW was pointing to RW but RW is no more hence it would be pointing no where.

softlinked file

softlinked file

Summary:

  1. In softlinks, one file is pointing to the other file.
  2. If you edit one file, changes will be made to the other one too.
  3. If you delete the original file, the softlinked file will also be lost.
  4. Advantage: Since second file is just pointing to first, it takes almost no space on your hard drive. It is just a pointer. So if you have a 2GB file, the softlinked file won’t be of 2GB.
  5. Disadvantage: The soft-linked file becomes useless if the original file is corrupted.

How to create a hardlink or softlink file?

For hardlink you need to write the command:

ln file1.txt file2.txt

If file2 doesn’t exists, it’ll create one and copy the contents of file 1 into it.

Example of hardlink file

Example of hardlink file

For softlink you need to write:

ln -s file1.txt file2.txt

Here we use the ‘-s‘ switch to tell the kernel to create a symbolic link between these files.

Example of softlink file

Example of softlink file

Leave a Reply