Mklink - Create Symbolic Link in Windows
In Windows, you can create symbolic links (soft links) and hard links with the mklink
CMD command.
In the Microsoft Windows operating system, a symbolic link is a shortcut that makes a file or directory accessible from another location without making a separate copy.
mklink LinkName OriginalFile
Here's a simple example:
mklink C:\Users\user1\Desktop\sales c:\data\sales.docx
In the above example, we created a shortcut called sales on the user's desktop, which points to a word document called sales.docx in the c:\data
folder.
The name of the link can be anything. It doesn't have to be the same name as the original file.
We can create symbolic links to directories with /D
option:
mklink /d C:\Users\user1\Desktop\Downloads C:\Users\user1\Downloads
In the above example, we created a symbolic link to the Download folder on the Desktop.
Note that, to run the mklink
, you need to open the command prompt as administrator.
Hard Links
By default, the mklink
command creates symbolic links (soft links). If you want to create a hard link, you'll still need to use the /H
option. For example, the following command creates a hard link from link.txt to original.txt.
mklink /H link.txt original.txt
Unlike symbolic links, a hard link is not a shortcut. When you create a hard link to another file, both files are pointing to the same entry. Even if you deleted the original file, the link file is still accessible.
Notes
- Hard links only work on files. Soft links can link to both directories and files.
- You can't create a hard link to a file on a different partition.
- With symbolic links, removing the link does not affect the original file. However, the link won't work if the original file is removed, renamed, or moved.
- With hard links, there is no difference between the original file and the linked file. They are just two filenames that point to the same data. Even if one file was deleted, the other will still work.