WINDOWS 的 MKLINK : 硬链接,符号链接 : 文件符号链接, 目录符号链接 : 目录联接...

玩转WIN7的MKLINK

引言:

换了新电脑,终于再次使用上啦WIN7 ,经过一个周每天重装N次系统,... ... ... ...

 

在xp系统下,junction命令要用微软开发的小程序 junction.exe实现,从http://live.sysinternals.com/可以下载。

现在Windows Vista之后的系统自带了mklink的功能。

Windows操作系统启动时不支持符号链接。所以不应该对下列文件夹重定向:

  • 目录包含了hiberfil.sys (如果被配置在根目录以外)
  • \Windows
  • \Windows\system32
  • \Windows\system32\Config

Windows Installer不完整支持符号链接。对\Windows\Installer重定向会导致多数基于.msi的Windows installers失败报告error 2755 或 error 1632.

可以重定向下列目录:

  • \Documents and Settings
  • \ProgramData
  • \Program Files
  • \Program Files (x86)
  • \Users

但是对\Users与\ProgramData重定向不被推荐,因为会打破Windows Update与Windows Store Apps.

Windows Vista中预先创建了一些符号链接(Junction point),以增进与旧版操作系统(Windows XP)间的兼容性,部分示例如下:

  • C:\Document and Settings → C:\Users(连接点)
  • C:\Users\All Users → C:\ProgramData(目录符号链接)
MKLINK [[/D] | [/H] | [/J]] Link Target

        /D      创建目录符号链接。默认为文件符号链接。
/H 创建硬链接,而不是符号链接。 /J 创建目录联接。 Link 指定新的符号链接名称。 Target 指定新链接引用的路径(相对或绝对)。

默认情况:

  • 文件的符号链接(SYMLINKD),如没有参数指定,则创建文件的符号链接,删除文件链接不影响目标文件,且创建链接时允许目标文件不存在;
  • 目录的符号链接(SYMLINKD) /D该参数可以创建目录的符号链接,删除目录链接不会影响目标目录,且创建链接时允许目标目录不存在;
  • 目录的软链接(JUNCTION) /J 该参数可以创建目录的软链接(联接),作用基本和符号链接类似,NT6系统的用户目录就是以这种形式存在的;
  • 文件的硬链接 /H 该参数可以创建文件的硬链接,即一个文件的多个别名,NT6系统WinSXS目录下的大部分文件是以这个形式存在的;

NTFS符号链接(Symbolic Link),又称符号链接,是NTFS文件系统中指向文件系统中的另一个对象的一类对象。

被指向的对象叫做“目标”。它们可以像普通文件一样操作,但所有对符号链接的操作都实际作用于目标对象。

符号链接对用户而言是透明的,符号链接看上去和普通的文件和文件夹没有区别,操作方法也一模一样。

NTFS符号链接的设计目的是为了和POSIX标准的操作系统兼容、并用来增进与旧版操作系统的兼容性,它首次出现于Windows Vista。

 

Windows 2000开始就有的NTFS连接点,只能用于目录,且只能用于本地文件系统。

而符号链接既可以指向本地文件,也可以指向远程SMB网络路径。

NTFS连接点只支持本地驱动器的绝对路径但NTFS符号链接允许使用相对路径。

还有,NTFS符号链接允许跨文件系统使用,但是对于跨主机的符号链接,还需要远程机器也支持这一功能才行。

硬链接只能用于文件,不能用于目录。

硬链接相当于一个实体文件有多个彼此平等的名字。

删除一个文件名字后,还可以用其它名字访问该文件。

硬链接只能用于同一个磁盘分区内的文件。

硬链接不能用于空文件,而符号链接可以用于空文件或空目录。

 

符号链接(Symlink,Softlink)是对文件或目录的引用,实际上符号链接本身是一个

“记录着所引用文件或目录的绝对或相对路径”的特殊文件,通过符号链接的操作都会被重定向到目标文件或目录。

交接点(Junction),也称为“再分析点”,是 NTFS 3.0 及以上文件系统(Windows 2000 及以上系统)的特性,

