Hard and Soft links, hmm what are those?
Well we are going to see what are those and how they are technically different from one another, lets jump in
Most of the people who uses windows operating system should be familiar with the term ShortCuts, which is nothing but accessing a file indirectly... if you want me to put this in other terms, I would say referring the contents through other means.
Well in Linux and Unix like operating systems, this concept would remain same as far as Soft links are concerned well with Hard links the story is entirely different.
Let us justify ourselves through a small demonstration....
Create a file in your favourite location using the following command
From the above image, we take three important properties i.e Inode, Links and the type of the file which is nothing but regular empty file, for now let the file be empty and when we try dealing with Hard Links we put some contents in it.
Let us have the statistics of the two file
Statistics of mainfile.txt
Statistics of softlink-for-mainfile
If you look at the File property from both the stat it is very evident that the second file is a link to the first one. The File property is not the only which confirms that but the inode property too, please do have a look at it the values of inode are different.
Now I am going to move the main file to a different location in order to test the validity of the link that we created.
Try moving the file mainfile.txt using the following command...
From the picture above, it is evident that the soft link has become invalid when the file is removed/moved or deleted from the original location. You can also confirm this by trying display the contents of the file using the cat command as given below...
As said now we can conclude that the soft link becomes invalid without the original file.
So as far as soft links are concerned, we could have the below inference...
1. Soft links cannot stand alone
2. Soft links becomes invalid if the source/original file is moved to different location
3. Soft link and the original file are not the same but they are two different files.
Let us create a hard link of the file mainfile.txt using the command given below...
Now let us have the statistics of the both the files....
Statistics of mainfile.txt
Statistics of hardlink-for-mainfile
From the images above, you can infer the following details
1. Both the files pointing to the same inode
2. Both the files are regular empty file
3. The total number of links to the inode is 2, which is given against the property Links.
Regarding the links, if you try to have another hard link to the mainfile.txt... the links count would be increased to 3, let us see how is this
Try creating an other hard link to the file mainfile.txt
Now here comes the interesting part of the story, put some contents into the file mainfile.txt using the command as given below
As you can see the file has been changed from "regular empty file" to "regular file". Now, what would be your expectation on the files hardlink-for-mainfile and hardlink-for-mainfile-1, let us see from the screen shots given below...
Statistics of the hard link hardlink-for-mainfile and hardlink-for-mainfile-1
If we look at the image above everything remains same except the name of the file... which conveys all the three files are pointing to the same inode than the different one.
This can even be concluded by deleting the original file, Now try deleting the original file i.e mainfile.txt using the below command and look at the statistics of the hard links we created.
From the above image it is evident that, though the original file is deleted the hard links created would retain the inode that actually belongs to the original file.
So, we could conclude the morale of the story as follows...
1. soft link created on a file cannot stand alone without the original file
2. soft link has its own inode
2. hard links would retain the inode of the original file even though the original is deleted
3. hard links uses the same inode as that of the original file.
Hope you liked the story. Thanks.
Well we are going to see what are those and how they are technically different from one another, lets jump in
Most of the people who uses windows operating system should be familiar with the term ShortCuts, which is nothing but accessing a file indirectly... if you want me to put this in other terms, I would say referring the contents through other means.
Well in Linux and Unix like operating systems, this concept would remain same as far as Soft links are concerned well with Hard links the story is entirely different.
Let us justify ourselves through a small demonstration....
Create a file in your favourite location using the following command
> touch mainfile.txtLet us see the statistics of the file mailfile.txt using the following command...
> stat mainfile.txt
From the above image, we take three important properties i.e Inode, Links and the type of the file which is nothing but regular empty file, for now let the file be empty and when we try dealing with Hard Links we put some contents in it.
Soft Link
Now let me create a soft link for the file mailfile.txt, the command given below would create a soft link for the file mainfile.txt
> ln -s mainfile.txt softlink-for-mainfilethe above command would create a new file named softlink-for-mainfile which would point to the file mainfile.txt and not the inode of the file mainfile.txt, technically the actual file i.e mainfile.txt and the link to it are two different files. the soft link to the actual file has no meaning in the absence of the main file.
Let us have the statistics of the two file
Statistics of mainfile.txt
Statistics of softlink-for-mainfile
If you look at the File property from both the stat it is very evident that the second file is a link to the first one. The File property is not the only which confirms that but the inode property too, please do have a look at it the values of inode are different.
Now I am going to move the main file to a different location in order to test the validity of the link that we created.
Try moving the file mainfile.txt using the following command...
> mv mainfile.txt ../
From the picture above, it is evident that the soft link has become invalid when the file is removed/moved or deleted from the original location. You can also confirm this by trying display the contents of the file using the cat command as given below...
> cat softlink-for-mainfile
As said now we can conclude that the soft link becomes invalid without the original file.
So as far as soft links are concerned, we could have the below inference...
1. Soft links cannot stand alone
2. Soft links becomes invalid if the source/original file is moved to different location
3. Soft link and the original file are not the same but they are two different files.
Hard Link
Let us now deal the second part of the story i.e the Hard linksLet us create a hard link of the file mainfile.txt using the command given below...
> ln mainfile.txt hardlink-for-mainfileAfter the execution of the command, you would have a file named hardlink-for-mainfile at the location where you executed the command, this can be witnessed from the image below...
Now let us have the statistics of the both the files....
Statistics of mainfile.txt
Statistics of hardlink-for-mainfile
From the images above, you can infer the following details
1. Both the files pointing to the same inode
2. Both the files are regular empty file
3. The total number of links to the inode is 2, which is given against the property Links.
Regarding the links, if you try to have another hard link to the mainfile.txt... the links count would be increased to 3, let us see how is this
Try creating an other hard link to the file mainfile.txt
> ln mainfile.txt hardlink-for-mainfile-1now if you look at the stat of the file mainfile.txt, the Links property of the inode would have increased by 1, to justify that have a look at the screen shot given below...
Now here comes the interesting part of the story, put some contents into the file mainfile.txt using the command as given below
> echo "hello world" > mainfile.txtnow let us have the stat of the file mainfile.txt
As you can see the file has been changed from "regular empty file" to "regular file". Now, what would be your expectation on the files hardlink-for-mainfile and hardlink-for-mainfile-1, let us see from the screen shots given below...
Statistics of the hard link hardlink-for-mainfile and hardlink-for-mainfile-1
If we look at the image above everything remains same except the name of the file... which conveys all the three files are pointing to the same inode than the different one.
This can even be concluded by deleting the original file, Now try deleting the original file i.e mainfile.txt using the below command and look at the statistics of the hard links we created.
> rm mainfile.txt
From the above image it is evident that, though the original file is deleted the hard links created would retain the inode that actually belongs to the original file.
So, we could conclude the morale of the story as follows...
1. soft link created on a file cannot stand alone without the original file
2. soft link has its own inode
2. hard links would retain the inode of the original file even though the original is deleted
3. hard links uses the same inode as that of the original file.
Hope you liked the story. Thanks.
No comments:
Post a Comment