[Vista] 符號連結(Symbolic/Soft Link)、永久連結(Hard Link)與連接點(Junction Point)

mklink
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),使用語法如下:

  mklink [[/d] | [/h] | [/j]]
參數 說明 類型 限制
如果沒有加任何參數時,預設建立的是檔案的符號連結 捷徑
/d 建立目錄的符號連結 捷徑
/j 建錄目錄的連接點(Junction point)。 捷徑圖示,但由「內容」看並無捷徑頁籤
/h 建立檔案的永久連結。若操作對象是目錄的話會出現【存取被拒】的錯誤訊息 一般檔案 只能對本機的NTFS磁碟操作
  • :要建立的連結名稱 (圖形裡的NewHardLink與NewSoftLink)
  • :被連結的目標,可以是實體目錄或檔案的完整或部份路徑 (圖形裡的OldFileName)

範例:

  rem 檔案符號連結
  mklink c:\ln\soft-file.txt e:\USB\Project1\source\test.txt
  rem 目錄符號連結
  mklink /D c:\ln\soft-dir c:\Users\jerry\Documents
  rem 目錄永久連結
  mklink /H c:\ln\hard-file.txt c:\users\jerry\Documents\test.txt
  rem Junction Point
  mklink /J c:\ln\jp-dir c:\Users\jerry\Recent

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

由檔案總管理無法很清楚的看出各種連結的類型,在DOS視窗使用dir /aL 命令才能很清楚的看到等類型。以下是使用 dir /s /aL c: 列出Vista輸出的內容。由其內容可以看出幾個重點:

  • 為了和Windows以前版本相容,C:\Documents and Settings是新建的連接點,連接目標是C:\Users
  • 連接點大量用來建立指向同一個目錄的多個名稱
 磁碟區 C 中的磁碟是 20080322
 磁碟區序號:  8C99-6FAB

 C: 的目錄

2006/11/02  21:02         Documents and Settings [C:\Users]
               0 個檔案               0 位元組

 C:\ProgramData 的目錄

2006/11/02  21:02         Application Data [C:\ProgramData]
2006/11/02  21:02         Desktop [C:\Users\Public\Desktop]
2006/11/02  21:02         Documents [C:\Users\Public\Documents]
2006/11/02  21:02         Favorites [C:\Users\Public\Favorites]
2006/11/02  21:02         Start Menu [C:\ProgramData\Microsoft\WindowsStart Menu]
2006/11/02  21:02         Templates [C:\ProgramData\Microsoft\Windows\Templates]
               0 個檔案               0 位元組

 C:\Users 的目錄

2006/11/02  21:02         All Users [C:\ProgramData]
2006/11/02  21:02         Default User [C:\Users\Default]
               0 個檔案               0 位元組

 C:\Users\All Users 的目錄

2006/11/02  21:02         Application Data [C:\ProgramData]
2006/11/02  21:02         Desktop [C:\Users\Public\Desktop]
2006/11/02  21:02         Documents [C:\Users\Public\Documents]
2006/11/02  21:02         Favorites [C:\Users\Public\Favorites]
2006/11/02  21:02         Start Menu [C:\ProgramData\Microsoft\Windows\Start Menu]
2006/11/02  21:02         Templates [C:\ProgramData\Microsoft\Windows\Templates]
               0 個檔案               0 位元組

 C:\Users\Default 的目錄

2006/11/02  21:02         Application Data [C:\Users\Default\AppData\Roaming]
2006/11/02  21:02         Cookies [C:\Users\Default\AppData\Roaming\Microsoft\Windows\Cookies]
2006/11/02  21:02         Local Settings [C:\Users\Default\AppData\Local]
2006/11/02  21:02         My Documents [C:\Users\Default\Documents]
2006/11/02  21:02         NetHood [C:\Users\Default\AppData\Roaming\Microsoft\Windows\Network Shortcuts]
2006/11/02  21:02         PrintHood [C:\Users\Default\AppData\Roaming\Microsoft\Windows\Printer Shortcuts]
2006/11/02  21:02         Recent [C:\Users\DefaultAppData\Roaming\Microsoft\Windows\Recent]
2006/11/02  21:02         SendTo [C:\Users\DefaultAppData\Roaming\Microsoft\Windows\SendTo]
2006/11/02  21:02         Start Menu [C:\Users\DefaultAppData\Roaming\Microsoft\WindowsS\tart Menu]
2006/11/02  21:02         Templates [C:\Users\Default\AppData\Roaming\Microsoft\Windows\Templates]
               0 個檔案               0 位元組

 C:\Users\Default\AppData\Local 的目錄