它是链接本地目录(可跨卷)的访问点,通过交接点的操作都会被系统映射到实际的目录上。

通过建立交接点,可以在保证一个目录实例(目录的一致性)的前提下,允许用户或程序从本地文件系统中的多个位置访问此目录。

对符号链接和快捷方式的“读、写、遍历”等操作都会被重定向到目标文件或目录;
但对它们的“复制、删除、移动、配置 ACL”等操作只针对自身;
符号链接不但可以应用于本地系统,还可以应用 UNC 路径。

对交接点内文件和子目录的“建立、删除、修改”等操作都被映射到对应的目录中的文件和子目录上;
对交接点的“复制、粘贴、剪切、配置 ACL”,只会影响此交接点;
在同一卷内移动交接点,只会影响此交接点;但在不同卷间移动交接点,会将此交接点转换为正常目录,并且交接点对应目录下的所有内容都会被移动;
通过“资源浏览器(Explorer.exe)”和“命令行 del”等工具删除交接点时,会同时删除对应目录下的所有内容(但不会删除目录);
可通过“linkd.exe /d”安全的删除交接点;但在 vista 及以后的系统中,对交接点的删除会被正确的处理。

符号链接 symlink 与 交接点 junction 在重定向对目录下的子目录和文件的操作上基本没有什么区别。
它们两最大的区别在于 符号链接 symlink 可以用于引用文件,而 交接点 junction 只能引用目录。

创建符号链接和软链接时允许目标文件或目录不存在,此时若访问链接文件会得到一个“位置不可用”的提示,
若之后重新将目标文件或目录补上(路径、名称一致即可),则该链接仍然有效; 

目录符号链接和软链接的区别在于,软链接在创建时会自动引用目标目录的绝对路径,而符号链接允许相对路径的引用,

如分别用 mklink /D dira tdir 和 mklink /J dirb tdir 创建 dira、dirb 对 tdir 的符号链接和软链接,
之后将 dira、dirb 移动到其它目录下,则访问 dira 时会提示“位置不可用”,访问 dirb 时仍然正常指向 tdir; 

而分别用 mklink /D dira c:\demo\tdir 和 mklink /J dirb c:\demo\tdir 创建 c:\demo\tdir 的符号链接和软链接, 
再将这两个目录链接移动到其它目录下,则 dira 和 dirb 均可正常指向 c:\demo\tdir;

由此可见当创建目录链接时对目标目录使用绝对路径,D 和 J 两个参数实现的目录链接效果是一样的

只能创建文件的硬链接,使用/H 参数创建目录的硬链接时会提示拒绝访问;

硬链接是同一个文件的不同别名,例如对 

a.txt 创建了b.txt 和c.txt 两个硬链接,此时 a.txt、b.txt 和 c.txt 均是同一个文件的别名,
若删除其中任意一个或两个,则剩下的那个文件(无论是事先存在还是之后创建)不受影响,仍然指向原先那个文件。

创建文件或目录链接仅限于 NTFS 文件系统,且链接只能针对同一个磁盘上的文件或目录;

硬链接只能创建对本分区文件的引用,即不可创建跨分区的文件硬链接;

通过硬链接创建的别名文件在统计大小时会按原文件的大小计算,而软链接和符号链接大小均为零; 

硬链接在创建时会事先检测目标文件是否存在,即不允许空链接的存在。

 

通过mklink命令的帮助信息,可以看到mklink可以创建目录和文件链接。

比如D:\temp目录下有目录dir1,我想在当前目录下创建到dir1目录的软链接,可以使用如下命令来操作:

mklink /D dir2 dir1
mklink /J dir3 dir1

上面两个命令都可以实现到dir1的软链接,参数/D和/J是创建目录软链接必须的,目前来说着两个参数都可以实现目录软链接,
而且应用过程中也没有发现有什么区别的地方,唯一的区别就是dir2和dir3链接的类型不懂,可以通过dir命令查看得到结果如下:

2011/05/17  14:05              dir1
2011/05/24  13:07         dir2 [D:\temp\dir1]
2011/05/24  13:07         dir3 [dir1]

 

