Use mklink command in Windows 7

February 21st, 2012

This post will show you how to use mklink command in windows 7 ,and will also talk about difference between SYMLINK(option /D) and SYMLINKD(option /J)

C:\>mklink /?
Creates a symbolic link.
 
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.

Some mklink examples:


1.Create symbolic link and hard link for file.

C:\test>dir
Volume in drive C is Primary
Volume Serial Number is 8E0A-1C36
 
Directory of C:\test
 
02/21/2012 05:19 PM <DIR> .
02/21/2012 05:19 PM <DIR> ..
02/21/2012 05:19 PM <DIR> targetdir
02/21/2012 05:14 PM 16 targetfile.txt
1 File(s) 16 bytes
3 Dir(s) 61,293,813,760 bytes free
 
C:\test>mklink link.txt targetfile.txt
symbolic link created for link.txt <<===>> targetfile.txt
 
C:\test>mklink /H linkH.txt targetfile.txt
Hardlink created for linkH.txt <<===>> targetfile.txt
 
C:\test>dir
Volume in drive C is Primary
Volume Serial Number is 8E0A-1C36
 
Directory of C:\test
 
02/21/2012 05:20 PM <DIR> .
02/21/2012 05:20 PM <DIR> ..
02/21/2012 05:20 PM <SYMLINK> link.txt [targetfile.txt]
02/21/2012 05:14 PM 16 linkD.txt
02/21/2012 05:19 PM <DIR> targetdir
02/21/2012 05:14 PM 16 targetfile.txt
3 File(s) 32 bytes
3 Dir(s) 61,293,813,760 bytes free

2.Difference between symbolic link and hard link in windows.
For symbolic link ,we can just treat it as a shortcut,if the target been deleted or moved,then we can not access it via the symbolic link,below example
just did the test

C:\test>type link.txt
For test purpose...
C:\test>type linkH.txt
For test purpose...
C:\test>del targetfile.txt
 
C:\test>type link.txt
The system cannot find the file specified.
 
C:\test>type linkH.txt
For test purpose...

3.Make symbolic link and directory junction for a folder

C:\test>mklink /D dirD targetdir
symbolic link created for dirD <<===>> targetdir
 
C:\test>mklink /J dirJ targetdir
Junction created for dirJ <<===>> targetdir
 
C:\test>dir
Volume in drive C is Primary
Volume Serial Number is 8E0A-1C36
 
Directory of C:\test
 
02/21/2012 05:33 PM <DIR> .
02/21/2012 05:33 PM <DIR> ..
02/21/2012 05:33 PM <SYMLINKD> dirD [targetdir]
02/21/2012 05:33 PM <JUNCTION> dirJ [C:\test\targetdir]
02/21/2012 05:22 PM <DIR> targetdir
0 File(s) 0 bytes
5 Dir(s) 61,291,851,776 bytes free

Difference between SYMLINKD (/D) and JUNCTION (/J) when using mklink command

  1. Both symlinkd and junction are symbolic link,when the target been moved or removed,there will just be fakes.
  2. Symlinkd is a newer feature than junction,and it can across file volumes,but junction only can make junction directory in local volume.Eg:we can make symlinkd link to a nfs diretory
  3. Although symlinkd is new powerful than junction,but some applications do not support it correctly,so if junction can work,just use junction

Leave a Reply