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.

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

  1. Sorry to dredge up an old thread, but I found this very useful in understanding the difference between a Junction and a Symbolic Link. I was always curious as to what Symbol Links brought to the table that were new in any way, so thanks for the article.

    I would like to clarify one point, specifically related to the point “stone” was making. There is a difference between how Junctions, Symbolic Links and Volume Mountpoints are treated when performing file system operations such as move/copy etc. If you create a Symbolic Link, and you then move that link it is much the same as a shortcut in that it just moves the link itself, no actual data is moved/copied.

    However, if you move a Junction or Volume Mountpoint, a new folder is created at the move destination point and the contents of the original data folder is physically moved from its source location to the new move location. The Junction or Volume Mountpoint remains where it was, and the source folder remains intact (it just becomes empty).

    So there are differences in the way Windows treats the types of links. Try it out and you will see what I mean.

    I have tested this on Windows 7, I do not have Vista.

  2. one very important information is missing. directory junction links can be moved to another directory and they still point to the right destination. when you however move a symbolic link, it’s no more valid.

    1. My reply comment is only referring to Windows 7, but I imagine it would be the same in Windows Vista. If you move a symbolic link or a directory junction into another folder, both links maintain the correct pointer. If you rename or move the pointer, both links do not update.

  3. Really nice articel. But i think the green link is as symlink to a directory. “The green is a symbolic link to a file.”

  4. self correction…. it looks like vista ueses symbolic links and not hard links (otherwise it would say junction).

  5. Ok… great your article has helped me out quite abit. but how do i get a report of all the existing junction links? Am i correct in assuming that the documents and settings folder is actually a directory junction hard link to the users folder?

    Thanks…

  6. Hi,

    Your article has helped me clarify a lot of things.
    There is one question that I don’t really know: What is the difference between a “directory symbolic link” and a “junction”?

    Thanks in advance!

    1. It is a little complicated but originally Windows 2000/XP only supported directory junctions, but these were not compatible with other file systems, files or network shares. So with Windows Vista Microsoft added file and directory symbolic links. This not only allowed connection with remote SMB network paths ( //SERVER/Directory/ ) but they were also more compatible with the POSIX operating-system standard, aka Linux/Unix links.

      Microsoft had to keep their older Junction system within NTFS for backwards compatibility with Windows 2000/XP. But if you are not using these systems, then you are probably better off sticking with symbolic links.

        1. Hi Maricon thanks for the follow up. Symbolic links were introduced into Vista to aid in interoperability between Windows and POSIX-compatible machines such as Unix, OS-X and Linux.

          “Symbolic links are designed to aid in migration and application compatibility with UNIX operating systems. Microsoft has implemented its symbolic links to function just like UNIX links.”
          http://msdn.microsoft.com/en-us/library/windows/desktop/aa365680(v=vs.85).aspx

          You are correct in that a NTFS junction is technically a soft-link. On an Windows NTFS partition a junction behaves much like a NTFS hard link but it links directories rather than files. When writing this blog entry a number of years ago I tried to simplify the terminology to make it less confusing for the uninitiated.

Leave a comment