Jump to content

Difference between a Symbolic Link and a Hard Link


aum

Recommended Posts

In a Unix-based file system, files are represented by inodes, which are data structures that store information about a file. A file in the file system is simply a link to an inode. Understanding the difference between symbolic links and hard links is important to manage files effectively.

 

A hard link is a file that points to the same underlying inode as the original file. When you create a hard link, another file with the same content is created in the file system. When you delete a file, it only removes one link to the inode, but the inode itself remains intact until all links to it have been deleted. Hard links are only valid within the same file system, which means that you cannot create a hard link that spans across file systems.

 

A symbolic link, on the other hand, is a special type of file that points to another file in the file system by its name. It is not directly tied to the underlying inode and, therefore, can span across file systems. When you delete or move the original file, the symbolic link will become broken and will not point to the correct file.

 

hardlink-softlink.jpg

 

In summary, the difference between a symbolic link and a hard link lies in the way they refer to the underlying file in the file system. Hard links are tied to the inode and, therefore, cannot span across file systems, while symbolic links are tied to the file name and can span across file systems.

 

Example:

 

Suppose you have a file called "file.txt". To create a hard link to this file, you can use the following command:

 

ln file.txt hard_link_to_file.txt

 

To create a symbolic link to the same file, you can use the following command:

 

ln -s file.txt symbolic_link_to_file.txt

 

If you delete or move "file.txt", the symbolic link "symbolic_link_to_file.txt" will become broken and will not point to the correct file. However, the hard link "hard_link_to_file.txt" will still point to the correct file (actually, the inode of the file).

 

When you delete a file in a Unix-based file system, it only removes one link to the underlying inode. The inode itself remains intact until all links to it have been deleted. In the case of a hard link, "hard_link_to_file.txt" is another link to the same underlying inode as "file.txt". Therefore, deleting "file.txt" only removes one link to the inode, and the inode, along with its contents, can still be accessed through the hard link "hard_link_to_file.txt".

 

If you change the contents of "file.txt", the changes will be reflected in both "hard_link_to_file.txt" and "symbolic_link_to_file.txt".

 

In the case of a hard link, "hard_link_to_file.txt" is another file that points to the same underlying inode as "file.txt". When you change the contents of "file.txt", the changes are made to the inode, and therefore, are reflected in "hard_link_to_file.txt".

 

In the case of a symbolic link, "symbolic_link_to_file.txt" points to "file.txt" by its name. When you change the contents of "file.txt", the changes are made to the file, and therefore, are reflected in "symbolic_link_to_file.txt".

 

Therefore, any changes made to the contents of "file.txt" will be reflected in both "hard_link_to_file.txt" and "symbolic_link_to_file.txt".

 

You cannot tell the hard link and the original file apart by looking at their contents or properties. A hard link and the original file are essentially identical. They both point to the same underlying inode, and any changes made to one of them will be reflected in the other.

 

Source

Link to comment
Share on other sites


  • Views 344
  • Created
  • Last Reply

Top Posters In This Topic

  • aum

    1

Popular Days

Top Posters In This Topic

Popular Days

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...