December 3, 2018

Create a Symbolic Link in Windows

Symbolic Link, Soft Link or SymLink referred to the same thing, is a file that is linked to another file or directory, which can be on the same computer or any other on the network. You can create Symbolic Link by using mklink command, which is available since Windows Vista.

Syntax of mklink is:

MKLINK [[/D] | [/H] | [/J]] Link_File_Or_Directory_Path Target_File_Or_Directory_Path

As you can see from the above syntax, mklink allows following 3 switches to create different types of Symbolic Links.

  • /D Directory symbolic link.
  • /H Creates a hard link instead of a symbolic link.
  • /J Directory junction.

The default is a file symbolic link, i.e. if you did not specify and switch than the link created will be File Symbolic Link.

Lets start create each type of link with an example:

First we setup he environment for examples to follow. I created a folder named Symbolic Links at C:/. Inside Symbolic Links folder created another folder named Original, which will be act as Target Folder of symbolic links and contains two files file1.txt and file2.txt.

Create a Hard Link

In this example, I will create a Hard Link to file1.txt which we created inside our Target Folder named Original. The linked file will be created at path C:\SymbolicLink\H_Link.

  • Open the command prompt and go to the path C:\SymbolicLink
  • Create new directory H_Link with the following command

       mkdir H_Link
      
  • Run the following command:

       mklink /H "C:\SymbolicLink\H_Link\file1.txt" "C:\SymbolicLink\Original\file1.txt"
      

    If you get the following error message:

       You do not have sufficient privilege to perform this operation.
      

    Then just restart the command prompt with Administrator Privileges. If you are already running command prompt with Administrator Privileges then you will see the success message as follows:

       Hardlink created for C:\SymbolicLink\H_Link\file1.txt <<===>> C:\SymbolicLink\Original\file1.txt
      

    We have successfully create the Symbolic Link to a file (also know as Hard Link).

Create a Directory Symbolic Link

In this exmaple, I will create a Directory Symbolic Link to Target Folder named Original. The directory linked will be created at path C:\SymbolicLink\D_Link.

  • Open the command prompt and go to the path C:\SymbolicLink
  • Run the following command:

       mklink /D "C:\SymbolicLink\D_Link" "C:\SymbolicLink\Original"
      

    If you get the following error message:

       You do not have sufficient privilege to perform this operation.
      

    Then just restart the command prompt with Administrator Privileges. If you are already running command prompt with Administrator Privileges then you will see the success message as follows:

       symbolic link created for C:\SymbolicLink\D_Link <<===>> C:\SymbolicLink\Original
      

    If you run the DIR command at C:\SymbolicLink, you will see that the newly created directory link will be displayed as .

    Create symbolic links

    In windows explorer, the linked directory will be shown with icon similar to the shortcut icon, like this:

    Create symbolic links

Create a Directory junction

In this exmaple, I will create a Directory Symbolic Link to Target Folder named Original. The directory linked will be created at path C:\SymbolicLink\J_Link.

  • Open the command prompt and go to the path C:\SymbolicLink
  • Run the following command:

       mklink /J "C:\SymbolicLink\J_Link" "C:\SymbolicLink\Original"
      

    You will see the success message as follows:

       Junction created for C:\SymbolicLink\J_Link <<===>> C:\SymbolicLink\Original
      

    If you run the DIR command at C:\SymbolicLink, you will see that the newly created directory link will be displayed as .

    Create symbolic links

    In windows explorer, the linked directory will be shown with the icon similar to Directory Symbolic Link:

    Create symbolic links

It seems like Directory Symbolic Link and Directory junction works in the same way. But there is a difference. I will explain the difference in next post soon.

No comments:

Post a Comment