我们可以发现dir1是DIR类型(这个是我们创建的实际目录),dir2是JUNCTION类型,而dir3是SYMLINKD类型,这个或许是区别,但应用中效果一样。

说完了目录软链接,文件当然也可以创建软链接了,而且还可以创建硬连接,比如d:\temp目录下有abc.txt文件,我要创建到这个文件的软链接的命令如下:

mklink abc1.txt abc.txt

 

这样我们就创建了一个到abc.txt文件的软链接abc1.txt,对于文件还可以创建硬链接,硬链接个人感觉用处不大,因为创建硬链接是磁盘上空间是增加了双倍,
对于硬链接而言,修改硬链接文件和原始文件的内容都会相互影响,即内容是一致的但空间占用确是双份的。使用如下命令创建硬链接:

mklink /H abc2.txt abc.txt

  文件的软链接和硬链接的类型查看如下(用dir命令):

2011/05/24  13:33                 7 abc.txt
2011/05/24  13:30          abc1.txt [abc.txt]
2011/05/24  13:33                 7 abc2.txt

 

 发现硬链接和普通的文件没有什么区别,唯一的区别就是修改的内容会直接影响到原始文件。

 顺带补充一句,软链接的目录和文件都可以直接删除,不会影响到原始目录和文件的,这个也非常方便,

而且软链接的目录和文件图标上都有一个小箭头,很好区分。

使用/D建立的连接是类似于linux下的软连接,不占存储空间;

使用/J建立的连接类似于Linux下的硬连接,占用同源文件一样的存储空间;

Link和Target文件夹内容一模一样,如果你对其中任意一个文件夹里面的内容做修改,另一个也会相应的改变。

 

通过建立符号链接(symbolic)磁盘空间使用量没有改变哦,因为只是实现连接罢了,并不是将原文件复制以及同步。

使用连接目录时就像使用原目录一样,就像实实在在有这个目录一样,程序不会发现连接目录与普通目录有何不同,这是一般的文件夹快捷方式是不行的。

与快捷方式的相同与不同之处:

  1. 快捷方式无论目标是文件还是目录,生成的都是新的有内容的 .lnk 文件,
    打开时是打开的源路径,直接编辑则是编辑 .lnk 文件的内容,源文件/目录不受影响。

    MKLINK 生成的文件和目录都是 0 字节,但访问时仍然是新文件/目录的路径,且编辑操作编辑的是源文件/目录;
    原理相当于网盘的同步,只不过不通过网络、不占用空间。

  2. 删除两者生成的目标文件/目录都不会对源文件/目录产生影响。但删除源文件/目录后,新路径都不再可用。


Junction Link Magic (340 kB - Windows 2000/XP/2003/Vista/2008/Windows 7) is a utility

that lets you create junction points with Windows 2000, XP, 2003, Vista, Windows Server 2008 and Windows 7.

Junction Link Magic automatically lists existing junction points, and it offers an easy interface to add, modify or remove junction points.

Junction Link Magic is freeware.

Download Junction Link Magic Location 1 
Download Junction Link Magic Location 2 
Download older version of Junction Link Magic for Windows 2000 and XP 

A junction point is a special type of reparse point. 
Reparse points are redirections in the Windows file system.
There are 3 types of reparse points:

  1. Symbolic links - can be thought of as a shortcut to a file or folder elsewhere in the file system
  2. Junction points - can only point to a folder
  3. Mount points - is a folder on a disk that points to an entire disk volume

Junction Link Magic will list all 3 types, not only junction links. With Junction Link Magic, you can also create and remove junction links.

A junction point is a technology for a folder to be grafted into another folder on the same local computer.

This might not seem like a big deal, but it can remove a lot of clutter.

It also helps a lot when moving programs from one place to another,

since just about every program in the Windows world expects to never be moved from the directory it was installed in.

E.g. moving your "Program Files" directory contents to another drive, and linking the original "Program Files" directory to this new location.

With junction points you can also surpass the 26 drive letter limitation.

Junction points are transparent to programs.

Starting with Windows Vista, junction points are used extensively in the Windows operating system.

