mklink 在 Winodws 7 建立 symbolic link

  • 59982
  • 0

mklink 在 Winodws 7 建立 symbolic link

mklink:Creates a symbolic link.

適用平台:Windows Server 2008, Windows Vista、Windows 7 等以上版本

 

Syntax:

mklink [[/d] | [/h] | [/j]] <Link> <Target>

 

Parameters:

Parameter

Description

/d Creates a directory symbolic link. By default, mklink creates a file symbolic link.
/h Creates a hard link instead of a symbolic link.
/j Creates a Directory Junction.
<Link> Specifies the name of the symbolic link that is being created.
<Target> Specifies the path (relative or absolute) that the new symbolic link refers to.
/? Displays help at the command prompt.

 

Examples:

mklink /d C:\Users\eric_c_huang\Desktop\AAA C:\Document\ABC

在桌面上建立名為 AAA 的 symbolic link ,連結至 C:\Document  的 ABC 目錄…

 

建立 symbolic link 需要管理者權限,否則會出現「you do not have sufficient privilege to perform this operation」的錯誤訊息

image

 

如果擁有足夠的權限,畫面如下:

image

 

一般的 shortcut 與 symbolic link 的差別,參考下圖:

外觀上圖示並沒有太大的差別,只能從 Type 來分別:一個是 Shortcut 為 .lnk 副檔名的檔案,而另一個是 File folder。

實務上,使用 symbolic link 可以直接當成目錄來使用,例如 C:\Users\eric_c_huang\Desktop\AAA\abc.txt 就相當於直接存取 c:\temp\ABC\abc.txt

而使用 shortcut 只能打開 c:\temp\ABC 目錄…

image

 

使用 /d 的 symbolic link 是所謂的 soft link,不管是在 link 目錄中存取檔案 (建立、修改、刪除),都會作用在 target 目錄上

 

如果作用在 file 的 soft link 不需要加上 /d 的參數:

當你修改 Link 的 file 內容,Target 的 file 內容會跟著修改;反之, 當你修改 Target 的 file 內容,Link 的 file 內容會跟著修改。

當刪除 Link 的檔案,並不會影響 Target 的檔案,因為只是一個 link。

如果刪除 Target 的檔案,會影響 Link 的檔案,因為 link 找不到實體檔案會發生錯誤。

 

使用 /h 的 symbolic link 是所謂的 hard link,僅能作用 file 上:

當你修改 Link 的 file 內容,Target 的 file 內容會跟著修改;反之, 當你修改 Target 的 file 內容,Link 的 file 內容會跟著修改。

但是,當刪除任何一邊的檔案,並不會影響另一邊的檔案。因為,不管 Link 或是 Target 兩邊都是 file。

image