The Developer's Tidbits

Helpful tips for random problems
The skyline of Sydney, Australia - used as a header image for aesthetics
The skyline of Sydney, Australia - used as a header image for aesthetics

windows

Windows File Junctions, Symbolic Links and Hard Links

Reading time for 851 words - 4 minutes

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.

A 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.

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

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

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.

Downloads properties
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
Command Prompt
Examples of Window’s symbolic link types

Written by Ben Garrett