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



Saturday, February 4, 2017

cut command with examples

You may often need to slit a file in such way that output should be only some or few columns.
 Unix/ Linux comes with powerful sword for this and its called cut ( the name is simple to guess )

Lets consider following file for this example:



now if you want to see first character of each row, in other words if you want to "slit vertically  in such a way that you only see red highlighted part of the file, you should try to play with cut




                                                                                               
                                                                                                                                              and that is how you do it





you can even specify range off characters, see below two examples


and off course you can join two different range of characters as well, hope below example will explain you this





Now if you don't know the end of your files that means if you don't know, how many numbers of characters each row is having and still you want to see the all characters till end, starting with certain character numbers
of each line use "-"  as below  ( starting from 9th character in this case )



cut command is quite versatile it even works with delimiter, all you have to do specify delimiter ( see red highlighted part ) and name the fields you want want to based on delimiter you specified



Few more examples :

you can specify more then one field 





and range of fields too



                                                                                                                                                              you must have observed the difference between cut -d "," -f1,3 Example-1.txt and cut -d "," -f1-3 Example-1.txt , in first case it only prints specified columns (1st and 3rd)  in second case it prints range of column ( 1st to 3rd ).





I am concluding this post here , cut is a great text formatting tool when used with some other unix commands, I will discuss them in my future posts
Thanks for reading , have a nice day :)  




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