MKLINK - CMD
Overview
MKLINK is a command-line utility in Windows that allows users to create symbolic links, hard links, and directory junctions. These links serve as references to other files or directories in the system, facilitating better file management and system organization. Symbolic links are particularly useful for linking files across different volumes, redirecting folder contents without moving the actual data, and creating shortcuts to files and folders.
Syntax
The basic syntax for MKLINK command is:
MKLINK [[/D] | [/H] | [/J]] Link Target
- Link: Specifies the name of the new symbolic link that is being created.
- Target: Specifies the path (relative or absolute) that the new link refers to.
Variations
MKLINK /D Link Targetcreates a symbolic link to a directory.MKLINK /H Link Targetcreates a hard link instead of a symbolic link.MKLINK /J Link Targetcreates a Directory Junction.
Options/Flags
/D: Creates a directory symbolic link. Default is a file symbolic link./H: Creates a hard link instead of a symbolic link./J: Creates a Directory Junction.
Examples
-
Creating a symbolic link for a file:
MKLINK linkname.txt targetfile.txtThis command creates a symbolic link
linkname.txtpointing totargetfile.txt. -
Creating a symbolic link for a directory:
MKLINK /D linkdir targetdirThis command establishes a symbolic directory link
linkdirthat points totargetdir. -
Creating a hard link:
MKLINK /H linkfile.txt targetfile.txtHere,
linkfile.txtis a hard link totargetfile.txt, meaning both files now reference the same data on the disk. -
Creating a Directory Junction:
MKLINK /J linkjunction targetdirThis creates a junction
linkjunctionthat points totargetdir.
Common Issues
- Permission Errors: Creating symbolic links requires administrative privileges. To resolve, run the command prompt as an administrator.
- Incorrect Path: Errors may occur if the target path does not exist or is typed incorrectly. Double-check the target path for accuracy.
Integration
MKLINK can be combined with other commands for more complex tasks. For example:
FOR /R %x IN (*.txt) DO MKLINK "C:\linked\%~nx.txt" "%x"
This command creates symbolic links for all .txt files in a directory, placing the links in the C:\linked directory.
Related Commands
- DIR: Useful for listing the contents of a directory, including symbolic links.
- DEL: Can delete symbolic links.
For further reading, visit the official MKLINK documentation.