天天看點

Win7中的軟連結詳解(mklink指令)

[轉載聲明] 轉載時必須标注:本文來源于鐵木箱子的部落格http://www.mzone.cc

[本文位址] 本文永久位址是: http://www.mzone.cc/article/619.html

      用過Linux的朋友都知道linux中有軟連結的概念,可以通過ln指令建立到目錄或檔案的軟連結,軟連結的好處就是可以讓一個目錄或檔案有多個入口但保持單一實體位置,友善應用和管理。在Linux下建立軟連結一般用如下指令:

<ol style="padding: 0px; margin: 0px;"><li style="list-style-type: none; vertical-align: top;"><div style="padding: 0px; margin: 0px; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;"># ln -sf file1 file2</div></li><li style="list-style-type: none; vertical-align: top;"><div style="padding: 0px; margin: 0px; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;"> </div></li><li style="list-style-type: none; vertical-align: top;"><div style="padding: 0px; margin: 0px; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;">其中file1是軟體連結的名稱,file2是實際檔案的路徑,以後通過file1就可以通路file2了</div></li></ol>      

      之前一直苦于windows下沒有類似的功能,導緻有些地方很不友善,不過進入windows vista和win7時代後,這樣的功能也被附帶在windows中了,通過win7作業系統中的mklink指令就可以建立類似的軟連結了。

<ol style="padding: 0px; margin: 0px;"><li style="list-style-type: none; vertical-align: top;"><div style="padding: 0px; margin: 0px; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;">D:\temp<span style="color: rgb(0, 0, 0); "><strong>></strong></span>mklink <span style="color: rgb(0, 0, 0); "><strong>/</strong></span>?</div></li><li style="list-style-type: none; vertical-align: top;"><div style="padding: 0px; margin: 0px; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;">建立符号連結。</div></li><li style="list-style-type: none; vertical-align: top;"><div style="padding: 0px; margin: 0px; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;"> </div></li><li style="list-style-type: none; vertical-align: top;"><div style="padding: 0px; margin: 0px; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;">MKLINK <span style="font-weight: bold;">[</span><span style="font-weight: bold;">[</span><span style="color: rgb(0, 0, 0); "><strong>/</strong></span>D<span style="font-weight: bold;">]</span> <span style="color: rgb(0, 0, 0); "><strong>|</strong></span> <span style="font-weight: bold;">[</span><span style="color: rgb(0, 0, 0); "><strong>/</strong></span>H<span style="font-weight: bold;">]</span> <span style="color: rgb(0, 0, 0); "><strong>|</strong></span> <span style="font-weight: bold;">[</span><span style="color: rgb(0, 0, 0); "><strong>/</strong></span>J<span style="font-weight: bold;">]</span><span style="font-weight: bold;">]</span> Link Target</div></li><li style="list-style-type: none; vertical-align: top;"><div style="padding: 0px; margin: 0px; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;"> </div></li><li style="list-style-type: none; vertical-align: top;"><div style="padding: 0px; margin: 0px; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;">        <span style="color: rgb(0, 0, 0); "><strong>/</strong></span>D      建立目錄符号連結。預設為檔案</div></li><li style="list-style-type: none; vertical-align: top;"><div style="padding: 0px; margin: 0px; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;">                符号連結。</div></li><li style="list-style-type: none; vertical-align: top; font-weight: bold;"><div style="padding: 0px; margin: 0px; font-weight: normal; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;">        <span style="color: rgb(0, 0, 0); "><strong>/</strong></span>H      建立硬連結,而不是符号連結。</div></li><li style="list-style-type: none; vertical-align: top;"><div style="padding: 0px; margin: 0px; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;">        <span style="color: rgb(0, 0, 0); "><strong>/</strong></span>J      建立目錄聯接。</div></li><li style="list-style-type: none; vertical-align: top;"><div style="padding: 0px; margin: 0px; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;">        Link    指定新的符号連結名稱。</div></li><li style="list-style-type: none; vertical-align: top;"><div style="padding: 0px; margin: 0px; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;">        Target  指定新連結引用的路徑</div></li><li style="list-style-type: none; vertical-align: top;"><div style="padding: 0px; margin: 0px; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;">                <span style="font-weight: bold;">(</span>相對或絕對<span style="font-weight: bold;">)</span>。</div></li></ol>      

      上面的是mklink指令的幫助資訊,可以看到mklink可以建立目錄和檔案連結。比如D:\temp目錄下有目錄dir1,我想在目前目錄下建立到dir1目錄的軟連結,可以使用如下指令來操作:

<ol style="padding: 0px; margin: 0px;"><li style="list-style-type: none; vertical-align: top;"><div style="padding: 0px; margin: 0px; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;">mklink <span style="color: rgb(0, 0, 0); "><strong>/</strong></span>D dir2 dir1</div></li><li style="list-style-type: none; vertical-align: top;"><div style="padding: 0px; margin: 0px; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;">mklink <span style="color: rgb(0, 0, 0); "><strong>/</strong></span>J dir3 dir1</div></li></ol>      

      上面兩個指令都可以實作到dir1的軟連結,參數/D和/J是建立目錄軟連結必須的,目前來說着兩個參數都可以實作目錄軟連結,而且應用過程中也沒有發現有什麼差別的地方,唯一的差別就是dir2和dir3連結的類型不懂,可以通過dir指令檢視得到結果如下:

<ol style="padding: 0px; margin: 0px;"><li style="list-style-type: none; vertical-align: top;"><div style="padding: 0px; margin: 0px; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;"><span style="color: rgb(0, 0, 0);">2011</span><span style="color: rgb(0, 0, 0); "><strong>/</strong></span>05<span style="color: rgb(0, 0, 0); "><strong>/</strong></span><span style="color: rgb(0, 0, 0);">17</span>  <span style="color: rgb(0, 0, 0);">14</span>:05    <span style="color: rgb(0, 0, 0); "><strong><</strong></span>DIR<span style="color: rgb(0, 0, 0); "><strong>></strong></span>          dir1</div></li><li style="list-style-type: none; vertical-align: top;"><div style="padding: 0px; margin: 0px; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;"><span style="color: rgb(0, 0, 0);">2011</span><span style="color: rgb(0, 0, 0); "><strong>/</strong></span>05<span style="color: rgb(0, 0, 0); "><strong>/</strong></span><span style="color: rgb(0, 0, 0);">24</span>  <span style="color: rgb(0, 0, 0);">13</span>:07    <span style="color: rgb(0, 0, 0); "><strong><</strong></span>JUNCTION<span style="color: rgb(0, 0, 0); "><strong>></strong></span>     dir2 <span style="font-weight: bold;">[</span>D:\temp\dir1<span style="font-weight: bold;">]</span></div></li><li style="list-style-type: none; vertical-align: top;"><div style="padding: 0px; margin: 0px; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;"><span style="color: rgb(0, 0, 0);">2011</span><span style="color: rgb(0, 0, 0); "><strong>/</strong></span>05<span style="color: rgb(0, 0, 0); "><strong>/</strong></span><span style="color: rgb(0, 0, 0);">24</span>  <span style="color: rgb(0, 0, 0);">13</span>:07    <span style="color: rgb(0, 0, 0); "><strong><</strong></span>SYMLINKD<span style="color: rgb(0, 0, 0); "><strong>></strong></span>     dir3 <span style="font-weight: bold;">[</span>dir1<span style="font-weight: bold;">]</span></div></li></ol>      

      我們可以發現dir1是DIR類型(這個是我們建立的實際目錄),dir2是JUNCTION類型,而dir3是SYMLINKD類型,這個或許是差別,但應用中效果一樣。

      說完了目錄軟連結,檔案當然也可以建立軟連結了,而且還可以建立硬連接配接,比如d:\temp目錄下有abc.txt檔案,我要建立到這個檔案的軟連結的指令如下:

