A symbolic link is used to describe a file, that doesn’t store any data. Symbolic Links on Windows contains a reference to another file or directory in the respective of an absolute or you can say to the relative path. The type of path (relative/absolute) is defined during the creation of the link.
Most operating systems offer support for Symbolic Links in one way or another. Linux and Windows both provide support for generic Symbolic Links with some OS exclusive features, i.e. Windows allows for the creation of Junction points which are folder soft links with a little different working. In this article, we will take a look at the creation of symbolic links on Windows using mklink command found in the command processor (cmd) of the OS.
The command requires administrator privileges to execute.
Types of symlinks :
Description of the command :
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.
The above text could be obtained by executing the mklink command without any arguments.
Creating a soft link to a file :
In order to create a soft link, the syntax of the command is:
mklink Link_path Target_path
- Where Link_path is the name (or path) to the symbolic link which is to be created.
- Target_path is the path which the new link will refer to.
There exists the a file with the path C:\suga\settings
In order to create a soft link of the file on the same path with a different name (ex. apple), the command would look as follows.
mklink "C:\suga\apple" "C:\suga\settings"
In the above command, both paths are absolute.
After the execution of the above command, a soft link to the file will be created appears as follows.
The same method could be used for creating a soft link to a directory as well, the only difference being that the /D switch needs to be added to the command.
making the syntax :
mklink /D Link_path Target_path
Creating a hard link to a file :
In order to create a soft link, the syntax of the command is as follows.
mklink /H Link_path Target_path
In this example, we would use the same file as the one used in the last example, located at C:\suga\settings. In order to create a hard link of the file at the same path with a different name (ex. moba) the command would look as follows:
mklink /H "C:\suga\moba" "C:\suga\settings"
After the execution of the above command, a hard link to the file will be created appearing as follows :
Hard links could not be created for directories, therefore unlike the previous example, both paths (Link & Target) should point to a file only.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.