Difference Between Hard Links and Soft Links

Difference Between Hard Links and Soft Links

hardlink is a feature in file systems (like those found in Unix-based operating systems such as Linux) that allows multiple filenames (or directory entries) to refer to the same physical file data on disk. When we create a hardlink to a file, we're essentially creating another name (or link) for that file's data, rather than copying the data itself.

Explanation of the Scenario:

Lets create a hardlink named shortcut-hard-link to a file named hardlink. Let's break down what happened and explain hardlinks based on our context:

Creating the Hardlink: we created a hardlink named shortcut-hard-link pointing to the hardlink file using the ln command:

ln hardlink shortcut-hard-link
  1. Checking the Links: Whenwe used stat to inspect both hardlink and shortcut-hard-link, we noticed that they both have the same inode number (35320) and both show Links: 2. This indicates that both filenames (hardlink and shortcut-hard-link) are linked to the same underlying file data.

  2. Modifying the Hardlink: After creating the hardlink, we modified the contents of the hardlink file by using cat > hardlink to write some text into it. Since shortcut-hard-link is a hardlink to the same inode (and therefore the same data blocks on disk), the modifications made to hardlink are reflected in shortcut-hard-link.

  3. Removing the Original File (hardlink): When we used rm -rf hardlink to remove the hardlink file,we essentially deleted one of the directory entries pointing to the underlying file data. However, since shortcut-hard-link still points to the same inode (and data blocks), the data remains accessible through shortcut-hard-link.

Accessing the Hardlink Data: Even after deleting the hardlink file, we were able to access the data through shortcut-hard-link because the file data (inode) was not actually deleted until all directory entries (links) pointing to it are removed.

  • Hardlink: In this context, shortcut-hard-link is a hardlink to the hardlink file. Both filenames (hardlink and shortcut-hard-link) refer to the same underlying file data (inode).

  • File Deletion: Deleting one of the hardlinks (hardlink) does not immediately remove the underlying file data as long as other hardlinks (like shortcut-hard-link) still exist.

A symbolic link, also known as a symlink or soft link, is a special type of file that acts as a reference to another file or directory. Unlike hardlinks, which directly reference the inode of a file, symbolic links contain the path to the target file or directory.

Explanation of the Scenario:

Creating a Symbolic Link: we created a symbolic link named shotcut-soft that points to softlink using the ln -s command:

ln -s softlink shotcut-soft

Here, shotcut-soft is a symbolic link pointing to softlink.

  1. Inspecting the Files:

    • softlink: This is the original file (or directory) that the symbolic link shotcut-soft points to.

    • shotcut-soft: This is the symbolic link itself, represented as a file. If we view its contents (cat shotcut-soft), it shows the contents of softlink because it's pointing to softlink.

  2. Modifying the Original File (softlink):we modified the contents of softlink using vim softlink, adding the text "this is a demo softlink file" to it.

  3. Effect on the Symbolic Link (shotcut-soft): Since shotcut-soft is a symbolic link to softlink, any changes made to softlink are immediately reflected when we view shotcut-soft. Both files reference the same underlying target file or directory

  4. Removing the Original File (softlink): When we deleted (rm -rf softlink) the original file (softlink), it was removed from the file system. As a result, attempts to access softlink directly (cat softlink) result in a "No such file or directory" error.

Accessing the Symbolic Link (shotcut-soft): After deleting the original file (softlink), attempts to access the symbolic link (shotcut-soft) also result in a "No such file or directory" error because the target of the symbolic link no longer exists.

  • Reference to Target: Symbolic links are references to other files or directories by their path.

  • Can Cross File Systems: Symbolic links can point to files or directories across different file systems.

  • Affected by Target Changes: Changes to the target file or directory are reflected in symbolic links.

  • Deleting Target: Deleting the original file or directory (target) renders symbolic links pointing to it invalid.

Thank You