Symbolic Links in Windows

Source: Internet
Author: User
Tags symlink

After changing the new version, I was uneasy about Windows Server 2003 R2. I also tried Windows Vista, Windows 7, and Windows Server 2008 before, but finally I gave up because of various unpleasant reasons, the main reason is that the computer configuration is too bad. This time, I finally had the opportunity to upgrade the system, so I installed Windows Server 2008 R2. After two months, I felt pretty good, and the performance in all aspects was quite satisfactory, it's pretty cool.

It is found that the user directory structure of 2008R2 has some changes (Win7 seems to be the same), instead of the previous SYS: \ Documents ents and Settings \ xxx, and changed to SYS: \ Users \ xxx, even if you use dir/a to view the data, a hidden sources and Settings point to the JUNCTION of [SYS: \ Users. In addition, the directory structure under SYS: \ User \ xxx \ has also changed significantly. Many directories in earlier Windows versions have been moved to SYS: \ User \ xxx \ AppData, SYS: the directories under \ User \ xxx \ are mostly the directories where some User files are stored. They were previously stored in "My Documents" and are now stored in the User Directories, A bunch of "My xx" directories.

This is the problem. In the past, I moved my documents directly to D through the "move" operation. Now I want to move a lot of directories, and some cannot move them. Google, I found a lot of related articles, and I do not like the method of modifying the Registry. In contrast, I think this foreigner's "How to Move your Windows User Profile to another Drive" is very good, very good and powerful! NTFS had long supported JUNCTION, but I never knew it! It may also be because I am using Linux. I am especially fond of using symbolic links. It's easy to move my user directory to D: disk, and mklink is really powerful. In the past few days, I moved the git library directory of my OpenSSHServer to my Workspace directory, and only one JUNCTION was put in OpenSSHServer, then, mklink several directories that are frequently synchronized with the mobile hard disk to a directory. In this way, it is too convenient to synchronize a directory!

Today, I suddenly wanted to try mklink's performance in XP. NTFS supports JUNCTION. I did not find mklink.exe in the entire system disk of 2008r2. I did not know it after Google. It turns out that mklink is a DOS internal command! Dizzy, And Then Google "mklink xp”, found two tools, junction.exeand linkd.exe, can achieve directory JUNCTION, really good! Unfortunately, I have never known it. In this regard, I am biased towards Windows, and I always think it is too bad.

My experience on directory connection <JUNCTION> and directory symbolic link <SYMLINKD>:
Mklink/j creates a directory connection <JUNCTION>, which is similar to a hard link but not a hard link. It uses an absolute path. The created link can be moved to any location;
Mklink/d creates a directory symbolic link <SYMLINKD>. It can use a relative path and a link created from a relative path. It cannot work after being moved.
In addition, mklink/j is only used to create a directory connection <JUNCTION>, while Symbolic Links can create files or directories.

Finally, I will repost some text about mklink, because I cannot figure out the final author, and I would like to express my sincere respect!

---------------------------------------------- Invincible split line ---------------------------------------------------------

1. What is mklink?

Where is mklink sacred? Is it worth writing?
If you have used Linux, you must have seen symbolic links (this is a special file) and the "ln" link command. The mklink and ln functions are almost the same.
Mklink is used to create symbolic links. This command is not well-known and does not use many people.

Since Vista, Microsoft has secretly used this tool in widows. In the user directory, you can see a lot of files that are similar to shortcuts. Of course, this is hidden, but it cannot be opened. In fact, this is a symbolic link.

> Dir C: \ Users \ wclu/
The volume in drive C is win7
The serial number of the volume is 8CD0-A743

C: \ Users \ wclu directory

2009/06/11 :42 <DIR>.
2009/06/11 :42 <DIR> ..
2009/06/11 <DIR> AppData
2009/06/11 <JUNCTION> Application Data [C: \ Users \ wclu \ AppData \ Roaming]
2009/06/11 <DIR> Contacts
2009/06/11 <JUNCTION> Cookies [C: \ Users \ wclu \ AppData \ Roaming \ Microsoft \ Windows \ Cookies]
2009/06/11 10: 05 <DIR> Desktop
2009/06/11 <DIR> Documents
2009/06/11 <DIR> Downloads
2009/06/11 <DIR> Favorites
2009/06/11 <DIR> Links
2009/06/11 <JUNCTION> Local Settings [C: \ Users \ wclu \ AppData \ Local]
2009/06/11 <DIR> Music
2009/06/11 <JUNCTION> My Documents [C: \ Users \ wclu \ Documents]

The above marked red file is a symbolic link (the above file is not all files under the C: \ Users \ wclu directory ).

Vista/Windows 7 uses these symbolic links to ensure compatibility with XP and other systems.

Ii. mklink usage

Mklink can only be used in the command prompt "CMD.

> Mklink /?
Create a symbolic link.

MKLINK [[/D] | [/H] | [/J] Link Target

/D. Create a directory symbolic link. Token considers the file
Symbolic Link.
/H creates a hard link instead of a symbolic link.
/J creates a directory connection.
Link specifies the new Symbolic Link name.
Target specifies the path referenced by the new link
(Relative or absolute ).

Note: The above symbolic links are equivalent to soft links in Linux.

To test the mklink function, I create directory a in partition C and store the file 1.txtin the directory.

Create File Link
E: \> mklink c: \ 1 c: \ a \ 1.txt: access is denied.

E: \> mklink c: \ 2.txt c: \ a \ 1.txt
The symbolic link created for c: \ 2.txt <<==> c: \ a \ 1.txt

It can be seen that when creating a link for a file, the extension of the symbolic link file must be consistent with that of the original file.

E: \> mklink c: \ a \ 1.txt d: \ 3.txt
If the file already exists, the file cannot be created.

E: \> mklink d: \ 3.txt c: \ a \ 1.txt
The symbolic link created for d: \ 3.txt <<==> c: \ a \ 1.txt

It can be seen that when creating a link for a file, it must be in the format of "original file name of mklink Symbolic Link.

Create directory Link
E: \> mklink/d c: \ B d: \
Symbolic Link created for c: \ B <<==>> d: \

Although the d: \ a directory does not exist, the link can still be created, which is very powerful. However, an error is prompted when the link is opened.

E: \> mklink/d: \ a c: \
The symbolic link created for d: \ a <<==>> c: \

E: \> dir d:/
The volume in drive D is soft
The serial number of the volume is the BE86-8884

D: \ directory

2009/06/11 10: 00 <DIR> $ RECYCLE. BIN
2009/06/11 13:08 <SYMLINK> 3.txt [c: \ a \ 1.txt]
2009/06/11 13:18 <SYMLINKD> a [c: \ a]
2009/06/11 :54 <DIR> Program Files
2009/06/11 <DIR> System Volume Information
1 file 0 bytes
4 directories, 31,089,442,816 available bytes

When the linked file in partition D is deleted, the files and folders in partition C are not affected.

Iii. Similarities and Differences between mklink/d and makelink/j

You can see from the help of mklink that both can create directory links.

Careful personnel can find that the former is to create directory links, while the latter is to create directory connections.

What are the differences between words?

The following two methods are used to create links for the same directory:

E: \> mklink/j d: \ a c: \
Join created for d: \ a <<==>> c: \

E: \> mklink/d: \ B c: \
The symbolic link created for d: \ B <<==>> c: \

The following shows the list of files and directories in partition D:

E: \> dir d:/
The volume in drive D is soft
The serial number of the volume is the BE86-8884

D: \ directory

2009/06/11 10: 00 <DIR> $ RECYCLE. BIN
2009/06/11 13:33 <SYMLINK> 1.txt [c: \ a \ 1.txt]
2009/06/11 13:34 <JUNCTION> a [c: \ a]
2009/06/11 13:35 <SYMLINKD> B [c: \ a]
2009/06/11 :54 <DIR> Program Files
2009/06/11 <DIR> System Volume Information
1 file 0 bytes
31,089,442,816 bytes available for five Directories

When mklink does not add parameters or add parameters/d, the created link file is <SYMLINK> (<SYMLINKD> indicates the link to the directory ).

When you add a parameter/j, you can only create a connection for the Directory and the created connection file is of the <JUNCTION> type. There are significant differences between the two types of link files:

From the initial "> dir C: \ Users \ wclu/a", we can see that Microsoft uses the <JUNCTION> type at its own discretion.

Iv. Features of linked files

I just talked about how to use mklink to create a link file. Next I will focus on how to link the file. Since it is a feature, it is different.

The symbolic link file itself cannot be "copied" (it can only be copied using special methods)
When you copy a symbolic link file, the target file or directory linked to the symbolic link is copied, not the symbolic link file itself.
How can we copy symbolic link files?
> Copy /?
/L if the source is a symbolic link, copy the link
To the target, instead of the actual file that the source link points.

> Xcopy /? /B Copies the symbolic link itself against the Link Target

/* D: \ 1.txt is a <SYMLINK> symbolic link */
E: \> xcopy d: \ 1.txt e: \ 1.txt/B
Target E: \ 1.txt is the file name
Or directory name
(F = file, D = directory )? F
D: \ 1.txt
Copied one file.

/* D: \ a is a <JUNCTION> symbolic link */
E: \> xcopy d: \ a e: \ a/B
Target E: \ a is the file name
Or directory name
(F = file, D = directory )? D
Copied 0 files

/* D: \ B is a <SYMLINKD> symbolic link */
E: \> xcopy d: \ B e: \ B/B
Target E: \ B is the file name
Or directory name
(F = file, D = directory )? D
Copied 0 files

E: \> dir e:/
The volume in drive E is data
The serial number of the volume is the FEEE-FB51

E: \ directory

2009/06/11 10: 00 <DIR> $ RECYCLE. BIN
2009/06/11 13:33 <SYMLINK> 1.txt [c: \ a \ 1.txt]
2009/06/11 13:34 <DIR>
2009/06/11 13:35 <SYMLINKD> B [c: \ a]
2009/06/11 <DIR> System Volume Information
1 file 0 bytes
4 directories, 31,362,203,648 available bytes

It can be seen that the <SYMLINK> symbolic link file can be successfully copied using the xcopy command and the parameter/B, but the <JUNCTION> link cannot be copied.

Transparency of symbolic link files
The symbolic link operation is transparent: The program that reads and writes the symbolic link file performs operations on the target file directly.
Common file operation commands (such as cp and rm) can directly act on symbolic links. The Command Used To Read and Write File Content will directly access the target file.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.