How to use the command 'mklink' (with examples)
- Windows
- December 25, 2023
The mklink
command is a Windows command that allows you to create symbolic links, hard links, and directory junctions. Symbolic links are similar to shortcuts and can be used to reference files or directories in different locations. Hard links are like copies of files, but they don’t take up any additional disk space. Directory junctions are used to create symbolic links to directories instead of files.
Use case 1: Create a symbolic link to a file
Code:
mklink path\to\link_file path\to\source_file
Motivation: Creating a symbolic link to a file can be useful when you want to access a file from a different location without making a copy of it. This can save disk space and also make it easy to update the file in one location, with changes being reflected in both the original file and the symbolic link.
Explanation:
path\to\link_file
: Specify the path and filename of the symbolic link you want to create.path\to\source_file
: Specify the path and filename of the file you want to create a symbolic link to.
Example output:
Symbolic link created for path\to\link_file <<===>> path\to\source_file
Use case 2: Create a symbolic link to a directory
Code:
mklink /d path\to\link_file path\to\source_directory
Motivation: Creating a symbolic link to a directory allows you to access files and subdirectories in that directory structure from a different location. This can be useful when you want to organize files in a different way without physically moving them.
Explanation:
/d
: This optional argument specifies that you want to create a symbolic link to a directory instead of a file.path\to\link_file
: Specify the path and directory name of the symbolic link you want to create.path\to\source_directory
: Specify the path of the directory you want to create a symbolic link to.
Example output:
Symbolic link created for path\to\link_file <<===>> path\to\source_directory
Use case 3: Create a hard link to a file
Code:
mklink /h path\to\link_file path\to\source_file
Motivation: Creating a hard link to a file allows you to have multiple references (links) to the same file. This can be useful when you want to have different ways of accessing a file without duplicating it or using up additional disk space.
Explanation:
/h
: This optional argument specifies that you want to create a hard link to a file instead of a symbolic link.path\to\link_file
: Specify the path and filename of the hard link you want to create.path\to\source_file
: Specify the path and filename of the file you want to create a hard link to.
Example output:
Hard link created for path\to\link_file <<===>> path\to\source_file
Use case 4: Create a directory junction
Code:
mklink /j path\to\link_file path\to\source_file
Motivation: A directory junction is used to create a symbolic link to an entire directory, including all its files and subdirectories. This can be useful when you want to access files and subdirectories from a different location without actually moving them.
Explanation:
/j
: This optional argument specifies that you want to create a directory junction instead of a symbolic link.path\to\link_file
: Specify the path and directory name of the directory junction you want to create.path\to\source_file
: Specify the path of the directory you want to create a symbolic link to.
Example output:
Directory junction created for path\to\link_file <<===>> path\to\source_file
Conclusion:
The mklink
command provides a convenient way to create symbolic links, hard links, and directory junctions in Windows. By understanding and using the different arguments, you can easily manage and access files and directories from different locations without making copies or using up additional disk space.