Tuesday, February 7, 2017

Hard links and soft links with examples ( The ln command )

Links are really tricky and interesting part on Unix/Linux operating systems, Unix comes with two types links Hard links and soft links , also called symbolic links

I will first talk about hard links,
lets consider following file ( MYHARDLINK.txt)  as an example



 To create a hard link you have to use "ln" command, see following example



 once i used "ln" command , it created another file  MYHARDLINK.txt with exact same size and same content too





Now have a closer look





what else is same ?
yes its the "inode" number ( 136912 )
let have a look what happens when you delete the original file
Will the file created during hard link creation will be deleted ?
Or it will remain unaffected ?




So that's not a secret anymore , deleting one of the "filename" wont affect the actual data , that is why you can see file created during hard link creation is not deleted, i said "filename" because both "MYURL.txt" and "MYHARDLINK.txt" are merely file names and they both are pointing to same data on disk

Now coming to the soft links
you can create soft links  using "ln -s"
i have a file called Europe.txt and i created a sof link to it "Britain.txt"




what do you see ?
"inode" numbers are different size of original files and file created during soft link creation is different, even though if you open any one of them you will see the same text.
and interestingly  you see .....an arrow !!!
one more thing you can notice the permissions of  "Britain.txt" starts with "l"
which is enough to tell you that its a soft link

i created one more soft link to file Europe.txt with name Germany.txt



What happens when you delete one of the soft link, what impact will it will cause to original file ?
lets see that



So it does not affect the original file
Now the question comes , what happens when you delete the original file ? How is it going to impact the
soft link

here is your answer


 once you delete the original file you can still see the soft link but it becomes unusable
while you try to open it, it behaves like it does not exists
but remember it is still consuming one inode in memory , so how do you get rid of it ?
either remove it using rm command or use unlink command  as demonstrated below 


 




One important difference between hard and soft link is that, hard link can not be created across two different file systems while is possible to create soft link ( symbolic link ) in such manner

See what happens when i try to create a hard link and soft link (NewZealand.txt) for File Australia.txt


 

Make sure you give absolute path to file while doing so in order to create a working soft link

I hope you liked my effort to give you basic practical knowledge about unix links, Please do comment and share, your suggestions are always welcomed


if you want learn to Basic Unix Commands in 1 Hour, here is the link

Basic Unix Commands in 1 Hour

if you want learn Unix/Linux Commands in detail, here is the link

Learn Unix/Linux Commands in detail

Also keep visiting my blog to learn more

unixtechworld.blogspot.com



No comments:

Post a Comment