What are Hard Links and Soft Links in Unix and How to Use Them

In computer networks, links are references to files and folders. Hard links and soft links are the two types of links. A hard link refers to a file directly, whereas a soft link refers to a file by name. As a result, a soft link uses the file name to refer to a file. This blog will explain What are Hard Links and Soft Links in Unix and How to Use Them. To learn more about UNIX, join the UNIX Training in Chennai at FITA Academy, which will provide you with a diverse skill set and the best Placement Training.

In UNIX, a link is a file pointer. Links in UNIX are pointers to files or directories, much like pointers in other computer languages. Creating links is a type of shortcut for getting to a file. Multiple file names might refer to the same file in multiple locations thanks to links. 

What is Hard Links?

  • Each hard linked file has the same Inode value as the original, they correspond to the same physical file location. Although hard links are more versatile since they stay linked even if the originating or linked files are moved throughout the file system, they cannot cross various file systems.
  • The ls -l command displays all links, with the link column displaying the number of links.
  • The contents of links are actual file contents.
  • Removing any link reduces the link count but has no effect on other links.
  • Even if we modify the filename of the original file, the hard links continue to function effectively.
  • To avoid recursive cycles, we cannot build a hard link for a directory.
  • If the primary file is deleted, the link will still display the file’s content.
  • The size of any hard link file is the same as the original file, and if the content of any hard link is changed, the size of all hard link files is updated.
  • Hard links have the disadvantage of not being able to be made for files on various file systems or for particular files or directories.

The command to make a hard link is: 

$ ln [original filename] [link name] 

Enroll in the UNIX Shell Scripting Online Training, Which will provide you with more Concepts about UNIX features.

What is Soft Links?

  • A soft link is analogous to the file shortcut capability found in Windows operating systems. Each soft linked file has its own Inode value, which points to the original file. Any changes to the data in either file are reflected in the other, much as hard links. Soft links can be linked between file system, but if the source files is removed or transferred, the soft linked file will stop working (this is known as a hanging link).
  • The ls -l command displays all links that have the first column value l? and point to the original file.
  • Soft Link only retains the path to the original file, not its contents.
  • Removing a soft link has no effect, but removing the original file causes the link to become a “dangling” link that leads to a nonexistent file.
  • A soft link can be used to point to a directory.
  • The soft link’s size is equal to the length of the original file’s path that we provided.
  • If we change the name of the original file, any soft links to that file become dangling, which means they are no longer useful.
  • Link between file systems: Only symlinks/soft links can be used to connect files between file systems.

The command for creating a Soft link is:

$ ln -s [original filename] [link name]

Conclusion

Hopefully, you enjoyed this blog and now understand everything about UNIX, including What are Hard Links and Soft Links in Unix and How to Use Them.

You can gain more expertise and ability in designing dynamic and interactive web applications by studying at the UNIX Training Institutes in Chennai at FITA Academy.

Read more: Cloud Computing Interview Questions and Answers

By Sumathi

Leave a Reply

Your email address will not be published. Required fields are marked *