玩轉WIN7的MKLINK
引言:
換了新電腦,終於再次使用上啦WIN7 ,經過一個周每天重裝N次系統,... ... ... ...
在xp系統下,junction命令要用微軟開發的小程序 junction.exe實現,從http://live.sysinternals.com/可以下載。
現在Windows Vista之后的系統自帶了mklink的功能。
Windows操作系統啟動時不支持符號鏈接。所以不應該對下列文件夾重定向:
- 目錄包含了hiberfil.sys (如果被配置在根目錄以外)
- \Windows
- \Windows\system32
- \Windows\system32\Config
Windows Installer不完整支持符號鏈接。對\Windows\Installer重定向會導致多數基於.msi的Windows installers失敗報告error 2755 或 error 1632.
可以重定向下列目錄:
- \Documents and Settings
- \ProgramData
- \Program Files
- \Program Files (x86)
- \Users
但是對\Users與\ProgramData重定向不被推薦,因為會打破Windows Update與Windows Store Apps.
Windows Vista中預先創建了一些符號鏈接(Junction point),以增進與舊版操作系統(Windows XP)間的兼容性,部分示例如下:
- C:\Document and Settings → C:\Users(連接點)
- C:\Users\All Users → C:\ProgramData(目錄符號鏈接)
MKLINK [[/D] | [/H] | [/J]] Link Target /D 創建目錄符號鏈接。默認為文件符號鏈接。
/H 創建硬鏈接,而不是符號鏈接。 /J 創建目錄聯接。 Link 指定新的符號鏈接名稱。 Target 指定新鏈接引用的路徑(相對或絕對)。
默認情況:
- 文件的符號鏈接(SYMLINKD),如沒有參數指定,則創建文件的符號鏈接,刪除文件鏈接不影響目標文件,且創建鏈接時允許目標文件不存在;
- 目錄的符號鏈接(SYMLINKD) /D該參數可以創建目錄的符號鏈接,刪除目錄鏈接不會影響目標目錄,且創建鏈接時允許目標目錄不存在;
- 目錄的軟鏈接(JUNCTION) /J 該參數可以創建目錄的軟鏈接(聯接),作用基本和符號鏈接類似,NT6系統的用戶目錄就是以這種形式存在的;
- 文件的硬鏈接 /H 該參數可以創建文件的硬鏈接,即一個文件的多個別名,NT6系統WinSXS目錄下的大部分文件是以這個形式存在的;
NTFS符號鏈接(Symbolic Link),又稱符號鏈接,是NTFS文件系統中指向文件系統中的另一個對象的一類對象。
被指向的對象叫做“目標”。它們可以像普通文件一樣操作,但所有對符號鏈接的操作都實際作用於目標對象。
符號鏈接對用戶而言是透明的,符號鏈接看上去和普通的文件和文件夾沒有區別,操作方法也一模一樣。
NTFS符號鏈接的設計目的是為了和POSIX標准的操作系統兼容、並用來增進與舊版操作系統的兼容性,它首次出現於Windows Vista。
Windows 2000開始就有的NTFS連接點,只能用於目錄,且只能用於本地文件系統。
而符號鏈接既可以指向本地文件,也可以指向遠程SMB網絡路徑。
NTFS連接點只支持本地驅動器的絕對路徑,但NTFS符號鏈接允許使用相對路徑。
還有,NTFS符號鏈接允許跨文件系統使用,但是對於跨主機的符號鏈接,還需要遠程機器也支持這一功能才行。
硬鏈接只能用於文件,不能用於目錄。
硬鏈接相當於一個實體文件有多個彼此平等的名字。
刪除一個文件名字后,還可以用其它名字訪問該文件。
硬鏈接只能用於同一個磁盤分區內的文件。
硬鏈接不能用於空文件,而符號鏈接可以用於空文件或空目錄。
符號鏈接(Symlink,Softlink)是對文件或目錄的引用,實際上符號鏈接本身是一個
“記錄着所引用文件或目錄的絕對或相對路徑”的特殊文件,通過符號鏈接的操作都會被重定向到目標文件或目錄。
交接點(Junction),也稱為“再分析點”,是 NTFS 3.0 及以上文件系統(Windows 2000 及以上系統)的特性,
它是鏈接本地目錄(可跨卷)的訪問點,通過交接點的操作都會被系統映射到實際的目錄上。
通過建立交接點,可以在保證一個目錄實例(目錄的一致性)的前提下,允許用戶或程序從本地文件系統中的多個位置訪問此目錄。
對符號鏈接和快捷方式的“讀、寫、遍歷”等操作都會被重定向到目標文件或目錄;
但對它們的“復制、刪除、移動、配置 ACL”等操作只針對自身;
符號鏈接不但可以應用於本地系統,還可以應用 UNC 路徑。
對交接點內文件和子目錄的“建立、刪除、修改”等操作都被映射到對應的目錄中的文件和子目錄上;
對交接點的“復制、粘貼、剪切、配置 ACL”,只會影響此交接點;
在同一卷內移動交接點,只會影響此交接點;但在不同卷間移動交接點,會將此交接點轉換為正常目錄,並且交接點對應目錄下的所有內容都會被移動;
通過“資源瀏覽器(Explorer.exe)”和“命令行 del”等工具刪除交接點時,會同時刪除對應目錄下的所有內容(但不會刪除目錄);
可通過“linkd.exe /d”安全的刪除交接點;但在 vista 及以后的系統中,對交接點的刪除會被正確的處理。
符號鏈接 symlink 與 交接點 junction 在重定向對目錄下的子目錄和文件的操作上基本沒有什么區別。
它們兩最大的區別在於 符號鏈接 symlink 可以用於引用文件,而 交接點 junction 只能引用目錄。
創建符號鏈接和軟鏈接時允許目標文件或目錄不存在,此時若訪問鏈接文件會得到一個“位置不可用”的提示,
若之后重新將目標文件或目錄補上(路徑、名稱一致即可),則該鏈接仍然有效;
目錄符號鏈接和軟鏈接的區別在於,軟鏈接在創建時會自動引用目標目錄的絕對路徑,而符號鏈接允許相對路徑的引用,
如分別用 mklink /D dira tdir 和 mklink /J dirb tdir 創建 dira、dirb 對 tdir 的符號鏈接和軟鏈接,
之后將 dira、dirb 移動到其它目錄下,則訪問 dira 時會提示“位置不可用”,訪問 dirb 時仍然正常指向 tdir;
而分別用 mklink /D dira c:\demo\tdir 和 mklink /J dirb c:\demo\tdir 創建 c:\demo\tdir 的符號鏈接和軟鏈接,
再將這兩個目錄鏈接移動到其它目錄下,則 dira 和 dirb 均可正常指向 c:\demo\tdir;
由此可見當創建目錄鏈接時對目標目錄使用絕對路徑,D 和 J 兩個參數實現的目錄鏈接效果是一樣的
只能創建文件的硬鏈接,使用/H 參數創建目錄的硬鏈接時會提示拒絕訪問;
硬鏈接是同一個文件的不同別名,例如對
a.txt 創建了b.txt 和c.txt 兩個硬鏈接,此時 a.txt、b.txt 和 c.txt 均是同一個文件的別名,
若刪除其中任意一個或兩個,則剩下的那個文件(無論是事先存在還是之后創建)不受影響,仍然指向原先那個文件。
創建文件或目錄鏈接僅限於 NTFS 文件系統,且鏈接只能針對同一個磁盤上的文件或目錄;
硬鏈接只能創建對本分區文件的引用,即不可創建跨分區的文件硬鏈接;
通過硬鏈接創建的別名文件在統計大小時會按原文件的大小計算,而軟鏈接和符號鏈接大小均為零;
硬鏈接在創建時會事先檢測目標文件是否存在,即不允許空鏈接的存在。
通過mklink命令的幫助信息,可以看到mklink可以創建目錄和文件鏈接。
比如D:\temp目錄下有目錄dir1,我想在當前目錄下創建到dir1目錄的軟鏈接,可以使用如下命令來操作:
mklink /D dir2 dir1
mklink /J dir3 dir1
上面兩個命令都可以實現到dir1的軟鏈接,參數/D和/J是創建目錄軟鏈接必須的,目前來說着兩個參數都可以實現目錄軟鏈接,
而且應用過程中也沒有發現有什么區別的地方,唯一的區別就是dir2和dir3鏈接的類型不懂,可以通過dir命令查看得到結果如下:
2011/05/17 14:05 <DIR> dir1 2011/05/24 13:07 <JUNCTION> dir2 [D:\temp\dir1] 2011/05/24 13:07 <SYMLINKD> dir3 [dir1]
我們可以發現dir1是DIR類型(這個是我們創建的實際目錄),dir2是JUNCTION類型,而dir3是SYMLINKD類型,這個或許是區別,但應用中效果一樣。
說完了目錄軟鏈接,文件當然也可以創建軟鏈接了,而且還可以創建硬連接,比如d:\temp目錄下有abc.txt文件,我要創建到這個文件的軟鏈接的命令如下:
mklink abc1.txt abc.txt
這樣我們就創建了一個到abc.txt文件的軟鏈接abc1.txt,對於文件還可以創建硬鏈接,硬鏈接個人感覺用處不大,因為創建硬鏈接是磁盤上空間是增加了雙倍,
對於硬鏈接而言,修改硬鏈接文件和原始文件的內容都會相互影響,即內容是一致的但空間占用確是雙份的。使用如下命令創建硬鏈接:
mklink /H abc2.txt abc.txt
文件的軟鏈接和硬鏈接的類型查看如下(用dir命令):
2011/05/24 13:33 7 abc.txt 2011/05/24 13:30 <SYMLINK> abc1.txt [abc.txt] 2011/05/24 13:33 7 abc2.txt
發現硬鏈接和普通的文件沒有什么區別,唯一的區別就是修改的內容會直接影響到原始文件。
順帶補充一句,軟鏈接的目錄和文件都可以直接刪除,不會影響到原始目錄和文件的,這個也非常方便,
而且軟鏈接的目錄和文件圖標上都有一個小箭頭,很好區分。
使用/D建立的連接是類似於linux下的軟連接,不占存儲空間; 使用/J建立的連接類似於Linux下的硬連接,占用同源文件一樣的存儲空間; Link和Target文件夾內容一模一樣,如果你對其中任意一個文件夾里面的內容做修改,另一個也會相應的改變。
通過建立符號鏈接(symbolic)磁盤空間使用量沒有改變哦,因為只是實現連接罷了,並不是將原文件復制以及同步。
使用連接目錄時就像使用原目錄一樣,就像實實在在有這個目錄一樣,程序不會發現連接目錄與普通目錄有何不同,這是一般的文件夾快捷方式是不行的。
與快捷方式的相同與不同之處:
-
快捷方式無論目標是文件還是目錄,生成的都是新的有內容的
.lnk文件,
打開時是打開的源路徑,直接編輯則是編輯.lnk文件的內容,源文件/目錄不受影響。MKLINK生成的文件和目錄都是 0 字節,但訪問時仍然是新文件/目錄的路徑,且編輯操作編輯的是源文件/目錄;
原理相當於網盤的同步,只不過不通過網絡、不占用空間。 -
刪除兩者生成的目標文件/目錄都不會對源文件/目錄產生影響。但刪除源文件/目錄后,新路徑都不再可用。
Junction Link Magic (340 kB - Windows 2000/XP/2003/Vista/2008/Windows 7) is a utility
that lets you create junction points with Windows 2000, XP, 2003, Vista, Windows Server 2008 and Windows 7.
Junction Link Magic automatically lists existing junction points, and it offers an easy interface to add, modify or remove junction points.
Junction Link Magic is freeware.
Download Junction Link Magic Location 1
Download Junction Link Magic Location 2
Download older version of Junction Link Magic for Windows 2000 and XP

A junction point is a special type of reparse point.
Reparse points are redirections in the Windows file system.
There are 3 types of reparse points:
- Symbolic links - can be thought of as a shortcut to a file or folder elsewhere in the file system
- Junction points - can only point to a folder
- Mount points - is a folder on a disk that points to an entire disk volume
Junction Link Magic will list all 3 types, not only junction links. With Junction Link Magic, you can also create and remove junction links.
A junction point is a technology for a folder to be grafted into another folder on the same local computer.
This might not seem like a big deal, but it can remove a lot of clutter.
It also helps a lot when moving programs from one place to another,
since just about every program in the Windows world expects to never be moved from the directory it was installed in.
E.g. moving your "Program Files" directory contents to another drive, and linking the original "Program Files" directory to this new location.
With junction points you can also surpass the 26 drive letter limitation.
Junction points are transparent to programs.
Starting with Windows Vista, junction points are used extensively in the Windows operating system.
Several of the special folders in Windows, such as the "Documents and Settings" folder,
are shown as shortcuts to other folders, but are actually junction points.
Unfortunately, Windows comes with almost no tools for creating junctions.
Prior to Windows Vista, you had to purchase the Windows Resource Kit, which offers the LINKD program for creating junctions.
In Vista, there is the MKLINK utility for the command line, which is not easy to use.
So that's where Junction Link Magic comes in...
Junction points can only be created on volumes formatted with NTFS 5.0 or higher.
NTFS 5.0 is supported in Windows 2000, Windows XP, Windows 2003 and higher.
symlinker
Symlink Creator
Program to create symlinks. Provides the CMD command as well.
Description
Just a small application I made to simplify the process of creating symbolic and hard links in Windows 7 and Windows 8 (likely Vista too).
It is a GUI for the MKLINK command in Windows, which makes creating the links much easier.
The program requires administrator permissions due to use of the command prompt, so you should run it as an administrator if you experience any problems.
Note: It is not possible to create a hard link from a network share. You must use a symbolic link instead.
Warning:
If you incorrectly specify the destination path, such as having the destination and source paths backwards,
it will delete the file/folder you are presumably trying to create a symlink from.
You will be presented with a confirmation prompt that shows you which folder will be replaced/deleted.
It is always a good idea to test this app on empty files or folders.
Features
- Can create symbolic link or a hard link automatically.
- Provides the CMD command if manual execution is desired.
http://downloads.sourceforge.net/project/symlink-creator/Symlink%20Creator%201.3/SymlinkCreator.exe
Windows Symbolic Link Maker
Description
This is a GUI interface to make symbolic link for windows vista(or up)
Does exactly what it states. Supports both file links (with hard links) and directory links.
No fuss, no .net required, no install needed.
I used to use DirLinker but it does not work anymore, this does. Just what I needed.
NTFS Link Ext
Description
This is a spin-off from Michael Elsdoerfer's original NTFS Link (hosted at Novell Forge) to incorporate some fixes and extended features.
NTFS Links
Features
Alax.Info NTFS Links extends Microsoft Windows XP operating system shell by adding additional features related to soft and hard links supported by NTFS file system. The description of the features is provided below.
Shell Extensions
The shell is extended with the following new interface items:
- Additional user interface commands:
- new item to directory context menus to accept dropped directories as soft links
- new item to directory context menus to paste copied to clipboard directories as soft links
- new item to directory context menus to accept dropped files as hard links
- new item to directory context menus to paste copied to clipboard files as hard links
- new directory column to display soft linked directory’s link target directory
- new directory column to display file’s number of hard links
- new info tip item to soft linked directories to display link target
- new info tip item to display file’s number of hard links
- icon overlay to display soft linked directories with a shortcut image
- Shell operation changes:
- an option to create move a soft link when a soft linked directory is being moved
- an option to create a soft link when a directory is being copied
- an option to copy a soft link when a soft linked directory is being copied
- an option to delete link when a soft linked directory is being deleted
Download
Current version of Alax.Info NTFS Links can be downloaded from:
- AlaxInfoNtfsLinksSetup.msi – Version 1.1.5, 32-bit
- AlaxInfoNtfsLinksSetup-x64.msi – Version 1.1.5, 64-bit (x64)
Alax.Info NTFS Links is free to use.
NTFS links 的使用是這樣的,安裝后,右鍵拖動任何文件/文件夾,在右鍵菜單中有 Creat Soft Link Here 選項,
選中后就會出現一個名為 Soft Link to XXX 的文件,查看屬性,大小也與原文件相同。
但這個 Soft Link to XXX 是個虛擬文件,你在這里的所有操作都會被繼承到原文件中,
包括改名,刪除。而在原文件中的操作也同樣會繼承到虛擬文件中。
你說的這個鏈接,NTFS5 有類似的實現,但是要比 Linux 下面的 soft link/hard link 要差一些
把一個目錄指向另一個目錄,可以使用 NTFS 的 directory junction point 功能,
這個功能微軟只提供了 API 但在explorer.exe/cmd.exe 里面沒有提供直接支持,
你可以下載sysinternals 寫的命令行工具 junction.exe,注意 rmdir junction point 時會刪除實際的目錄!
如果只想刪除 junction point 的話,要用junction -d
如果是 Vista 的話,微軟提供了一個命令 mklink,使用方法可見這里
如果喜歡圖形界面的話,可以考慮開源的 ntfslinkext,或者免費的NTFS links。(NTFS links 網頁有個 Similar Tools,下面還有幾個)
WinLink
Description
A software that support creating (directory) symlink, hardlink, and NTFS junctions.
Very simple, all you need is administrative rights.
I am sure everyone knows how to use it. 2.x is developed with Delphi (faster than VB.NET).
HardlinkGUI
Description
If you don't know how command line works or too lazy to open command prompt and want to create a hard link, use this tool.
This only work on Windows 2000 and later with NTFS file system
[Vista] 符號連結(Symbolic/Soft Link)、永久連結(Hard Link)與連接點(Junction Point)

