Create Symbolic Links, Hard Links and Directory Junction Points in Windows with MKLINK

»»»Create Symbolic Links, Hard Links and Directory Junction Points in Windows with MKLINK

For Unix-like or Linux system users, symbolic link is a common feature in use almost daily. Symbolic link (aka soft link) or symlink as it often shortened to, is a special type of file or file-system object that serves as a reference or points to another file, UNC or directory, known as target. In Windows systems such Windows Vista and Windows Server 2008, e.g. Windows 7, Windows 8, Windows 8.1, Windows 10, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2 and Windows Server 2016, symlinks feature has been added to NTFS filesystem and can be created by using MKLINK command.

In previous Windows operating system such as Windows XP, the closer thing to symbolic links is NTFS junction point, or worse, Windows shell shortcut (.lnk) file.

Symlink in Windows, unlike .lnk shortcut in desktop or Windows Explorer, allow user to access files within the symbolic link created itself via File Explorer, the console and etc. And symbolic link also differ from NTFS junction point which can only link to folders and volumes, in which symlinks can point to a file, a UNC, a folder or a volume, as well as able to span file systems.

Symbolic link is useful when you have a lot of folders and files are scattered all over the directory tree, and you need to manage them from a single location. Another scenario is that you have a deep nested file that you want to access quickly instead of traverse through one branch by one branch. And furthermore, for programmer, symlink provides a static file path that can be point to a ever changing object without affecting the functionality of the program. In all these situation, symbolic links can be created at a convenient location which point to the ‘real’ objects.

As mentioned, to create a symbolic link, use MKLINK command line tool. MKLINK can be used to create a hard link or directory junction (junction point) too. Hard link is essentially giving another name or label to the objects, while NTFS directory junction point is used to redirect the whole folder to another folder as if it’s the original folder itself. Junction point is used extensively in Windows to provide support for old directory structures for user profiles (i.e Documents and Settings) to point to the new user folders located inside Users directory.

Syntax and Options of MKLINK

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

/D – Creates a directory symbolic link. Default to 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.

Examples and Usages of MKLINK

Note: Only Administrators can use MKLINK to create symbolic links unless override by using secpol.msc, so you need to run Command Prompt with elevated privileges, or else you will get “You do not have sufficient privilege to perform this operation.” error.

To create symbolic link called foo to reference to c:\Windows\System32\notepad.exe:

C:\test>mklink foo c:\Windows\System32\notepad.exe

You will see the following result:

symbolic link created for foo <<===>> c:\Windows\System32\notepad.exe

If you type dir in commnd prompt, you will see the following listing:

 Volume in drive C has no label.
 Volume Serial Number is 2211-7428

 Directory of C:\test

04/14/2006  11:24 AM    <DIR>          .
04/14/2006  11:24 AM    <DIR>          ..
04/14/2006  11:24 AM    <SYMLINK>      foo [c:\Windows\system32\notepad.exe]
               1 File(s)              0 bytes
               2 Dir(s)  69,238,722,560 bytes free

To create symbolic link to a folder, simply use /D switch, or /J for junction point (directory junction is not available for remote network location):

C:\test>mklink /d bar c:\Windows
symbolic link created for bar <<===>> c:\Windows

Results of dir command:

 Volume in drive C has no label.
 Volume Serial Number is 2211-7428

 Directory of C:\test

04/14/2006  11:24 AM    <DIR>          .
04/14/2006  11:24 AM    <DIR>          ..
04/14/2006  11:24 AM    <SYMLINKD>     bar [c:\Windows]
04/14/2006  11:24 AM    <SYMLINK>      foo [c:\Windows\system32\notepad.exe]
               1 File(s)              0 bytes
               3 Dir(s)  69,238,722,560 bytes free

MKLINK cannot be used to delete symbolic link. To remove a symbolic link, simply delete them as if you’re removing a normal file. For example, to delete the foo symbolic link created above, enter the following command:

