Symlink – also called a Symbolic link or Soft link – could be one of the most hidden concepts for computer users. However, that does not take away its importance and the benefits it offers. How about accessing folders and files from different folders without maintaining duplicate copies? Yes, that’s the power of Symlink.
What is a Symlink or Symbolic Link
Symlink are shortcut files that refer to a physical file or folder located elsewhere. Symlinks act as virtual files or folders, which can be used to link with individual files or folders, making them appear as if they are stored in the folder with Symlinks, even though the Symlinks is only pointing to their real location.
Don’t confuse Symlink as just shortcuts
It is important to understand that Symlinks are more than just Shortcuts that as a Windows user you already know. A shortcut file simply points to the desired file while Symlink will make it look like the linked file is actually there. Once you click on the Symlink, you will be directed to the actual file location.
Structure of a Symlink
A Symlink is build of a text string that is automatically interpreted and followed by the operating system as a path to another file or directory. This other file or directory is called the “target“.
Symlink exists independent of its target. If a Symlink is deleted, its target remains unaffected. If a Symlink points to a target, and sometime later that target is moved, renamed or deleted, the symbolic link is not automatically updated or deleted, but continues to exist and still points to the old target. However, such Symlinks pointing to moved or non-existing targets are sometimes called broken, orphaned, dead, or dangling.
Benefits of Symlinks
- Symlinks consume very little space and are very fast to create. You save a lot of hard disk space with Symlinks
- Contrary to hard links, symlinks can link to files across file systems. Remember, if you delete the original file, the hard link still keeps it alive, a Symlink doesn’t
- Symlinks maintain the folder structure in which the Symlinks are contained. For example, let’s say there is a txt file is contained in the “Utility” folder which is located at /D/Myfolder/Utility/windowsclub.txt . Now if a Symlink for the Utility folder was created in the Dropbox folder, and you went searching for windowsclub.txt within the Dropbox folder, the file path would read /D/Myfolder/Utility/windowsclub.txt rather than transforming back to the original/actual file path.
- Using Symlinks you can store your media files Music/Videos on another hard drive, but make them show up in your standard Music/Videos folders, so they’ll be detected by your media programs
- Developers often replace duplicate copies of shared files/folders with symlinks referencing physical files/folders. Replacing redundant copies of files can save a great deal of physical disk space, and significantly reduce the time taken to copy/backup/deploy/clone projects.
Significance of Symlinks in today’s development world
As Yosef Durr, Lead Senior Program Manager, Microsoft mentions on Windows Blog,
Many popular development tools like git and package managers like npm recognize and persist symlinks when creating repos or packages, respectively. When those repos or packages are then restored elsewhere, the symlinks are also restored, ensuring disk space (and the user’s time) isn’t wasted. Git, for example, along with sites like GitHub, has become the main go-to-source code management tool used by most developers today.
The use of package managers in modern development has also exploded in recent years. For example, node package manager (npm) served ~400 million installs in the week of July 1st, 2015, but served more than 1.2 billion installs just one year later – a 3x increase in just one year! In late June 2016, npm served more than 1.7 billion node packages in just seven days!
Symlinks in Windows 11/10
Although Symlinks have distinct advantages, it was primarily utilized by UNIX-compatible operating systems like Linux, FreeBSD, OSX, etc., where symlinks can be created without restrictions. For Windows users, although Symlinks was made available starting from Windows Vista, it was difficult and tedious to create.
Because of Windows Vista’s security requirements, users needed local admin rights and, importantly, had to run mlink in a command-line console elevated as administrator to create/modify Symlinks. This latter restriction resulted in Symlinks being infrequently used by most Windows developers and caused many modern cross-platform development tools to work less efficiently and reliably on Windows.
However, now on Windows 10 or Windows 11, once a user with admin rights enables Developer Mode, anybody on the PC can run the mklink command without elevating a command-line console.
How do you create Symlinks
Symlinks can be created either using the mklink command or the CreateSymbolicLink API.
You can make Symlinks or Symbolic Links in Windows, using the command-line utility: mklink. The syntax is as follows:
MKLINK [[ /D] | [ /H] | [ /J]] Link Target
Where –
- /D: creates a symbolic directory link or soft link
- /H: creates a hard link to a file instead of a symbolic link
- /J: creates a Directory Junction link
Target: Specifies the path that the new link refers to.
Now for example, if you want d:\Data to be made available from the folder D:\Maindata use:
C:\mklink /D D:\Maindata D:\Data
Now, if you now look in D:\Maindata, you will see whatever files that were there in the other directory. It has always been there in Linux, but this feature has now been made available in Vista too.
Using mklink command
While using mklink command, use the below syntax:
“mklink /prefix link_path file/folder_path”
Note: mklink can create several types of links. Below are the types-
- /D Creates a directory symbolic link. The default is a file symbolic link.
- /H Creates a hard link instead of a symbolic link.
- /J Creates a Directory Junction.
For instance, I created a directory junction of my Music folder to my desktop. Refer the below screenshot-
So when I click on the Symlink, it looks that my music files are stored in C:\Users\<username>\Desktop\Music although it is originally present in C:\Users\<username>\Music.
Remember, if your path has any spaces in it, then you need to place quotes around it.
Using CreateSymbolicLink
To enable the new behavior when using the CreateSymbolicLink API, there is an additional dwFlags option you will need to set the value as:
SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
0x2
So by choosing the above value, you have specified the flag to allow the creation of symbolic links when the process is not elevated.
To read more on how to create Symlink using CreateSymbolicLink API, visit windows.com.
Read: Create Hardlinks, Symbolic Links, Junctions, Volume Mountpoints.
Conclusion
It can be said that Symlinks are more useful than mere shortcuts however they are a bit difficult to create. An average PC user may still find it a little scary to create. What’s more, even today, many users struggle to understand the concept of shortcuts well and hence may struggle to differentiate Symlinks and understand its use.
Having said that, it is almost a guarantee that you will find it easy to change whatever settings you can so that a program is pointing to the right directory, and you don’t really create a Symlink, but knowing how to set up and use a good Symlink can be really handy.