Several of the special folders in Windows, such as the "Documents and Settings" folder,

are shown as shortcuts to other folders, but are actually junction points.

Unfortunately, Windows comes with almost no tools for creating junctions.
Prior to Windows Vista, you had to purchase the Windows Resource Kit, which offers the LINKD program for creating junctions. 
In Vista, there is the MKLINK utility for the command line, which is not easy to use.
So that's where Junction Link Magic comes in...

Junction points can only be created on volumes formatted with NTFS 5.0 or higher.
NTFS 5.0 is supported in Windows 2000, Windows XP, Windows 2003 and higher.

symlinker

Symbolic Link Creator. GUI for mklink, Microsoft Windows symlink utility
 
 
http://symlinker.googlecode.com/files/Symlink%20Creator%201.1.1.3.exe
 

Symlink Creator

Program to create symlinks. Provides the CMD command as well.

Description

Just a small application I made to simplify the process of creating symbolic and hard links in Windows 7 and Windows 8 (likely Vista too).

It is a GUI for the MKLINK command in Windows, which makes creating the links much easier.

The program requires administrator permissions due to use of the command prompt, so you should run it as an administrator if you experience any problems.

Note: It is not possible to create a hard link from a network share. You must use a symbolic link instead.

Warning:

If you incorrectly specify the destination path, such as having the destination and source paths backwards,

it will delete the file/folder you are presumably trying to create a symlink from.

You will be presented with a confirmation prompt that shows you which folder will be replaced/deleted. 

It is always a good idea to test this app on empty files or folders.

Features

  • Can create symbolic link or a hard link automatically.
  • Provides the CMD command if manual execution is desired.

http://downloads.sourceforge.net/project/symlink-creator/Symlink%20Creator%201.3/SymlinkCreator.exe

Windows Symbolic Link Maker

Description

This is a GUI interface to make symbolic link for windows vista(or up)

Does exactly what it states. Supports both file links (with hard links) and directory links.

No fuss, no .net required, no install needed.

I used to use DirLinker but it does not work anymore, this does. Just what I needed.

NTFS Link Ext

Description

This is a spin-off from Michael Elsdoerfer's original NTFS Link (hosted at Novell Forge) to incorporate some fixes and extended features.

NTFS Links

Features

Alax.Info NTFS Links extends Microsoft Windows XP operating system shell by adding additional features related to soft and hard links supported by NTFS file system. The description of the features is provided below.

Shell Extensions

The shell is extended with the following new interface items:

  • Additional user interface commands:
    • new item to directory context menus to accept dropped directories as soft links
    • new item to directory context menus to paste copied to clipboard directories as soft links
    • new item to directory context menus to accept dropped files as hard links
    • new item to directory context menus to paste copied to clipboard files as hard links
    • new directory column to display soft linked directory’s link target directory
    • new directory column to display file’s number of hard links
    • new info tip item to soft linked directories to display link target
    • new info tip item to display file’s number of hard links
    • icon overlay to display soft linked directories with a shortcut image
  • Shell operation changes:
    • an option to create move a soft link when a soft linked directory is being moved
    • an option to create a soft link when a directory is being copied
    • an option to copy a soft link when a soft linked directory is being copied
    • an option to delete link when a soft linked directory is being deleted

Download

Current version of Alax.Info NTFS Links can be downloaded from:

  • AlaxInfoNtfsLinksSetup.msi – Version 1.1.5, 32-bit
  • AlaxInfoNtfsLinksSetup-x64.msi – Version 1.1.5, 64-bit (x64)

 Alax.Info NTFS Links is free to use.

NTFS links 的使用是这样的,安装后,右键拖动任何文件/文件夹,在右键菜单中有 Creat Soft Link Here 选项,

选中后就会出现一个名为 Soft Link to XXX 的文件,查看属性,大小也与原文件相同。

但这个 Soft Link to XXX 是个虚拟文件,你在这里的所有操作都会被继承到原文件中,

包括改名,删除。而在原文件中的操作也同样会继承到虚拟文件中。

你说的这个链接,NTFS5 有类似的实现,但是要比 Linux 下面的 soft link/hard link 要差一些