Vista強化了類似Unix的符號連結(Symbolic Link,又稱Soft Link)與永久連結(Hard Link)的概念,
由於我參考的網路資源大都語焉不詳,讓人越看越糊塗,因此試着以我的理解呈現出來,若有謬誤還請讀者們指正。
我們由圖形來對照不同連結的觀念。在作業系統里要開啟任何檔案都必須先由檔案配置表
(File Allocation Table,FAT,Vista里不清楚有無別的名詞來表示)里找到檔案項目(Entry),
再由該項目查出相關屬性后,再連結到實體的內容磁區,就能讀取其檔案內容。
如果我們建立一個永久連結,在FAT里新建的連結檔名同樣會連結到內容磁區(紅線)。
- 如果OldFileName被刪除,但因為NewHardLink仍然存在,因此其內容仍會保留在系統里。
- 如果修改OldFileName的內容,則因為NewHardLink也指向同一個內容磁區,因此其內容也同步變動了
- OldFileName與NewHardLink都刪除,內容磁區才會被清除
而符號連結則只是存在於FAT里的一個項目而已,亦即NewSoftLink是一個指向OldFileName項目的指標(藍線),
如果OldFileName被改名或刪除,則NewSoftLink就找不到內容了。
觀念清楚后,再來看Vista里建立連結的指令。
Vista里用來建立連結的指令是mklink.exe(Make Link),使用語法如下:
- :要建立的連結名稱(圖形里的NewHardLink與NewSoftLink)
- :被連結的目標,可以是實體目錄或檔案的完整或部份路徑(圖形里的OldFileName)
范例:

上列指令執行后的結果: 永久連結和檔案符號連結建立了兩個檔案(紅框),目錄符號連結和連接點是兩個目錄,再加上目前目錄的點( . )和代表上層目錄的兩個點( .. )總共四個目錄(淺藍框)。

由檔案總管理無法很清楚的看出各種連結的類型,在DOS視窗使用dir /aL命令才能很清楚的看到、等類型。以下是使用dir /s /aL c: 列出Vista輸出的內容。由其內容可以看出幾個重點:
- 為了和Windows以前版本相容,C:\Documents and Settings是新建的連接點,連接目標是C:\Users
- 連接點大量用來建立指向同一個目錄的多個名稱

Mklink is a MS Windows command line utility that you can use to create symbolic links or symlinks and hard links in MS Windows. Mklink is a nice tool for troubleshooting file system problems. Mklink requires administrative privileges.
This page relates to explaining the difference between symbolic links and hard links on the Difference between symbolic link and hard link page.
First, let's introduce the mklink command. You can see it in the following print screen:

It is pretty self-explanatory. Now, let's take a look at an example.
Mklink example
The print screen shown below demonstrates creating symbolic links, hard links, and shortcuts in MS Windows and how they differ.
Symbolic link:
- Created as mklink link_name target_name
- The icon next to the link is the same like that of a shortcut
- Occupies zero space in the system
- It is not a separate file in the file system
- Interpreted at the operating system level
- If the original file is deleted, the link is useless
- Removing the original file does not remove the symbolic link
- Removing the symbolic link does not remove the original file
Hard link:
- Created as mklink /H link_name target_name
- The space it occupies equals the size of the original file, but refers to the same object in the system (not a copy)
- Interpreted at the operating system level
- The icon next to the hard link is different from the other objects
- Removing the original file does not remove the hard link
- Removing the hard link does not remove the original file
- If the original file is deleted, its content is still available through the hard link
- Making a change to the hard link propagates all changes to the original file
Shortcut:
- Created by right clicking the original file and selecting action from the drop down menu
- A file in the system that is completely separate from the original file
- Only those programs that know the concept of shortcuts understand them
- If the original file is deleted, the link is useless
- Removing the original file does not remove the shortcut
- Removing the shortcut does not remove the original file
The print screen below shows these concepts.

