What is difference between hard link and soft link in UNIX?

There are two types of links in UNIX system, hard link and soft links, also known as symbolic link or symlink. Though both points to some other source, there are a lot of difference between them. hard link is direct pointer to the inode of the original file. If you compare the original file with the hard link there won't be any differences between them. On other hand, a soft link is a file that have the information to point to another file or inode. That inode points to the data in the disk. Hard links are much more restrictive than soft links and that's why they are used rarely. Also not all file systems supports hard links but UNIX does. Earlier Windows file system doesn't support soft links but from Windows 2000 onward they are supporting it, both Windows Vista and Windows 7 support links now. MS DOS operating system doesn't support neither hard link nor soft link. You can use ln command in UNIX to create, update and remove soft links. I often use links for build and release management purpose e.g. our application never points to hard-coded file or directory instead it points to links which is updated on every release.

Difference between Soft link and Hard link in UNIX

Here are couple of important differences between hard link and soft link in UNIX :

1) The target of the the hard link must exist, which is not mandatory in case of soft link. A soft link is said broken if target link doesn't exists.

2) Unlike soft link which are mostly created to reference directories e.g. current link pointing to latest release, Hard links are generally not allowed on directories.

3) One more critical difference between soft link and Hard link is that hard links are not allowed to cross partitions or volumes. Therefore, they cannot exist across file systems.

4) A hard link looks, and behaves, like a regular file, so they can be hard to find. On the other hand soft links are quite different than regular files.

5) A hard link is, for all intents and purposes, the same entity as the original file. They have the same file permissions, time stamps, and so on. All attributes are identical.

Difference between hard and soft link is also one of the popular Linux and UNIX Interview question, so knowing difference will also help you on interviews.  BTW, A picture is worth a thousand words and this is true in connection with this image. You can see that how hard link and soft link are different to each other, hard link directly point to file

What is difference between hard and soft link in UNIX

That's all about difference between hard link and soft link in UNIX. Because of these restrictions, hard links are not used as often as symbolic links. I have yet to find a good reason to use hard link but I have been using soft links from long time for starting our Java application. It's much more flexible to refer links in shell script than hard coded files and directory names.

Here are couple of more UNIX Interview questions and articles from this blog :
  • How do you find symbolic links in a directory in UNIX? (answer)
  • How to find hostname from IP address in UNIX? (answer)
  • How to find swap space and usage in Solaris? (answer)
  • How to remove empty files and directory in UNIX? (answer)
  • How to find size of a directory including all files and sub-directory? (answer)

Further Learning
Linux Command Line Basics
Linux Command Line Interface (CLI) Fundamentals
Learn Linux in 5 Days and Level Up Your Career

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.