Symbolic links, Hard Links, Soft Links, Junctions - Creation and Use
A previous article described the file and directory aliases called hard links, symbolic links (symlinks), and junctions. This article describes how to use the command-line utilities Fsutil and Mklink to create these links and what the links are used for.
Command Line Tools for Creating File and Folder Links
Windows 7 comes with two command-line tools for the creation of various kinds of file and folder aliases. Generally, elevated or administrator privileges are required to use them. How to run the command prompt as an administrator is explained at this link.
The various types of links that are discussed below are defined in a previous article.
How to create hard links with Fsutil
Commands with Fsutil have been available at least since Windows XP but only hard links can be created this way. Here is the command to create a hard link "new_name" linked to an existing file "existing_name":
fsutil hardlink create new_name existing_name
Hard links are applicable to files only. All linked files must be on the same volume. Each hard link acts like a copy of a file but only one copy of the file actually exists.
Creating links with Mklink
The command line tool Mklink was introduced in Windows Vista for the specific purpose of creating links, either hard or soft. Its default action is to create soft links for files but there are a number of switches that can be used to create other types of links. The syntax for commands is given below.
mklink [[/d] | [/h] | [/j]] <Link> <Target>
Here <Link> specifies the name of the symbolic link that is being created and <Target> specifies the path (relative or absolute) that the new symbolic link refers to. The three types of links defined by switches are explained in the table below.
|Switch||Action and properties|
|/d||Creates a symbolic (soft) link for directories. Relative paths are supported. Can be used on networks. Deleting the target strands and isolates the link|
|/h||Creates a hard link. Files on the same volume only.|
|/j||Creates a directory junction. Same computer only. A type of soft link. Deleting the target strands and breaks the link|
For example, to create a directory soft link C:\MyNewFolder to a directory D:\MyFolder, the command would be:
mklink /d C:\MyNewFolder D:\MyFolder
If the target D:\MyFolder is moved, renamed, or deleted, the link C:\MyNewFolder will be broken.
How to Use Hard Links, Symbolic Links, and Junctions
Here are some situations where links might be used:
- Program redirection
If you wish to move a program installed on the C:\ drive to another drive, say the D:\ drive, but maintain the original path references in the Registry and elsewhere, a soft link can be used.
- Save disk space
If you want access to a large directory from more than one place, a directory symbolic link (symlink) can be used. Note that the directory symlink is not an actual copy but only a pointer so it cannot serve as a backup. Deleting the target deletes the folder.
- Synchronizing folders or files with symlinks
If you want two directories to always have the same contents, a symlink can be used. For example, symlinks can keep a Dropbox folder updated from more than one location. Again, however, note that only one copy of the folder actually exists.
- Synchronizing files with hard links
Files on the same volume can be synchronized so that changes at one address occur at all addresses. In the case of hard links, deleting the original target does not affect other links and the file remains as long as one hard link exists. Deleting the target of a soft link deletes the file and breaks the soft link.
Listing symbolic links and junctions
A list the symbolic lists and junctions on a given volume (for example, the C:\ drive) can be obtained in the command line. Open a command prompt and enter:
dir /s /al C:\