Difference between a hard link and a symbolic link
Hard link & soft link are created to connect files to a source file. But the result is different! And we will check the difference now!
Hard Link:
It is a hard link connection that you can make with the address of the content of the source file. That’s why, in the picture, we see the arrows connected to a transparency box called “lnode 100” (this is in the memory, that’s why we can’t see)
Soft Link:
It is a connection between 2 files!: file pointing to another file. That ‘s why, we can see the arrows connected to the “original file” (not to the memory address). That’s why if we delete the original file, our soft link file will become useless!.
How to create?
- Original file that we want to “linked” : OriginalFile
- File that will connect by a soft link: SoftLink file
- File that will connect by a hard link: HardLink file
To create a hard link:
ln {OriginalFile} {HardLink file}
To create a soft link:
ln -s {Originalfile} {SoftLink file}
what is the difference?
Hard link creates the connection to the address & of the content (in the memory), that’s why is like create a copy and their size is bigger than soft link
Soft link: creates a link to the source file (not to the memory ) that ‘s why, if we decide to delete de source file, the Soft link file would have an error but the HardLink file would continue works!
Let’s check in the next image:
Now, let’s go to terminal and write an example!
And now, we can see the difference in the terminal too:
Finally, let’s check the differences:
- Types of files (first letter of the left column): Hard Link is a file (-), Soft Link (l) is a link
- Also, we can check that soft link make a pointer ( — ->) to the source file “prueba/saludos”
- We check that permissions are differents: Hard link file have the same permissions of the source file (saludos). But soft link change (it has the executive permission(x) for all! this is because, the “soft-saludos” is a pointer to “saludos”.
- the size of files are differents: “hard-saludos” have the same size of “saludos” (case of hard-link). The size of “soft-saludos” is less than “saludos” (soft-link)
Not difference: we see in the second image that they have the same content! but remember, if we delete the source file. Hard-link file will be the same. Soft-link file will become useless. This is happening because in C, the content allocates in memory address and if we want to clean the content of the memory address we need to erase the content of the memory using malloc() or calloc.
And this is all :), now you can be a crack in soft-link & hard-link :)! If you have a question, you can write it in the comments!