http://blog.nsfocus.net/shortcuthard-linkjunction-pointsymbolic-link/
Windows支持4種”鏈接”機制,分別是shortcut、hard link、junction point、symbolic link。本文針對這4種機制進行簡要介紹,可作為速查手冊存在。
先吐個槽,天朝很多與本問題相關的技術文章,在術語層面相當混亂,作者的一知半解以及不使用規范術語,給本就容易混淆的概念帶來更大的誤導。
Windows支持4種”鏈接”機制,分別是shortcut、hard link、junction point、symbolic link。本文針對這4種機制進行簡要介紹,可作為速查手冊存在。
先吐個槽,天朝很多與本問題相關的技術文章,在術語層面相當混亂,作者的一知半解以及不使用規范術語,給本就容易混淆的概念帶來更大的誤導。
--------------------------------------------------------------------------
1) shortcut/快捷方式
以.lnk文件方式存在,適用於Explorer等應用程序。
非NTFS內置機制,從Win95開始得到支持。FAT32支持。
同時適用於文件、目錄。
只能使用絕對路徑。
可以跨盤符,可以跨主機,可以使用UNC路徑、網絡驅動器。
刪除shortcut,不影響target。
--------------------------------------------------------------------------
2) (file) hard link
假設本文讀者具有*nix基礎,此處不做hard link的語義解釋。
NTFS內置機制,從Windows NT 4開始得到支持。FAT32不支持。
只適用於文件。
只能使用絕對路徑。
hard link與target file必須位於同一volume,可以簡單理解成不能跨盤符。
在Explorer中刪除hard link,不影響target file。
刪除target file,不影響hard link。事實上由於hard link的語義,此時剩下的
hard link就是原始數據的唯一訪問點。
相關Win32 API:
CreateHardLink()
CreateHardLinkTransacted()
創建:
mklink /H "hard link name" "target file"
fsutil.exe hardlink create "hard link name" "target file"
查看:
fsutil.exe hardlink list "hard link name"
fsutil.exe hardlink list "target file"
二者效果一樣,hard link的語義本就如此。
--------------------------------------------------------------------------
3) (directory) junction point/soft link/reparse point
junction point也叫soft link,這是微軟官方文檔里說的:
Hard Links and Junctions
https://msdn.microsoft.com/en-us/library/windows/desktop/aa365006
junction point的底層機制是NTFS的reparse point:
Reparse Points
https://msdn.microsoft.com/en-us/library/windows/desktop/aa365503
Junction v1.06
http://www.sysinternals.com
http://technet.microsoft.com/en-us/sysinternals/default.aspx
https://technet.microsoft.com/en-us/sysinternals/bb896768
How to create and manipulate NTFS junction points
https://support.microsoft.com/en-us/kb/205524
NTFS內置機制,從Windows 2000/XP開始得到支持。
只適用於目錄。Vista的"C:\Documents and Settings\"是指向"C:\Users\"的
junction point,這樣一些使用了硬編碼"C:\Documents and Settings\"的老程序可
以在Vista上正常工作。
只能使用絕對路徑。即使創建junction point時使用了相對路徑,保存到NTFS中時將
隱式轉換成絕對路徑。
junction point必須與target directory位於同一local computer,可以簡單理解成
不能跨主機。不能使用UNC路徑;假設Z是通過網絡映射生成的盤符,同樣不適用於Z。
在local computer范圍內,可以跨盤符。
在Explorer中刪除junction point,有兩種情況。對於Windows 2000/XP/2003,會同
步刪除target directory,這真是一個奇葩的行為。注意,我們強調,在Explorer中
刪除,高版本的Total Commander沒有這個奇葩行為。對於Vista及之后版本,不影響
target directory,這才是人類所能理解的行為。
刪除target directory,junction point仍將存在,但失效了,變得不可用。這個很
好理解,因為此時junction point指向不存在的目錄。
diskmgmt.msc
右鍵選中某volume
更改驅動器號和路徑
添加
裝入以下空白NTFS文件夾中
這個功能用的就是junction point機制,還可以用mountvol.exe完成操作。
創建:
mklink /J "junction point name" "target directory" // 生成的Reparse Data相比junction.exe要多
linkd.exe "junction point name" "target directory" // Windows Resource Kits
查看:
dir /A:L /S "path"
fsutil.exe reparsepoint query "junction point name" // 有Reparse Data的16進制轉儲
linkd.exe "junction point name" // 不能查看junction.exe生成的"junction point"
刪除:
fsutil.exe reparsepoint delete "junction point name" // 不建議使用
linkd.exe "junction point name" /D // 可以刪除junction.exe生成的"junction point"
它這個行為不是我們期望的效果,比如"target directory"下有普通文件,上述命令
會刪除"junction point"下的普通文件,但不會刪除"junction point",同時
"target directory"下的普通文件仍然存在。這個效果我不能理解。在Explorer中操
作無法達到這種效果。
sysinternals的junction.exe:
junction.exe "junction point name" "target directory" // 創建
junction.exe "junction point name" // 查看
junction.exe -q -s "path" // 遞歸查看
junction.exe -d "junction point name" // 刪除
我猜junction.exe提供-d參數,就是因為Windows 2000/XP/2003的Explorer奇葩行為,
這個-d不影響target directory。
示例:
"dir /A:L /S c:\
2009/07/14 13:08 "JUNCTION" Documents and Settings [C:\Users]
"junction.exe "C:\Documents and Settings"
C:\Documents and Settings: JUNCTION
Print Name : C:\Users
Substitute Name: C:\Users
"junction.exe -q -s c:\
\\?\c:\\Documents and Settings: JUNCTION
Print Name : C:\Users
Substitute Name: C:\Users
--------------------------------------------------------------------------
4) symbolic link
Symbolic Links
https://msdn.microsoft.com/en-us/library/windows/desktop/aa365680
symbolic link不是soft link,不要跟着SB瞎起哄。
NTFS內置機制,從Vista開始得到支持。
同時適用於文件、目錄。這是一種超級shortcut。
可以使用相對、絕對路徑。假設創建symbolic link時使用了相對路徑,保存到NTFS
中的就是相對路徑,不會隱式轉換成絕對路徑。
可以跨盤符,可以跨主機,可以使用UNC路徑、網絡驅動器。
在Explorer中刪除symbolic link,不影響target。
刪除target,symbolic link仍將存在,但失效了,變得不可用。
相關Win32 API:
CreateSymbolicLink()
CreateSymbolicLinkTransacted()
創建:
mklink "file symbolic link name" "target file"
mklink /D "directory symbolic link name" "target directory"
注意不指定/D時創建file symbolic link,指定/D創建directory symbolic link。
--------------------------------------------------------------------------