Windows硬連接 軟連接 符號連接 快捷方式

http://blog.nsfocus.net/shortcuthard-linkjunction-pointsymbolic-link/windows

Windows支持4種」連接」機制,分別是shortcut、hard link、junction point、symbolic link。本文針對這4種機制進行簡要介紹,可做爲速查手冊存在。sublime-text

 

先吐個槽,天朝不少與本問題相關的技術文章,在術語層面至關混亂,做者的只知其一;不知其二以及不使用規範術語,給本就容易混淆的概念帶來更大的誤導。網絡

 

Windows支持4種」連接」機制,分別是shortcut、hard link、junction point、symbolic link。本文針對這4種機制進行簡要介紹,可做爲速查手冊存在。編碼

 

先吐個槽,天朝不少與本問題相關的技術文章,在術語層面至關混亂,做者的只知其一;不知其二以及不使用規範術語,給本就容易混淆的概念帶來更大的誤導。spa

 

--------------------------------------------------------------------------
1) shortcut/快捷方式.net

以.lnk文件方式存在,適用於Explorer等應用程序。blog

非NTFS內置機制,從Win95開始獲得支持。FAT32支持。遞歸

同時適用於文件、目錄。seo

只能使用絕對路徑。ip

能夠跨盤符,能夠跨主機,可使用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。
--------------------------------------------------------------------------

相關文章
相關標籤/搜索