C:\test>del foo

To remove the bar symbolic link to a folder created above, use the following command:

C:\test>rd bar

By | 2016-12-29T07:21:23+00:00 December 29th, 2016|Categories: Windows|Tags: , |13 Comments

About the Author:

LK is a technology writer for Tech Journey with background of system and network administrator. He has be documenting his experiences in digital and technology world for over 15 years.Connect with LK through Tech Journey on Facebook, Twitter or Google+.
  • david

    I cannot figure out how to go to or switch to a symbolic link after it is created. How do you do it? I can't find instructions for this mentioned anywhere.

  • Ed

    Thanks for the post, but I don't see the point of creating a symlink to a file. In your example, you created a symlink called "foo" pointing to notepad.exe, but what can you do to the file "foo"? It's not executable, even after I rename it to foo.exe.

    Thanks.

  • ludo

    you can't execute symlinks, for that you need to make a shortcut the normal way. A symlink is more usefull for ppl to read a certain file wich is in a folder they normaly can't go to. Or the same file is needed on 2 different folder locations, but you dont want to edit 2 seperate files all the time.

  • DrLecter

    JUNCTION.EXE is a command line tools that allows local symlinks in NTFS volumes under windows XP.

  • Donato

    Buongiorno,
    mi complimento per il tuo lavoro, ma ho da sottoporti un quesito difficilissimo, che se riuscirai a risolvere ti farò una DONAZIONE.

    Ho la necessità di fare un LINK SIMBOLICO alla cartella DESKTOP, ma purtroppo dopo averlo fatto non mi sarà più possibile cancellare file o cartelle dal DESKTOP a causa di una limitazione di EXPLORER di WIN XP, mentre il problema è stato risolto in VISTA!
    Se trovi la soluzione sei un grande ed io compenserò il tuo sforzo per averla trovata.
    Saluti.

  • Donato

    Hello,
    I congratulate you on your work, but I have to sub a difficult question, that if you manage to resolve you make a DONATION.

    I need to make a symbolic link to the DESKTOP folder, but unfortunately after the fact you can not delete files or folders from DESKTOP due to a limitation of EXPLORER WIN XP, but the problem is resolved in VISTA!
    If you find the solution you're a big and I will compensate you for your effort have found.
    Greetings.

  • harto

    En el segundo párrafo pone:
    "los enlaces simbólicos pueden apuntar a un archivo, un Mando de las Naciones Unidas, una carpeta o volumen, así como capaz de abarcar los sistemas de archivos."
    Pues yo, por mucho que lo intento no consigo apuntar a un Mando de las Naciones Unidas con MKLINK

  • skattegatte

    aooooooooo!!!!! ma se po' sape' ando azzo stai ??? w l'itlia anfammmmmmiiiiiiii …….FIGLIO DI TROTA ..MAN: YOU SKI LITTLE ?….
    STUPID MAN : YES

    MUAAAAAAAAAA AHAHAHAHHAH TE TO LI FKOE SOE WER BHSY IN TO KIE WS CAIID …STPIK EAD IN BOTLAE ANSOWIMA———))))STUPID ARE YOU ???

  • GCV

    Why do you need to be an administrator to use this feature? I sometimes think that every developer in Redmond should have Windows taken away for a year so they can learn some other viewpoint other than their own. Understanding how useful symlinks are in general usage would be quite enlightening to them.

  • Pingback: Vorsicht bei NTFS Junction Points! – nodomain.cc()

  • Tom

    There is a little utility I discovered for creating sybolic directory links in Windows 7 that works great

    dirlinker.codeplex.com

  • 4563456

    To switch to your symbolic link symply use:

    cd yourlinkname

  • Art Bacon

    Please fix the grammar. The paragraph that says: "MKLINK cannot use to delete symbolic link" should say "MKLINK cannot be used to…"

STAY IN TOUCH

close-link