<ol style="padding: 0px; margin: 0px;"><li style="list-style-type: none; vertical-align: top;"><div style="padding: 0px; margin: 0px; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;">mklink abc1.txt abc.txt</div></li></ol>      

      這樣我們就建立了一個到abc.txt檔案的軟連結abc1.txt,對于檔案還可以建立硬連結,硬連結個人感覺用處不大,因為建立硬連結是磁盤上空間是增加了雙倍,對于硬連結而言,修改硬連結檔案和原始檔案的内容都會互相影響,即内容是一緻的但空間占用确是雙份的。使用如下指令建立硬連結:

<ol style="padding: 0px; margin: 0px;"><li style="list-style-type: none; vertical-align: top;"><div style="padding: 0px; margin: 0px; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;">mklink <span style="color: rgb(0, 0, 0); "><strong>/</strong></span>H abc2.txt abc.txt</div></li></ol>      

      檔案的軟連結和硬連結的類型檢視如下(用dir指令):

<ol style="padding: 0px; margin: 0px;"><li style="list-style-type: none; vertical-align: top;"><div style="padding: 0px; margin: 0px; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;"><span style="color: rgb(0, 0, 0);">2011</span><span style="color: rgb(0, 0, 0); "><strong>/</strong></span>05<span style="color: rgb(0, 0, 0); "><strong>/</strong></span><span style="color: rgb(0, 0, 0);">24</span>  <span style="color: rgb(0, 0, 0);">13</span>:<span style="color: rgb(0, 0, 0);">33</span>                 <span style="color: rgb(0, 0, 0);">7</span> abc.txt</div></li><li style="list-style-type: none; vertical-align: top;"><div style="padding: 0px; margin: 0px; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;"><span style="color: rgb(0, 0, 0);">2011</span><span style="color: rgb(0, 0, 0); "><strong>/</strong></span>05<span style="color: rgb(0, 0, 0); "><strong>/</strong></span><span style="color: rgb(0, 0, 0);">24</span>  <span style="color: rgb(0, 0, 0);">13</span>:<span style="color: rgb(0, 0, 0);">30</span>    <span style="color: rgb(0, 0, 0); "><strong><</strong></span>SYMLINK<span style="color: rgb(0, 0, 0); "><strong>></strong></span>      abc1.txt <span style="font-weight: bold;">[</span>abc.txt<span style="font-weight: bold;">]</span></div></li><li style="list-style-type: none; vertical-align: top;"><div style="padding: 0px; margin: 0px; font-stretch: normal; font-size: 1em; line-height: 1.2em; vertical-align: top;"><span style="color: rgb(0, 0, 0);">2011</span><span style="color: rgb(0, 0, 0); "><strong>/</strong></span>05<span style="color: rgb(0, 0, 0); "><strong>/</strong></span><span style="color: rgb(0, 0, 0);">24</span>  <span style="color: rgb(0, 0, 0);">13</span>:<span style="color: rgb(0, 0, 0);">33</span>                 <span style="color: rgb(0, 0, 0);">7</span> abc2.txt</div></li></ol>      

      發現硬連結和普通的檔案沒有什麼差別,唯一的差別就是修改的内容會直接影響到原始檔案。

      基本上win7系統下軟連結建立和基本知識就介紹到這裡,對這些有什麼用處,舉個例子,我們常見的在windows下開發PHP程式,而php程式又需要在apache下運作,一般情況下我們需要修改apache的htdocs目錄或者把代碼放到htdocs目錄下才可以運作。但是用了軟連結後,我們可以在apache的htdcos目錄下建立一個到目錄軟連結到實際開發目錄,這樣我們代碼開發就可以直接在apache中進行預覽更新了,當然你也可以用這個技術在其他任何想要的地方。

      順帶補充一句,軟連結的目錄和檔案都可以直接删除,不會影響到原始目錄和檔案的,這個也非常友善,而且軟連結的目錄和檔案圖示上都有一個小箭頭,很好區分。

  mklink指令相關文章: http://www.cnblogs.com/asion/archive/2011/03/10/1979282.html