把一个目录指向另一个目录,可以使用 NTFS 的 directory junction point 功能,

这个功能微软只提供了 API 但在explorer.exe/cmd.exe 里面没有提供直接支持,

你可以下载sysinternals 写的命令行工具 junction.exe,注意 rmdir junction point 时会删除实际的目录!

如果只想删除 junction point 的话,要用junction -d

如果是 Vista 的话,微软提供了一个命令 mklink,使用方法可见这里

如果喜欢图形界面的话,可以考虑开源的 ntfslinkext,或者免费的NTFS links。(NTFS links 网页有个 Similar Tools,下面还有几个)

 

WinLink

Description

A software that support creating (directory) symlink, hardlink, and NTFS junctions.

Very simple, all you need is administrative rights.

I am sure everyone knows how to use it. 2.x is developed with Delphi (faster than VB.NET).

HardlinkGUI

Description

If you don't know how command line works or too lazy to open command prompt and want to create a hard link, use this tool.

This only work on Windows 2000 and later with NTFS file system 

[Vista] 符号连结(Symbolic/Soft Link)、永久连结(Hard Link)与连接点(Junction Point)

WINDOWS 的 MKLINK : 硬链接,符号链接 : 文件符号链接, 目录符号链接 : 目录联接..._第1张图片

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

  • :要建立的连结名称(图形里的NewHardLink与NewSoftLink)
  • :被连结的目标,可以是实体目录或档案的完整或部份路径(图形里的OldFileName)

范例:

上列指令执行后的结果: 永久连结和档案符号连结建立了两个档案(红框),目录符号连结和连接点是两个目录,再加上目前目录的点( . )和代表上层目录的两个点( .. )总共四个目录(浅蓝框)。

WINDOWS 的 MKLINK : 硬链接,符号链接 : 文件符号链接, 目录符号链接 : 目录联接..._第2张图片

由档案总管理无法很清楚的看出各种连结的类型,在DOS视窗使用dir /aL命令才能很清楚的看到、等类型。以下是使用dir /s /aL c: 列出Vista输出的内容。由其内容可以看出几个重点:

  • 为了和Windows以前版本相容,C:\Documents and Settings是新建的连接点,连接目标是C:\Users
  • 连接点大量用来建立指向同一个目录的多个名称

 

Mklink is a MS Windows command line utility that you can use to create symbolic links or symlinks and hard links in MS Windows. Mklink is a nice tool for troubleshooting file system problems. Mklink requires administrative privileges.

 

This page relates to explaining the difference between symbolic links and hard links on the Difference between symbolic link and hard link page.

First, let's introduce the mklink command. You can see it in the following print screen:

It is pretty self-explanatory. Now, let's take a look at an example.

Mklink example

The print screen shown below demonstrates creating symbolic links, hard links, and shortcuts in MS Windows and how they differ.

Symbolic link:

  • Created as mklink link_name target_name
  • The icon next to the link is the same like that of a shortcut
  • Occupies zero space in the system
  • It is not a separate file in the file system
  • Interpreted at the operating system level
  • If the original file is deleted, the link is useless
  • Removing the original file does not remove the symbolic link
  • Removing the symbolic link does not remove the original file

Hard link:

  • Created as mklink /H link_name target_name
  • The space it occupies equals the size of the original file, but refers to the same object in the system (not a copy)
  • Interpreted at the operating system level
  • The icon next to the hard link is different from the other objects
  • Removing the original file does not remove the hard link
  • Removing the hard link does not remove the original file
  • If the original file is deleted, its content is still available through the hard link
  • Making a change to the hard link propagates all changes to the original file

Shortcut:

  • Created by right clicking the original file and selecting action from the drop down menu
  • A file in the system that is completely separate from the original file
  • Only those programs that know the concept of shortcuts understand them
  • If the original file is deleted, the link is useless
  • Removing the original file does not remove the shortcut
  • Removing the shortcut does not remove the original file

The print screen below shows these concepts. 

Linux uses a similar command called ln.

Mklink in Windows versus Ln in Linux 

