Win7中的软链接详解(mklink命令)

时间:2022-03-31 05:55:20
[转载声明] 转载时必须标注:本文来源于铁木箱子的博客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