Programming Field

Mklink - DOS/Command Prompt Reference

[Windows Vista or later] Creates a link file. You can create symbolic links, hard links, and junction points (junctions).

* Mklink can be used on disks formatted with the NTFS/ReFS file system. It is not compatible with FAT/FAT32/exFAT.
* Creating symbolic links requires administrative privileges (SeCreateSymbolicLinkPrivilege). This requirement does not apply to creating hard links or junctions.

Syntax

mklink [/D | /H | /J] <link-name> <target>

Options

/D | /H | /J Specifies either /D, /H, or /J, or executes without specifying anything. Each option has the following meanings:
(not specified)
Creates a symbolic link for the file with the name <link-name>, regardless of <target>. If <target> is not a valid file, the operation succeeds but results in an inaccessible file.
/D
Creates a symbolic link for the directory with the name <link-name>, regardless of <target>. If <target> is not a valid directory, the operation succeeds but results in an inaccessible directory.
/H
Creates a hard link for the file with the name <link-name>. The operation fails if <target> is not a valid file.
/J
Creates a junction with the name <link-name>. If <target>em> is not a valid directory, the operation succeeds but results in an inaccessible directory.
<link-name> Specifies the name (file name/directory name) of the link to be created using a relative or absolute path. The operation fails if the specified name points to an already existing file/directory.
<target>

Specifies the name to which the link points using a relative or absolute path. In the case of creating not a hard link (i.e. creating symbolic link/junction), you can specify non-existent file names/directory names. However, attempting to access a link pointing to a non-existent file or directory will result in an invalid name error.

Note that there is a difference in specifying relative paths and absolute paths when creating symbolic links. Please refer to ‘Details’ for more details.

* Mklink is one of the commands that can be executed in the Command Prompt, and it is not an executable file (application).
* If the command execution is successful, the exit code will be 0. In case of failure, it will be 1.

Details

Usage of Mklink

The Mklink command enables the creation of ‘Symbolic Links’, ‘Hard Links’, and ‘Junctions’ on drives formatted with the NTFS/ReFS file systems. The main features of each, including ‘Shortcuts’, are as follows:

Symbolic link
A symbolic link contains a file name internally and behaves as if it were the file pointed to by that file name. The symbolic link itself is created as either a file or a directory, and it can be treated as a normal file or directory (regardless of the existence or name of the target).
The file/directory pointed to by a symbolic link can be manipulated independently of the symbolic link itself. Even if these files/directories are moved or deleted, the name of the target stored internally in the symbolic link remains unchanged. Note that if the target does not exist, it is treated as an invalid file name (directory name).
Please note that when accessing symbolic links over the network (within a shared folder on the network), the reference information of the symbolic link is applied to the computer accessing it. This means that you will access the files/directories on the source (your) computer. Be cautious when symbolic links exist within shared folders.
* If you want to avoid the mentioned issue (referencing data on the computer where the shared folder's original data is located), consider using hard links for files and junctions for directories.
Hard link
A hard link shares all aspects of a file (file data, extended data, attributes, file timestamps, etc.). In reality, it's not a relationship between the source and destination links; instead, multiple file names are assigned to the same file content. Therefore, a file created as a hard link is treated exactly the same as the original file. Additionally, when one file is deleted, the content continues to exist as long as the other file is not deleted (and vice versa). However, since the file is not duplicated, editing the content using one file name will reflect those changes when accessed with the other file name.
If a symbolic link references a file, and that file is deleted, then a new file created with the same name will be referenced by the link. However, if a similar operation is performed with a hard link (deleting the linked file and creating a new file with the same name), the hard link will continue to reference the original file and won't switch to the new file.
Junction (Junction point)
A junction contains a directory name internally and behaves as if it were the directory pointed to by that name. Essentially, it is similar to a symbolic link, but it predates symbolic links and has some specifications that differ from symbolic links. For example, there are constraints such as ‘valid only for directories’, ‘cannot use network paths in the target’, and ‘the name stored internally is always an absolute path’. When moving a junction to a different directory, such as in File Explorer, a new directory with the same name is created, and the contents of the linked directory in the junction are moved into the newly created directory (while the junction and the linked directory remain as they are). However, when moved using the Move command, the junction point itself is moved.
Similar to symbolic links, the target directory does not need to exist for junctions (conversely, if the target does not exist, the junction is treated as an invalid directory).
Unlike symbolic links, when accessing a junction over the network (within a shared folder on the network), the contents will be the actual target data on the computer where the junction was created.
* In NTFS, both symbolic links and junctions are provided using a feature called "Reparse Points." Reparse points include various types, not just symbolic links and junctions, and all of them contain the letter ‘l’ in the attributes that can be obtained with the ‘%’ extension.
Shortcut
A shortcut is a file that contains the file/directory name of the target as data. The extensions are ‘.lnk’ (shortcut file), ‘.pif’ (MS-DOS program shortcut), or ‘.url’ (internet shortcut). When you directly open a shortcut file, it accesses binary data that includes target information. The functionality to open the target pointed to by a shortcut file is provided not through the OS file API but as a shell function (utilized by File Explorer and similar applications). Therefore, opening the target directly from a shortcut file without involving the shell is generally not possible, and the shortcut file itself cannot be treated as the target file.
* The PIF file is an MS-DOS Program Shortcut file supported at the system level and is treated as a type of executable file. PIF files are used when running MS-DOS applications under specific conditions/settings. They can be edited with the PIF Editor (Windows 3.1) or through the file properties (Windows 95 and later). However, PIF files cannot be used on 64-bit versions of Windows that cannot execute MS-DOS programs.

There are two types of symbolic links: file links and directory links. When creating a symbolic link, you specify whether to create it as a file or a directory using an option, regardless of whether the target is a file or a directory. (If you use the /D option, it creates a directory link; otherwise, it creates a file link.) If the target does not exist or if there is a mismatch between the target and the link itself (e.g., the target is a file and the link is a directory), the link cannot be used as a file or directory.

Note that junctions store the target as an absolute path; therefore, if <target> is specified as a relative path, it will be converted to an absolute path based on the current directory at the time of creation. On the other hand, if a symbolic link is created with <target> specified as a relative path, that relative path is directly included in the symbolic link. The relative path is treated with respect to the directory where the link is created, not the current directory.

  • If <link-name> is ‘symlink1’ and <target> is ‘..\target1’, the symbolic link ‘symlink1’ will be created in the current directory, and the link will point to ‘target1’ in the parent directory of the current directory.
    • Subsequently, if ‘symlink1’ is moved to the C:\Data directory, the target information will still be maintained as ‘..\target1’, ultimately resulting in it pointing to ‘C:\target1’.
  • If <link-name> is ‘D:\MyFolder\symlink2’ and <target> is ‘..\target2’, the symbolic link ‘symlink2’ will be created in D:\MyFolder, and the link will point to ‘D:\target2’.
    • Subsequently, if ‘symlink2’ is moved to the D:\Backup\20150924 directory, the target information will still be maintained as ‘..\target2’, ultimately resulting in it pointing to ‘D:\Backup\target2’.
  • If <link-name> is ‘..\symlink3’ and <target> is ‘X:\Hello\target3’, the symbolic link ‘symlink3’ will be created in the parent directory of the current directory, and the link will point to ‘X:\Hello\target3’.
    • Even if ‘symlink3’ is later moved to the E:\Notes\Quarter directory, the target information being ‘X:\Hello\target3’ means that symlink3 will continue to point to ‘X:\Hello\target3’.

Samples

Sample 1

mklink P:\Options\MyApp.ini %CD%\myopts.ini

[Extensions] Creates a file symbolic link named ‘MyApp.ini’ in the ‘P:\Options’ directory, pointing to the ‘myopts.ini’ located in the current directory. To ensure the symbolic link's path is an absolute path, uses the ‘CD’ environment variable available when the extension is enabled.

Sample 2

mklink /J E:\Dev\Include\ALib ..\ALib

Creates a junction point named ‘ALib’ in the ‘E:\Dev\Include’ directory, pointing to the ‘ALib’ directory located in the parent of the current directory. Since junctions store absolute paths, even if the ‘E:\Dev\Include\ALib’ junction is moved to a different location, the reference destination of the link will not change.