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 Target creates a symbolic link to a directory.
  • MKLINK /H Link Target creates a hard link instead of a symbolic link.
  • MKLINK /J Link Target creates 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

  1. Creating a symbolic link for a file:

    MKLINK linkname.txt targetfile.txt
    

    This command creates a symbolic link linkname.txt pointing to targetfile.txt.

  2. Creating a symbolic link for a directory:

    MKLINK /D linkdir targetdir
    

    This command establishes a symbolic directory link linkdir that points to targetdir.

  3. Creating a hard link:

    MKLINK /H linkfile.txt targetfile.txt
    

    Here, linkfile.txt is a hard link to targetfile.txt, meaning both files now reference the same data on the disk.

  4. Creating a Directory Junction:

    MKLINK /J linkjunction targetdir
    

    This creates a junction linkjunction that points to targetdir.

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.

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