What is Soft and Hard links in linux?

Soft links and Hard links are used to access the file from other path apart from the original location of the files or folders. In this following post, you will learn what is soft link and hard links in linux along with their differences.

Soft(Symbolic) Links:

1. Soft links(Symbolic links) are links that acts as a pointer. Soft links maps the original file or folder to the target duplicate file similar to windows shortcuts.

2. Every file or folder in linux has its own unique inode number. When creating soft links, the source and destination files or folders have different inode numbers.

3. If the source or original file is deleted, the target file mapped to the source file remains broken or forbidden.
A file can be soft linked using below commands.
ln –s <source absolute path> <actual original regular file>

or

ln –sf <source absolute path> <actual original regular file>

Below is the figure that lists before soft linking the file2.

In the below figure, the file file2(source file) is linked to dir2/linkfile2(destination file link).

The destination file went broken as the absolute source file path is not given while linking. The red colour denotes that the link file is broken.

The “l” letter will be present in the beginning of the permissions of soft link file as mentioned in the below figure but in the original source file that is file2, there will not be any “l” letter in the file permission section as shown in previous figure.

Below are the figures that shows the correct linking of file4 and errorfile1, where the absolute source file path is mentioned and absolute path of destination file link or relative path of destination file link are mentioned which becomes working active soft link(shown in cyan blue).

Below figure shows the differences between the soft(symbolic) link file and regular original file.

Hard Links:

1. Hard links are the copy/duplicate of original source file but with same inode numbers for both source and destination files.

2. If the original source file is deleted, the destination file contents can be viewed without issues.

The ln command can also be used to create a hard link of a file.

ln <absolute path of file> <destination file name>

Below figure shows the metadata of source file and destination file after hard linked.

How do you feel about this post or want to suggest a new post? Drop your comments below..