Introduction to Linux - A Hands on Guide | Linux Bible | Linux From Scratch | A Newbie's Getting Started Guide to Linux | Linux Command Line Cheat Sheet | More Linux eBooks



Sunday, 21 December 2014

Soft Links (i.e.Symlinks or Symbolic Links) and Hard Links in Linux File Systems

Most of us know that, every single component of Linux file systems is associated with an Inode. Every single element in the Linux file systems is uniquely identified by an Inode number. In one of my previous articles, Understanding Inodes in Linux/Unix File Systems, I have explained 'What Inodes are', 'How they can be accessed' and 'How they are used'.


This article can be considered as a continuum of the above mentioned article and this article will elaborate on Links in Linux file systems, which are Soft Links (also known as Symlinks or Symbolic Links) and Hard Links.

Here we go!

What are Links..?

Links in Linux/Unix can are very much similar to Pointers in programming languages. The basic difference is that, Pointers in programming languages are used to point to the other variables, likewise Link is a pointer to a file or a directory. Creating a links is very much similar to creating a shortcut to a file in order to access it.

There are two kinds of these links based on their properties, which are:
1. Soft Links or Symbolic Links or Symlinks
2. Hard Links

Let's see what exactly they are.

Soft Links:
If you have been using Windows operating systems, understanding Soft Links would be an easier task for you. You can just compare Soft Links in Linux with the 'shortcuts' in Windows, they do not possess any information in them, but they point to the location of a file or a directory in the file system. Eventually, when original file is lost, Soft Links lose their importance.

Hard Links:
Again, when compared to Windows operating systems, Hard Links in Linux are very much similar to 'Copy' of a file in Windows. A bit of difference is that, whenever any changes are made to original file, those changes are reflected in the Hard Links, unlike in Windows. But, when the original file is lost, the contents in the Hard Links are still preserved. How? Lets see in the upcoming section.

Creating Soft Links and Hard Links

Soft Links:
Syntax:

ln -s [SOURCE-FILE-NAME] [LINK-FILE-NAME]
where option -s instructs to create a Soft Link.

Example:
Consider that there is a file 'OrigFile' as follows:


In order to create a soft link 'SoftLinkFile' to 'OrigFile', you would use:

ln -s OrigFile SoftLinkFile
Result:



The SoftLinkFile -> OrigFile indicates that 'SoftLinkFile' is a soft link to the file 'OrigFile'.

Hard Links:
Syntax:

ln [SOURCE-FILE-NAME] [LINK-FILE-NAME]
Example:

ln OrigFile HardLinkFile
Result:


After creating the links, let us see what information they contain, it should be the same as that in the 'OrigFile'.



Even though the contain same information, they do differ from each other in various aspects. Lets see them one by one.

Inode Numbers for Symlinks and hard Links

In order to see Inode Numbers of the files, list them with the option -li. The first column will display Inode numbers of the corresponding files.


Observe the Inodes of 'OrigFile', 'SoftLinkFile' and 'HardLinkFile'. Inode numbers of 'HardLinkFile' and 'OrigFile' are same while that of 'SoftLinkFile' being different than other two. What does this indicate?

The same Inode numbers of 'OrigFile' and 'HardLinkFile' confirms that they point to the same memory location where the contents of the file are kept. Therefore, same contents can be accessed by both the files. On the other hand, 'SoftLinkFile' contains only the name 'OrigFile', but no information.



So, when the 'OrigFile' is deleted, 'SoftLinkFile' would point to some file that would not exist and thus, the file contents would not be accessed, often called as "broken link". The link would still be broken even if the file name gets changed. Whereas, there will be no effect on Hard Link as such. Lets verify this by renaming the 'OrigFile' to 'OrigFile.bak'.


As shown in the above screenshot, broken link is highlighted in red and when accessed, it shows an error message. After restoring the changes made to the 'OrigFile', the 'SoftLinkFile' becomes accessible again.


Sizes of Soft Link and Hard Links

Soft Link:
Soft Links occupies the disk space equal to number of characters in the Source file name. In our example, 'OrigFile' has 8 characters in its name, so 'SoftLinkFile' will occupy 8 bytes of the disk space.

Hard Link:
As Hard Links point to the same location as that of Source files, they have exactly same size as that of Source files.

Memory Occupied

Soft Links:
As mentioned earlier, Soft Links save only the name of the file to which they are linked. Hence they consume extra memory in order to save the Source file name as data.

Hard Links:
Although they have exactly the same size as that of Source file, they do not require any extra memory as such, to store any data, as they just point to the location where contents of the file are placed.

Linking Across File Systems

Soft Link:
Yes, whenever you wish to create a link to the file or directory from some other file systems, you have to use Soft Links, because they use different Inode numbers than the Source files.

Hard Link:
Consider that, we are having two file systems 'FS1' and 'FS2' in our Linux system and file 'One' is present in 'FS1' and file 'Two' is in 'FS2'. The files 'One' and 'Two' can have exactly the same Inode numbers. This is why Hard Links do not work in cross file systems and they need same file system.

Linking to the Directories

Another advantage of using Soft Links. Soft Links do work at the directory level, but Hard Link do not.

Access Time

Soft Link:
As mentioned in previous sections, when Soft Link is accessed, it will first see the Source file name to which it is linked, then it will search for the location where contents of the Source file are kept and after that it will present the contents of that file to the user. A bit complex, isn't it?

Hard Link:
On the other hand, working of Hard Links is pretty straight forward. When we access the Hard Link, it has the location of the content, it will go and bring the contents for you.

Conclusion:
When Soft Links are used, the time required to have access to the file is on the slower side as compared to when Hard Links are used.

That's all about Soft Links and Hard Links!

0 comments:

Post a Comment