Windows

Windows File Junctions, Symbolic Links and Hard Links


For a long time, Linux and Unix have had the fantastic ability to allow user created links on their file systems. These links are file and folder pointers that are sorted and registered by the operating system file partition. But unlike the more common Microsoft Windows shortcut, programs that encounter user-created links will treat them as the source file or folder.

These pointers are advantageous for many reasons primarily if you use multiple partitions on your computer. I, for example, keep all my work-related files on a separate partition labelled as the W: drive. That allows me to optimise searches and backups as well as keeping the partition more secure from potential problems.

Part of my work involves web development and for testing often the files I am working with need to be located within a web server’s root directory. But this can be problematic if the server application is on the C: drive. So a hard link is useful as I can create a pointer from the server root directory and target it to the folder containing the project files on the W: work drive. When the project is over the hard link can be removed which would sever the connection to the testing web server without the need to move any files.

The Windows NTFS file system has supported some form of file and directory pointing since Windows 2000. But this article will only focus on the implementation used in Windows 6 (Vista) through to Windows 10.

Shortcuts, symbolic links, hard links & junctions?!

shortcut is a file that points to another file. It is an antiquated pointing system from the Windows 95 era that many programs do not recognise. Shortcuts do not only use up space on the hard drive, but they also break and linger behind after the deletion, renaming or moving of the target.

A symbolic link is similar to a shortcut, but instead of being saved as a file it is registered to the hard drive partition. It does not use any disk space, and all programs recognise both the link and the target. A symbolic link can point to any file or folder either locally on the computer or over a network using an SMB path.

A file hard link is a little different and can not be used over multiple partitions meaning you can not have a link on drive C: pointing to a file on drive D:. A file hard link points to and duplicates a target as a mirrored copy, but the duplicate does not use any additional space on the hard drive partition. So two hard links that mirror a 1 GB file would in total only use 1 GB on the partition rather than 3 GB. Importantly if either the hard links or the target were to delete, the other links retain the data. Changes to the content of either the target or the links automatically propagate to all other items.

A junction behaves like a hard link for directories, but unlike file hard links you can create junctions that span multiple partitions. Again a directory junction and its content are stored on the hard drive partition, but they do not use any additional space. Any changes to the content within either the target or the links will automatically propagate except where the target directory is deleted or renamed. In that case, all links that point to the target will break and linger on the partition.

Creating links

Microsoft Windows uses the command line program mklink to create links [1]. It has three slash (/) parameters and requires both a link name and target. Multiple slash parameters cannot be together.

mklink /D /H /J [LINK NAME] [TARGET]

Supplying no slash parameters creates symbolic file link which is a pointer to a file.

/D parameter creates a symbolic directory link which is a pointer to a directory.

/H parameter creates a hard file link and is best used in situations where you need multiple mirrors of a file.

/J parameter creates a directory junction which is a directory link that mirrors the target directory over the same or on a different hard drive partition.

Examples

mklink /J Win10 C:\Windows

This example would create a directory junction named Win10 that linked to the directory C:\Windows.

mklink /D C:\User\Ben\Desktop\Downloads C:\User\Ben\Downloads

It creates a symbolic link on my Desktop to link to my Downloads directory. But unlike a shortcut, I can right-click on the generated icon to view the target’s properties and even apply folder attributes.

Shortcut and symbolic link
The general properties of both a shortcut and a symbolic link to the Downloads folder

Below is a screen capture of the four different types of links in use on Windows.

  • The blue is a symbolic link to a file
  • The red is a directory junction
  • The yellow is a hard file link that does not consume any additional hard drive space
  • The green is a symbolic link to a directory
windows symbolic links
Examples of Window’s symbolic link types

 

[1] Windows XP and 2000 did not use mklink but instead used 3rd party tools such as linkd or junction.

Advertisements

