What is a Symbolic Link?
Actually, we’ve seen and used them in Windows for ages. We simply have not used the term. In Windows-speak, they’re called Shortcuts. We’ve seen them on the Windows Desktop and in folders since the early days of Windows.
But, Windows 7 and Windows 8 will also create a symbolic link within the DOS Shell/Command Shell. These symbolic links can be used from either the command shell or within the Windows graphical user interface (GUI).
A symbolic link appears to be a file or directory- but it really is simply a link, or shortcut with a different name, that automatically redirects any attempt to access the file by the symbolic name to the true file.
Think of it as a nickname. Say you create a folder called goodfiles. Then, you create a symbolic link to the goodfiles directory and call it testfiles. Testfiles will have a directory folder icon, but with the little arrow showing that it is a Shortcut.
When you try to open the testfiles directory, Windows will actually show you that you had opened testfiles, but it would actually show you the contents of goodfiles.
As you can imagine, this setup creates some danger if you are trying to clean up your hard drive. The danger is thinking that you have duplicate files. If you delete the symbolic link, you’re ok. But, if you delete the original, the files are gone. Fortunately, if you delete from the Windows GUI, you see icons with little Shortcut arrows, so you can easily tell which is the shortcut/symbolic link and which is the original.
Symbolic links are useful to make easily accessed directories and files. Windows uses them for shortcuts and for Libraries (Windows 7 and later). They can make your life easier, but you really don’t need to know how to create them.
From the command line, you can make a symbolic link by using the mklink command. The command shell must be running as Administrator.
The mklink command’s syntax (the way you write the command) is:
mklink [[/d] | [/h] | [/j]] <Link> <Target>
where the brackets indicate those particular parameters are optional.
The command’s parameters are:
- /d Creates a directory symbolic link. By default, mklink creates a file symbolic link.
- /h Creates a hard link instead of a symbolic link.
- /j Creates a Directory Junction.
- <Link> Specifies the name of the symbolic link that is being created.
- <Target> Specifies the path (relative or absolute) that the new symbolic link refers to.
- /? Displays help at the command prompt.
A command to create a MyDocs symbolic link in the root (topmost) directory of C:\ for userID terry’s My Documents folder on the C: drive would be:
mklink /d C:\MyDocs C:\users\terry\Documents
The C: is optional, if your current active directory is on C: and if you have not moved the user’s My Documents folder somewhere else.
Note that you can change the symbolic link to point to another directory or file. This concept is very important, as there is no command (that I have found) to remove a symbolic link without deleting all of its contents at the same time!
So, if you want to play around with this and see how it works, I suggest creating a disposable directory. Or, at least, when you want to delete the symbolic link, first create a directory to be subsequently deleted, then mklink to that new directory, and then delete the symbolic link.