2006/11/02  21:02         Application Data [C:\Users\Defaul\tAppData\Local]
2006/11/02  21:02         History [C:\Users\DefaultAppData\Local\Microsoft\Windows\History]
2006/11/02 
21:02         Temporary Internet Files
[C:\Users\Default\AppData\Local\Microsoft\Windows\Temporary Internet
Files]
               0 個檔案               0 位元組

 C:UsersDefaultDocuments 的目錄

2006/11/02  21:02         My Music [C:\Users\Default\Music]
2006/11/02  21:02         My Pictures [C:\Users\Default\Pictures]
2006/11/02  21:02         My Videos [C:\Users\Default\Videos]
               0 個檔案               0 位元組

 C:\Users\jerry 的目錄

2008/03/22  12:15         Application Data [C:\Users\jerry\AppData\Roaming]
2008/03/22  12:15         Cookies [C:\Users\jerry\AppData\Roaming\Microsoft\Windows\Cookies]
2008/03/22  12:15         Local Settings [C:\Users\jerry\AppData\Local]
2008/03/22  12:15         My Documents [C:\Users\jerry\Documents]
2008/03/22  12:15         NetHood [C:\Users\jerry\AppData\Roaming\Microsoft\Windows\Network Shortcuts]
2008/03/22  12:15         PrintHood [C:\Users\jerry\AppData\Roaming\Microsoft\Windows\Printer Shortcuts]
2008/03/22  12:15         Recent [C:\Users\jerry\AppData\Roaming\Microsoft\Windows\Recent]
2008/03/22  12:15         SendTo [C:\Users\jerry\AppData\Roaming\Microsoft\Windows\SendTo]
2008/03/22  12:15         Templates [C:\Users\jerry\AppData\Roaming\Microsoft\Windows\Templates]
2008/03/22  12:15         「開始」功能表 [C:\Users\jerry\AppData\Roaming\Microsoft\Windows\Start Menu]
               0 個檔案               0 位元組

 C:\Users\jerry\AppData\Local 的目錄

2008/03/22  12:15         Application Data [C:\Users\jerry\AppData\Local]
2008/03/22  12:15         History [C:\Users\jerry\AppData\Local\Microsoft\Windows\History]
2008/03/22 
12:15         Temporary Internet Files [C:\Users\jerry\AppData\Local\Microsoft\Windows\Temporary Internet Files]
               0 個檔案               0 位元組

 C:\Users\jerry\AppData\Roaming\Microsoft\Windows\Start Menu 的目錄

2008/03/22  12:15         程式集 [C:\UsersjerryAppDataRoaming\Microsoft\Windows\Start Menu\Programs]
               0 個檔案               0 位元組

 C:\Users\jerry\Documents 的目錄

2008/03/22  12:15         My Music [C:UsersjerryMusic]
2008/03/22  12:15         My Pictures [C:UsersjerryPictures]
2008/03/22  12:15         My Videos [C:UsersjerryVideos]
               0 個檔案               0 位元組

 C:\Users\Public\Documents 的目錄

2006/11/02  21:02         My Music [C:\Users\Public\Music]
2006/11/02  21:02         My Pictures [C:\Users\Public\Pictures]
2006/11/02  21:02         My Videos [C:\Users\Public\Videos]
               0 個檔案               0 位元組

     檔案數目總計:
               0 個檔案               0 位元組
              51 個目錄  102,296,236,032 位元組可用

參考:

  • In Unix, what is a symbolic link, and how do I create one?
  • In Unix, what is a hard link?
  • NTFS junction point
  • Vista's symbolic links feature saves navigating time
  • Windows的12种快捷方式(2007-4-3修訂版)

##

您可能也會有興趣的類似文章

簡睿

服務於軟體業的資訊老兵。興趣廣泛,學習力佳,樂於分享所知所學。

您可能也會喜歡…

2 個回應

  1. Eric表示:

    Really helpful, thx!

  2. linjy表示:

    讚~深度與實用兼具的好文章!Vista的各種秘技到處都有,都是我早已知道的內容。只有版主如此用心,發掘這種少有人知,卻非常實用的秘技!

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *