Wednesday, March 14, 2012

Soft Links & Hard Links - All about Inodes - Part 2

 In one of our earlier articles, we discussed about the inode structure and its properties under the title All about Inodes. In continuation to the same, in this article, we will see about the links in UNIX: hard links and soft links, and their relationship with Inodes. We will learn them in the way of questions and answers.

1. What are links in Unix?
    A link in UNIX is a pointer to a file. Like pointers in any programming languages, links in UNIX are pointers pointing to a file or a directory . Creating links is a kind of shortcuts to access a file. The two different types of links in UNIX are:
  • Soft Links or Symbolic Links
  • Hard links

2. How to create Soft links and hard links? And how do we access them?
     Say, you have a file named "file1" with the following contents:
$ cat file1
.   To create a hard link of file1:
$ ln file1 file2
   To create a soft link of file1:
$ ln -s file1 file3 
    Once the links are created, the linked files contain the same content as of the original file. See below
$ cat file2
$ cat file3
Note: Soft links can be created on non-existent files as well.

3. "file2" and "file3" being the linked files, can I say which is a soft link & which is hard link?
   Yes. When you do the listing of the files with "-li" option:
$ ls -li
total 20
9962464 -rw-r--r-- 2 guru users 8 Mar  9  file1
9962464 -rw-r--r-- 2 guru users 8 Mar  9  file2
9962471 lrwxrwxrwx 1 guru users 5 Mar  9  file3 -> file1
   If you notice file3, it shows "->" towards file1. This indicates file3 is a soft-link of file1.  In case of file2, if you notice the inode numbers of file1 and file2, they are the same. Same inode number indicates file1 and file2 are hardlinks. Also note, the link count of these files is 2.

4. file1 and file2 are of the same size. In case of the file "file3", the file size is being shown as 5. How is it 5?
      The file size of a soft linked file is the length of the filename of the original file. In this case, the original file "file1" is of length 5. In a soft linked file, the location where the file content is to be stored, the file name of the original file gets stored, and hence the file size is so.

5. file1 and file2 are hardlinks? Can we say which is the original file and which is the hard-linked file?
    No. We cannot say which is the original file and which one was the hard-linked file. Once a hard-link is created, it is like 2 files pointing to the same location. In fact, once a hard link is created on a file, using the term 'original file' is actually incorrect.

6. Will deleting the file "file1" make "file2" and "file3" inaccessible?
   On deleting the file "file1", the soft linked file "file3" will become inaccessible. However, the hard linked file "file2" can still be accessed.

7. How is it possible that while deleting the original file, still the hard linked file is accessible, and the soft linked file is not?
    Let us look into the inode structure of the hard and soft links to understand this in detail.
                             _______        ________
         FIle1--------\_____|inode  |______| welcome|
                       _____|_______|      |________|
         File2--------/      9962464

      Fig 1: Hard Link representation(Files pointing to inodes, in turn pointing to data location)
   File1 and File2 are hardlinks. As we know, they both have the same inode number(9962464) and hence they both point to the same inode structure. One of the parameters in the inode tells the location of the file contents, and hence the contents are accessed by both the files.

   Now, say we try to delete the file "File2". When the file is deleted, only the link connecting of File2 to the inode structure gets disconnected. However, the inode and the file contents and the link of File1 are still in tact, and hence the file is still accessible.

    However, the same is not the case with the soft link. In case of soft link:
                       _______        ________
         FIle1--------|       |______| welcome|
                      |_______|      |________|     
                       _______        ________
         FIle3--------|       |______| File1  |
                      |_______|      |________|
     Fig 2: Soft Link Representation  (Files pointing to inodes, in turn pointing to data location)

   File1 and File3 both have different inode numbers. These different inode numbers point to different data locations. File1 location points to the contents of the file. However, if you see the content location of File3, it contains "File1" which is nothing but the original file from which the soft link is formed. Now, when File3 is accessed, through its inode metadata, it gets the name "File1" and searches for "File1" and reaches to the content of "File1". Hence, once the "File1" is deleted, "File3" becomes dangling meaning it points to something which does not exist. And so, the soft link becomes inaccessible.

   For these same reasons, we can create a soft link on an non-existent file, however, the same is not possible for hard links.

8. Soft links can span across Filesystems whereas hard links can be created only within a file system. Why so?
   Hard link refers to its file using the inode number. Inode numbers are specific within a file system. This means a file "f1" in filesystem "X" can have the same inode number as of a file "f2" in filesystem "Y". This is very much possible. For this reason, hard links cannot be shared across file systems.

  However, the same is not true for soft links. Soft links refer to the files using the file name as seen in the earlier case. Hence, a soft links can be accessed across the file systems.

9. Does soft links and hard links work only at the file level? Does it work at the directory level as well?
     Hard links work only at the file level. However, soft links works at the directory level as well. This particular use of soft links, to create links at the directory level, serves us a shortcut for many frequently used directories.

10. Summarizing, what are all the differences between soft and hard links?
Soft Links:
  •      Soft Links can be created across file systems.
  •      Soft link has a different inode number than the original file.
  •      On deleting the original file, soft link cannot be accessed. 
  •      Soft link needs extra memory to store the original file name as its data.
  •      Source file need not exist for soft link creation.
  •      Can be created on a file or on a directory.
  •      Access to the file is slower due to the overhead to access file. 
Hard Links:
  •      Hard links can be created only within the file system.
  •      Hard links have the same inode number as the original file.
  •      On deleting the original file, hard linked file can still be accessed.
  •      Hard links do not need any extra data memory to save since it uses links
  •      Source file should exist.
  •      Can be created only on files, not on directories.
  •      Access  to the file is faster compared to soft link.

Very soon, we will have one more part on Inodes!!!


  1. "Access to the file is faster compared to soft link."

    Above line is present in difference between soft link & hard link section...please rectify it.

  2. @Anonymous: Since the soft link points to a file which is a kind of pointer to pointer, there is a slight overhead to access the file. Hence, I mean to say there is a slight overhead to access the file. What is to be rectified according to you?

  3. what the source file presence means???

  4. Its really useful. has more points compared to other articles but you missed a point that file permission for soft link is not same as original file.