Note, the mklink syntax is a bit different from the Linux ln syntax (ln is used in Linux to create links).

Mklink: The name of the link is followed by the name of the target in the mklink command syntax. If you do not specify any flag, the mklink utility creates a symbolic link by default.

Ln: The name of the target is followed by the name of the link. If you want to create a symlink, you have to specifically include the -s flag.

Difference between symbolic link and hard link

What is the difference between symbolic link and hard link? Explaining the difference between a symbolic link or asymlink and a hard link is easy and vital to knowing about how Linux/Unix environments work.

 

Understanding the difference between a symbolic link and a hard link is also important for web server maintenance and understanding how modern websites work.

What is symbolic link or symlink?

Symbolic link, often called symlink or softlink, is very similar to what we know from Windows - a shortcut. They are kind of shortcuts in the Linux/Unix world. Well, symbolic link can exist in the Windows world too, but for the simplicity of our explanation, let's just work with the comparison that symlink is kind of a shortcut for now. We will get into more details later. Symbolic link contains information about the destination of the target file.

What is hard link?

Hard link (often also called hardlink) is a bit different object when compared to a symlink. Hard link is a directory reference or pointer to a file. Hardlink is a label stored in a directory structure that refers the operating system to the file data when it is accessed. The important part is that hard link is closely tied together with its originating file. If you make changes to a hard link, you automatically make changes to the underlying file that the hardlink is attached to.

Hard link can only refer to data that exists on the same file system.

Many of us are used to Windows where files live in folders. Files in Linux/Unix are not stored in directories. Files in Linux are assigned an inode number which Linux uses to locate files. Each file can have multiple hard links which are located in various directories. A file does not get deleted until there are no remaining hard links to it.

Differences between symbolic link and hard link

Let's summarize our findings. The list bellow summarizes some differences between symlink and hard link:

  1. Hardlink or hardlinks cannot be created for directories (folders). Hard link can only be created for a file.
  2. Symbolic links or symlinks can link to a directory (folder).
  3. Removing the original file that your hard link points to does not remove the hardlink itself; the hardlink still provides the content of the underlying file.
  4. If you remove the hard link or the symlink itself, the original file will stay intact.
  5. Removing the original file does not remove the attached symbolic link or symlink, but without the original file, the symlink is useless (the same concept like Windows shortcut).

In case you are interested getting to know even more details, this list is expanded more on the the Mklink page. The concept is simply that hard links are tied to their sources more rigidly.

What is the difference between symlink and shortcut?

 

 

We mentioned that symlinks are like shortcuts. They are like shortcuts but with some small differences. Symbolic links are automatically resolved by the file system. Any software programs, upon accessing a symbolic link, will see the target instead, whether the program is aware of symbolic links or not. On the other hand, shortcuts are treated like ordinary files by the files system and by software programs that are not aware of them. Only software programs that understand shortcuts (such as Windows) treat shortcuts as references to other files. Shortcuts can point to files or directories that exist in another file system or on the network. The difference between symbolic link and shortcut is clearer from the example mentioned at the bottom of this article, respectively as shown in the big print screen on theMklink page.

 

How to create a hard link or hardlink?

In Linux, you would use the ln command to create a hard link.

$ ln fileA fileB

where fileA is the original file and fileB is the name you want to give to the hardlink. Let's do some research now. You have the original file and one hard link that is attached to it. Now, you look at these two objects with the ls command:

$ ls -il fileA fileB

You can see in the output of this command that both files fileA and fileB have the same inode number (the first number on the line). In addition to having the same inode, both files have the same file permissions and the samesize. Because that size is reported for the same inode, we can see that a hard link does not occupy any extra space on your space.

If you now remove the original file and open the hard link, you will still be able to see the content of the original file.

Note, hard link cannot be created to a folder. If you try creating a hard link to a folder, you will get "Access denied."

How to create a symlink?

Let's demonstrate this with an example. When talking about Linux, you would use the ln command with the -sparameter. You would do something like:

$ ln -s fileA fileB

where fileA is the original file and fileB is the name you want to give to the symbolic link. Now, let's take a look at these two objects with the ls command again:

