Symbolic/Soft Links and Hard Links

Safaetul Ahasan
2 min readOct 21, 2023

--

soft link vs hard link

What are soft links in Linux?
We have a file shortcut feature in windows that is used to create a shortcut for files.

A symbolic link is similar to a file shortcut. Each symbolic-linked file has its own Inode value, which refers to the original file.
Any changes to the information in one file are mirrored in the other. You can connect soft links across different file systems, but if the source file is removed or transferred, the symbolic-linked file will not function properly. This link is called a hanging link. Removing a soft link has no effect, but If you remove the original file, it may cause the link will stop working.
A symbolic link may be used to point to a directory and it just retains the path to the original file, not its contents. The size of the soft link will be equal to the file for which the soft link is formed. For example, if the file abc.txt is 2KB, the symbolic link of this file will be also 2KB.

To create symbolic links, we use the command ln with the flag -s . For example, ls -s /path/to/original /path/to/linkName. A file named linkName has been created and linked to the file original.

ln -s orginal.file softlink.file

Let us compare the data of both orginal.file and softlink.file.

cat orginal.file 
Welcome to medium

cat softlink.file
Welcome to medium

What are hard links in Linux?
A hard link in Linux is equivalent to a file saved on a hard drive — and it really refers to or links to a location on a hard drive. A hard link is essentially a mirror image of the original file. The difference between a hard link and a soft link is that removing the source file has no effect on a hard link but makes a soft link unworkable. So the most significant benefit of making a hard link is that you can still access the contents of the file even if you unintentionally erase it.

To create hard links, we use the same command ln without the -s flag. For example, ln original.file hardlink — would create a file named hardLink that has the same inode as the original file originalFile.

ln orginal.file hardlink.file

--

--

Safaetul Ahasan
Safaetul Ahasan

No responses yet