Thursday 19 November 2015

Symbolic Link

Hi Friends,

Today I am back with very small thing but I was not aware and faced some problem with that. So I am here with the thought that this might help someone. 

Did you heard about “Symbolic Link”?

I did not heard about it earlier but found a requirement in one of the project as per suggestion from my coordinator. Then I started exploring it and found that in Windows environment it is just like a shortcut which points to another location. 

But later i found that there is some differences in Symbolic link and shortcuts. Shortcuts need a specific application to make it working which is supported by windows like operating system. But in case of Linux/Unix based system this is not working (not declaring this but found such in initial phase). 

Shortcut is just a file which keep information of target file or directory with additional information like icon etc. But very important fact is that Shortcut is a file and not behaves as a directory to navigate to targeting location. Here symbolic link comes into existence in my requirement.

Definition:- In computing, a symbolic link (also symlink or soft link) is a special type of file that contains a reference to another file or directory in the form of an absolute or relative path and that affects path name resolution.

So in my words, Symbolic link is a bridge between two directories (or directory to file) which are linked through a symbolic link file. This symbolic link file provides a door to navigate directly to the targeting location and behaves like an actual directory/sub directory which can be used/referenced in path while saving or accessing file.

Now point is how to create symbolic link, so below are the steps to create symbolic link.

Windows Environment :- In windows environment, symbolic link creation is easy through command prompt.
Step 1 :- Open command prompt with run as administrator option.
Step 2:- Use mklink command to create symbolic link as given below 

MKLINK [[/D] | [/H] | [/J]] Link Target

/D      Creates a directory symbolic link.  Default is a file
symbolic link.
/H      Creates a hard link instead of a symbolic link.
/J      Creates a Directory Junction.
Link    specifies the new symbolic link name.
Target  specifies the path (relative or absolute) that the new link
refers to.

For ex. Suppose we need to create a symbolic link named sftp from the root directory to the \Users\User1\data directory, then type:

mklink /D \sftp \Users\User1\data

This will create a symbolic link “sftp” at root which will navigate user to “\Users\User1\data” directory when opened.

2. Linux environment :- In Linux/Unix based environment “ln” command is used to create link. With “-s” parameter it creates a symbolic/soft link. Below is “ln” command to create a symbolic link.

ln -s [source_dir] [Link_Name]

For ex. Suppose we need to create a soft link named as “sftp” at root which should point to “/var/opt/data”. Then:

ln -s /var/opt/data /sftp  

This will create a soft link “sftp”which will point to “/var/opt/data”

To remove existing symbolic link navigate to the directory where symbolic link exists and then execute below command.

rm [Link Name]

For Ex. If “sftp” symbolic link exist already then we can remove it through below command

rm sftp

Now below are few important points while creating or removing links.
1. Create specific type of link as per need (i.e Soft or Hard)

2. By removing soft link it just removes soft link file and actual linked file or directory is safe.

3. By removing a link which is hard link and created with /h or -h option then it actually represents target file or directory and removing such link may remove actual linked file or directory as well. So be careful with it.

4. If you have created a soft link “sftp”at “/tmp/sftp” then you can use it to create new file or directory at targeting location. For ex. If sftp pointing to “/var/opt/data”then you can try to create a directory at “/tmp/sftp/dev-data”which will create “dev-data” directory at targeting location that means at “/var/opt/data/dev-data”

5. Similarly you can access a file from targeting location using this soft link like “/tmp/sftp/test.html”where “test.html” is placed at linked location (i.e. “/var/opt/data/test.html”)

I hope it will give you some idea about symlink/symbolic link or soft link and about their use and you can start thinking where you can gain an advantage from this.

So keep exploring and share your thoughts and findings.

References

https://kb.iu.edu/d/abbe