Linux uses a similar command called ln.
Mklink in Windows versus Ln in Linux
Note, the mklink syntax is a bit different from the Linux ln syntax (ln is used in Linux to create links).
Mklink: The name of the link is followed by the name of the target in the mklink command syntax. If you do not specify any flag, the mklink utility creates a symbolic link by default.
Ln: The name of the target is followed by the name of the link. If you want to create a symlink, you have to specifically include the -s flag.
Difference between symbolic link and hard link
What is the difference between symbolic link and hard link? Explaining the difference between a symbolic link or asymlink and a hard link is easy and vital to knowing about how Linux/Unix environments work.
Understanding the difference between a symbolic link and a hard link is also important for web server maintenance and understanding how modern websites work.
What is symbolic link or symlink?
Symbolic link, often called symlink or softlink, is very similar to what we know from Windows - a shortcut. They are kind of shortcuts in the Linux/Unix world. Well, symbolic link can exist in the Windows world too, but for the simplicity of our explanation, let's just work with the comparison that symlink is kind of a shortcut for now. We will get into more details later. Symbolic link contains information about the destination of the target file.
What is hard link?
Hard link (often also called hardlink) is a bit different object when compared to a symlink. Hard link is a directory reference or pointer to a file. Hardlink is a label stored in a directory structure that refers the operating system to the file data when it is accessed. The important part is that hard link is closely tied together with its originating file. If you make changes to a hard link, you automatically make changes to the underlying file that the hardlink is attached to.
Hard link can only refer to data that exists on the same file system.
Many of us are used to Windows where files live in folders. Files in Linux/Unix are not stored in directories. Files in Linux are assigned an inode number which Linux uses to locate files. Each file can have multiple hard links which are located in various directories. A file does not get deleted until there are no remaining hard links to it.
Differences between symbolic link and hard link
Let's summarize our findings. The list bellow summarizes some differences between symlink and hard link:
- Hardlink or hardlinks cannot be created for directories (folders). Hard link can only be created for a file.
- Symbolic links or symlinks can link to a directory (folder).
- Removing the original file that your hard link points to does not remove the hardlink itself; the hardlink still provides the content of the underlying file.
- If you remove the hard link or the symlink itself, the original file will stay intact.
- Removing the original file does not remove the attached symbolic link or symlink, but without the original file, the symlink is useless (the same concept like Windows shortcut).
In case you are interested getting to know even more details, this list is expanded more on the the Mklink page. The concept is simply that hard links are tied to their sources more rigidly.
What is the difference between symlink and shortcut?
We mentioned that symlinks are like shortcuts. They are like shortcuts but with some small differences. Symbolic links are automatically resolved by the file system. Any software programs, upon accessing a symbolic link, will see the target instead, whether the program is aware of symbolic links or not. On the other hand, shortcuts are treated like ordinary files by the files system and by software programs that are not aware of them. Only software programs that understand shortcuts (such as Windows) treat shortcuts as references to other files. Shortcuts can point to files or directories that exist in another file system or on the network. The difference between symbolic link and shortcut is clearer from the example mentioned at the bottom of this article, respectively as shown in the big print screen on theMklink page.
How to create a hard link or hardlink?
In Linux, you would use the ln command to create a hard link.
$ ln fileA fileB
where fileA is the original file and fileB is the name you want to give to the hardlink. Let's do some research now. You have the original file and one hard link that is attached to it. Now, you look at these two objects with the ls command:
$ ls -il fileA fileB
You can see in the output of this command that both files fileA and fileB have the same inode number (the first number on the line). In addition to having the same inode, both files have the same file permissions and the samesize. Because that size is reported for the same inode, we can see that a hard link does not occupy any extra space on your space.
If you now remove the original file and open the hard link, you will still be able to see the content of the original file.
Note, hard link cannot be created to a folder. If you try creating a hard link to a folder, you will get "Access denied."
How to create a symlink?
Let's demonstrate this with an example. When talking about Linux, you would use the ln command with the -sparameter. You would do something like:
$ ln -s fileA fileB
where fileA is the original file and fileB is the name you want to give to the symbolic link. Now, let's take a look at these two objects with the ls command again:
$ ls -il fileA fileB
You can see that you get different result as compared to when we displayed the hard link. The first difference between symlink and the original file is the inode number. The inode is different for the original file and for the symbolic link. Next, you can also notice that there is the pipe symbol "l" before the permissions on the symlink line. Also, the symbolic link has different permissions than the original file (because it is just a symbolic link). The content of the symlink is just a string pointing to the original file. The size of the symlink is not the same as the size of the original file. The symbolic link is a separate entity and as such occupies some space on your hard drive. You can see at the end of the line where the symlink points to.
You can access the content of the original file directly by calling the original file or by calling the symbolic link. You will see the same result.
Now if you remove the original file, the symlink will still be there. If you try to access the content of the original file through the symbolic link after removing the original file, you will get a message saying there is no such file or directory.
Can I make a symlink to a hard link?
Yes. The hard link functions the same way like the original file; therefore, you can make symlinks to it. You would use in our example the following command:
$ ln -s fileB fileC
where fileB would be the name of the hard link, and fileC would be the name of your new symlink.
Difference between symlink and hard link in Windows?
There are not many differences between symbolic link and hard link in the concept; the concept is the same whether we are working with Linux or Windows; the difference between symlink and hardlink is in how you create them. When talking about MS Windows, you can create three things:
- shortcut
- hard link / hardlink
- symbolic link / symlink
See the following example. The example shown on the next page (click the thumbnail) explains how you can create shortcut, hard link, and symbolic link in Windows and how they differ.
This tutorial will tell you how to create and use links using MKLINK to redirect system queries made to a local or network storage location to another location.
In this tutorial we will concentrate on the most important link type, a Directory Junction. A junction is a very practical way to customize your file handling and procedures.
A Link is basically nothing more than a shortcut. Its purpose is to get Windows to think that a file or folder which is physically located in folder Real on drive A is in fact in folder Fake on drive B by creating a link betweenB:\Fake (a link or junction) and A:\Real (a target folder). Opening the link for instance in Explorer shows contents of target, saving a document to link actually saves it on target.
As Windows now gets a query from user to open B:\Fake this query is sent to A:\Real. In reality the location B:\Fake does not exist but system answers this query as it was a real folder instead of just a symbolic link and presents user the contents of A:\Real as if it was contents of B:\Fake. Sounds complicated? Don’t worry, it’s quite easy in fact.
Part 1: What is a Link?
A Link can be so called Hard Link linking one file to a target file, or a Soft Link linking a folder to a target folder. Soft Links are also called Symbolic Links. Third link type is Junction, basically a hard link but as hard links can only link files we need to use junctions to link folders.
If you are using Windows Vista or later you have most probably used links without ever noticing it. In Windows XP all user data was saved in a folder called C:\Documents and Settings (default name and location). Windows Vista changed this; instead of Documents and Settings the data was divided between two system folders, C:\Users for user specific application data and personal files and folders, andC:\ProgramData for all users shared application data. To allow backwards compatibility all Windows versions since Vista have a linkC:\Documents and Settings with two target folders, ProgramData and Users.
You can check this by yourself by allowing protected system files and folders to be shown and opening C: drive on Explorer:
(Please notice, screenshots from Windows 8 but links work the same way on Windows 7. Highlighted with yellow = visible system folders created by Windows installation, red = hidden or protected system folders.)
If you try to open Documents and Settings you get an "Access Denied" message. It does not exist, there is no such folder on your C: drive but it is needed for legacy software.
An example: You want to install Microsoft Office XP on your Windows 7. As Office XP was published for Windows XP long before Vista it uses the XP system folder Documents and Settings to store user data. Vista and later Windows do no longer have this folder so a link is needed. Office XP setup thinks it is creating files and folders normally in Documents and Settings but actually your Windows 7 is lying
to Office XP setup, not telling it those files and folders are in fact created in Users and ProgramData. Office XP setup does not notice this deception, installation works and later when user works with Office XP it still thinks the Documents and Settings folder is there, never learning the fact that all queries to that folder are sent further and returned as if they really came from Documents and Settings.
Part 2: Working with Directory Junctions
(1.) Command Syntax
Links are created wit command mklink, short from Make Link. Typing mklink /? on Command Prompt you get the command syntax and options:
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.
(2.) Use an Elevated Command Prompt
You must use elevated command prompt to work with links.
(3.) Create a Link or a Junction
Creating a directory junction D:\Docs with target E:\Users\Kari\Documents:
mklink /j D:\Docs E:\Users\Kari\Documents
Windows tells you clearly if the creating of junction has succeeded:
(4.) Main principles when creating a link or a junction
- A file or a folder with the name of the intended Link name may not exist. The link file or folder name must be free to use, not reserved by an existing file or folder. If the name is reserved you get an error message. My D: drive already has a folder Test, this is what happens when trying to create a junction with D:\Test (already an existing folder) as link and E:\Users as target:
Notice that command prompt is telling about a file already existing instead of a folder. This is because the system sees links as shortcuts (files) and not as folders.
- Target folder may but must not exist. If it does not exist at the time when a link is created it must be created before you can use the link:
- Target can be another Link. Above in (3.) we created a junction D:\Docs with target E:\Users\Kari\Documents. Following command would now create a link C:\Docs with target D:\Docs:
In fact Windows sees no difference if the target is a real physical folder location or just another link. In this case all queries toC:\Docs would be sent to D:\Docs which would send them further to E:\Users\Kari\Documents. If a user now opens C:\Docsin Explorer, it shows the contents of E:\Users\Kari\Documents:Code:mklink /j C:\Docs D:\Docs
Noticed something interesting in screenshot above? The target folder is called Documents but Explorer shows it as My Documents. This is because those "My ..." folders in your user profile folder are actually not real folders. They are directory links:My Videos is simply a link with target Videos and so on.
(5.) Remove or rename a link
Links and junctions can be renamed and removed as any real folders. System automatically modifies registry and sets the target to be the same for a renamed link as it was on the original link.
This is because even not really existing, the system handles links as if they were real existing folders. This is what Explorer shows when asked for Properties for our example junction D:\Docs:
Only way to really see it is in fact a junction is to get directory listing in command prompt:
The listing not only shows it is a junction but also its target folder.
Part 3: Practical Examples
A typical situation: You have bought a game that must be installed on root level folder C:\Games. Your C: drive is becoming full, so you decide to move the whole C:\Games folder to D:, delete now empty C:\Games (remember, link folder may not exist) and create a junction:
mklink /j C:\Games D:\Games
When launching the game it still assumes it is located on C: although you, me and Windows know better
: it's "secretly" residing on D: but still using the old address on C:.
Another situation: You want to work with some pics from last Christmas, to edit them to be ready to be published on that website of yours. Pics are currently located in X:\Backups\Pictures\My Pictures\Holidays\Christmas 2012.
You can create a junction X:\Pics, and now whenever you want to work with those pics you just type X:\Pics to Run dialog to open the correct folder:
mklink /j X:\Pics "X:\Backups\Pictures\My Pictures\Holidays\Christmas 2012"
Notice that if a path contains spaces it must be set in between quotation marks as in above target path.
That's about it. Any feedback welcome.
Kari