57 thoughts on “Windows File Junctions, Symbolic Links and Hard Links

  1. Hello, I have done this a few months ago in windows 7 without issues.
    But currently I am getting an error in windows, as if the junction link is not working.
    When trying to login i get “The user profile service service failed the logon. User profile can not be loaded.”

    E = SSD
    D = HHD
    (as shown from disk part)

    robocopy /copyall /mir /xj E:\Users D:\User
    (no errors, takes about 15secs)

    rename e:\users e:\users.bak (also note i have renamed the folder to something like TESTING) just in case the .bak was confuisng it)

    mklink /J E:\Users D:\Users

    Do a few tests, If i go to E:\users and create a folder, then go to D:\users i see it; all looks good. And still same issue.
    I have done this about 10times to make sure im not being retarded.

    One fishy thing that i have seen, is if i use rmdir e:\users (deleting the junction), then the d:\users should be deleted, but its NOT! It is staying there…

    Anything that i am doing wrong? I swear this is what i did like 6months ago with two normal HHDs…

    1. Well I don’t know if this is a typo in the comment … but you are missing the trailing S on D:\User

      robocopy /copyall /mir /xj E:\Users D:\User

      But your symlink is pointing to D:\Users

      mklink /J E:\Users D:\Users

    1. I have never done it myself nor seen it written about. The System Volume Information directory is specifically used to house Windows System Restore files and databases. Considering the purpose of System Restore is to have a ready fail-safe recovery point in case your operating system, software or some drivers go haywire to be on the cautious side it is probably best not to play around with this. If you don’t need this extra protection and wish to save some disk space you could simply turn System Restore off.

  2. My computer has an SSD (boot drive) and an HDD (data drive). I have relocated the Documents folder to the HDD, but would like for one small folder (which needs to be be in the Documents folder for some program to work) to be stored on the SSD for decreased latency. If I use a directory junction, will the HDD need to be read to reach the link in the file system? Is there an interim case where the file system is in some chip in the HDD assembly at the end of the SATA cable, but not on the disk that has to spin up?

    1. Sorry for the slow reply Luke I must have mistakenly missed your question.

      I am pretty sure that the junction data will be contained within the NTFS file system on the HDD rather than your target SSD drive. So the HDD containing this junction the target details will probably need to be read at least once. I don’t know if after it has been read for the first time if Windows remembers the target and keeps the information in the system memory.

    1. I did a quick test on Windows 7 and managed to create a symbolic directory link to a shared directory on my local network.

      I ran command prompt as an Administrator and then ran the following

      mklink /d testlink \\NAS\my_shared_folder_name

      I could then cd \testlink and that would take me to the folder stored on the NAS. I hope that helps.

  3. Okay, everyone seems to have skipped over this so I’m wondering if I’m just not familiar with it because I’m not as used to Windows as I am Linux…

    How did you get the color support in your terminal? Is it a terminal replacement of some sort or some special hack or just some image editing trick?

    Other than that; very decent article. You may wish to know, however, that on most UNIX/Linux systems a symbolic link does, in fact, take up space though it is not much. It is created as a special independent file which takes up a minimal amount of space and acts as a pointer to a different location where the target file may be found. Hard links share the target file’s inode and, as such, don’t take extra space in the filesystem itself. As well, because POSIX-compliant systems follow the ‘everything is a file’ philosophy, there is no distinction between a ‘file’ link and a ‘directory’ link. So one needn’t worry about figuring out how to create a ‘directory junction’ in *nix.

    1. Hi GoSteen unfortunately that screenshot is not a colour terminal rather it involved some post image editing to simply highlight the different types of links. As a Linux user yourself I would highly recommend using Cygwin and combine it with Mintty for a decent terminal for Windows. It also gives you a slew of ported Linux shell apps, full access to your ntfs partitions and most importantly colour.

      http://code.google.com/p/mintty/

  4. Sorry – that should read:

    From the cmd prompt (regardless of which drive or directory I start from), when I enter

    “mklink /j c:\users e:\users”

    I receive error message: ‘Cannot create a file when that file already exists.’

    Could you please advise whether (a) it is possible to create a junction across partitions? And (b) if so, what is the correct syntax?

    Many thanks in anticipation.

    1. You syntax directory / target syntax looks to be wrong way around, you need do LINK NAME then TARGET. Your example is trying to join E:\users and create the junction at c:\users. But that will not work because you have an existing directory already there.

      Try “mklink /j e:\users c:\users” 🙂

  5. After a clean install of my Vista 32-bit OS, I am taking the opportunity to redirect my user folders away from the C: drive. It is a process fraught with frustration due to duplication of folders and inability to delete the empty original folders. Although there are plenty of helpful suggestions out there for complex registry edits, this seems a cumbersome and untidy – not to mention risky – approach. So a MKLINK /J redirection from source to my desired partition would be an elegant and convenient ‘solution’, if I can make it work.

    But it’s not working at the moment! From the cmd prompt (regardless of which drive or directory I start from), when I enter I receive error message: ‘Cannot create a file when that file already exists.’

    Could you please advise whether (a) it is possible to create a junction across partitions? And (b) if so, what is the correct syntax?

    Many thanks in anticipation.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s