$ ls -il fileA fileB

You can see that you get different result as compared to when we displayed the hard link. The first difference between symlink and the original file is the inode number. The inode is different for the original file and for the symbolic link. Next, you can also notice that there is the pipe symbol "l" before the permissions on the symlink line. Also, the symbolic link has different permissions than the original file (because it is just a symbolic link). The content of the symlink is just a string pointing to the original file. The size of the symlink is not the same as the size of the original file. The symbolic link is a separate entity and as such occupies some space on your hard drive. You can see at the end of the line where the symlink points to.

You can access the content of the original file directly by calling the original file or by calling the symbolic link. You will see the same result.

Now if you remove the original file, the symlink will still be there. If you try to access the content of the original file through the symbolic link after removing the original file, you will get a message saying there is no such file or directory.

Can I make a symlink to a hard link?

Yes. The hard link functions the same way like the original file; therefore, you can make symlinks to it. You would use in our example the following command:

$ ln -s fileB fileC

where fileB would be the name of the hard link, and fileC would be the name of your new symlink.

Difference between symlink and hard link in Windows?

There are not many differences between symbolic link and hard link in the concept; the concept is the same whether we are working with Linux or Windows; the difference between symlink and hardlink is in how you create them. When talking about MS Windows, you can create three things:

  • shortcut
  • hard link / hardlink
  • symbolic link / symlink

See the following example. The example shown on the next page (click the thumbnail) explains how you can create shortcut, hard link, and symbolic link in Windows and how they differ.

 

How to Create and Use Links with MKLINK in Windows

 

   Information
As so many other things in computing, Windows Links seem more complicated to understand and use than what they really are in reality.

This tutorial will tell you how to create and use links using  MKLINK to redirect system queries made to a local or network storage location to another location.

In this tutorial we will concentrate on the most important link type, a Directory Junction. A junction is a very practical way to customize your file handling and procedures.


Link is basically nothing more than a shortcut. Its purpose is to get Windows to think that a file or folder which is physically located in folder Real on drive A is in fact in folder Fake on drive B by creating a link betweenB:\Fake (a link or junction) and A:\Real (a target folder). Opening the link for instance in Explorer shows contents of target, saving a document to link actually saves it on target. 

As Windows now gets a query from user to open B:\Fake this query is sent to A:\Real. In reality the location B:\Fake does not exist but system answers this query as it was a real folder instead of just a symbolic link and presents user the contents of A:\Real as if it was contents of B:\Fake. Sounds complicated? Don’t worry, it’s quite easy in fact.



Part 1: What is a Link?

   Note
Link in Windows is a virtual object that points to a real physical location. It can be compared to Windows desktop shortcut, doing essentially the same thing in redirecting user to a physical storage location.



Link can be so called Hard Link linking one file to a target file, or a Soft Link linking a folder to a target folder. Soft Links are also called Symbolic Links. Third link type is Junction, basically a hard link but as hard links can only link files we need to use junctions to link folders.

If you are using Windows Vista or later you have most probably used links without ever noticing it. In Windows XP all user data was saved in a folder called C:\Documents and Settings (default name and location). Windows Vista changed this; instead of Documents and Settings the data was divided between two system folders, C:\Users for user specific application data and personal files and folders, andC:\ProgramData for all users shared application data. To allow backwards compatibility all Windows versions since Vista have a linkC:\Documents and Settings with two target folders, ProgramData and Users. 

You can check this by yourself by allowing protected system files and folders to be shown and opening C: drive on Explorer:

(Please notice, screenshots from Windows 8 but links work the same way on Windows 7. Highlighted with yellow = visible system folders created by Windows installation, red = hidden or protected system folders.)

If you try to open Documents and Settings you get an "Access Denied" message. It does not exist, there is no such folder on your C: drive but it is needed for legacy software. 

An example: You want to install Microsoft Office XP on your Windows 7. As Office XP was published for Windows XP long before Vista it uses the XP system folder Documents and Settings to store user data. Vista and later Windows do no longer have this folder so a link is needed. Office XP setup thinks it is creating files and folders normally in Documents and Settings but actually your Windows 7 is lying to Office XP setup, not telling it those files and folders are in fact created in Users and ProgramData. Office XP setup does not notice this deception, installation works and later when user works with Office XP it still thinks the Documents and Settings folder is there, never learning the fact that all queries to that folder are sent further and returned as if they really came from Documents and Settings.



Part 2: Working with Directory Junctions

(1.) Command Syntax

Links are created wit command mklink, short from Make Link. Typing mklink /? on Command Prompt you get the command syntax and options:

Code:
MKLINK [[/D] | [/H] | [/J]] Link Target
 
        /D      Creates a directory symbolic link.  Default is a file symbolic link.
       /H      Creates a hard link instead of a symbolic link.
        /J   Creates a Directory Junction.
        Link    specifies the new symbolic link name.
        Target  specifies the path (relative or absolute) that the new link refers to.

(2.) Use an Elevated Command Prompt

You must use elevated command prompt to work with links.

(3.) Create a Link or a Junction

Creating a directory junction D:\Docs with target E:\Users\Kari\Documents:

Code:
mklink /j D:\Docs E:\Users\Kari\Documents

Windows tells you clearly if the creating of junction has succeeded:

(4.) Main principles when creating a link or a junction

  1. A file or a folder with the name of the intended Link name may not exist. The link file or folder name must be free to use, not reserved by an existing file or folder. If the name is reserved you get an error message. My D: drive already has a folder Test, this is what happens when trying to create a junction with D:\Test (already an existing folder) as link and E:\Users as target:
    Notice that command prompt is telling about a file already existing instead of a folder. This is because the system sees links as shortcuts (files) and not as folders.
  2. Target folder may but must not exist. If it does not exist at the time when a link is created it must be created before you can use the link:
  3. Target can be another Link. Above in (3.) we created a junction D:\Docs with target E:\Users\Kari\Documents. Following command would now create a link C:\Docs with target D:\Docs:
    Code:
    mklink /j C:\Docs D:\Docs
    In fact Windows sees no difference if the target is a real physical folder location or just another link. In this case all queries toC:\Docs would be sent to D:\Docs which would send them further to E:\Users\Kari\Documents. If a user now opens C:\Docsin Explorer, it shows the contents of E:\Users\Kari\Documents:
    Noticed something interesting in screenshot above? The target folder is called Documents but Explorer shows it as My Documents. This is because those "My ..." folders in your user profile folder are actually not real folders. They are directory links:My Videos is simply a link with target Videos and so on.

(5.) Remove or rename a link

Links and junctions can be renamed and removed as any real folders. System automatically modifies registry and sets the target to be the same for a renamed link as it was on the original link. 

This is because even not really existing, the system handles links as if they were real existing folders. This is what Explorer shows when asked for Properties for our example junction D:\Docs:

Only way to really see it is in fact a junction is to get directory listing in command prompt:

The listing not only shows it is a junction but also its target folder.


Part 3: Practical Examples

A typical situation: You have bought a game that must be installed on root level folder C:\Games. Your C: drive is becoming full, so you decide to move the whole C:\Games folder to D:, delete now empty C:\Games (remember, link folder may not exist) and create a junction:

Code:
mklink /j C:\Games D:\Games

When launching the game it still assumes it is located on C: although you, me and Windows know better : it's "secretly" residing on D: but still using the old address on C:.

Another situation: You want to work with some pics from last Christmas, to edit them to be ready to be published on that website of yours. Pics are currently located in X:\Backups\Pictures\My Pictures\Holidays\Christmas 2012.

You can create a junction X:\Pics, and now whenever you want to work with those pics you just type X:\Pics to Run dialog to open the correct folder:

Code:
mklink /j X:\Pics "X:\Backups\Pictures\My Pictures\Holidays\Christmas 2012"

Notice that if a path contains spaces it must be set in between quotation marks as in above target path.

That's about it. Any feedback welcome.

Kari

 

 

 

 

 

你可能感兴趣的:(WINDOWS 的 MKLINK : 硬链接,符号链接 : 文件符号链接, 目录符号链接 : 目录联接...)