Symbolic links (symlinks) in Windows 7 or Vista
One feature that was notoriously absent from Windows until recently was the ability to create symbolic links. A symbolic link is merely a small piece of code that is put in some physical location (DIR1) which tells the operating system to move somewhere else (DIR2) when software attempts to access DIR1. I use this feature regularly in OS X or other Linux distributions, mainly as a way to sync files through Dropbox without having to messily move where my software stores my files, although I also like shortening the absolute path to various directories that look like they were named by a scientist having too much fun with alphabet magnets on the fridge.
In Unix speak, this is quite easy– open up a terminal window and type:
ln -s /PATHTO/DIR2/ /PATHTO/DIR1/
This has finally been added in Windows Vista and Windows 7, but remains somewhat sloppy and confusing. Here is a very brief reference for those trying to do a similar thing in Windows, with emphasis on the flags.
mklink \PATHTO\DIR1\file.txt \PATHTO\DIR2\file.txt
You will be returned with:
symbolic link created for \PATHTO\DIR1\file.txt <<===>> /PATHTO\DIR2\file.txt
This is the most basic entry for creating a symlink between files. The one issues? This creates a soft link (essentially the same as creating a shortcut in Windows Explorer). If I am doing something like syncing my Keepass database, I will actually break this symlink because the software will not be forwarded to the file on a basic level– it will merely sit in the folder and go “where the hell is my database?” and either create a blank one or give some other error message.
To solve this problem we need to use the /H flag which tells Windows to create a hard link. A hard link physically points to the secondary location. You’ll want to use this option if you are trying to trick an application into looking for a different directory. For me, most of the time.
mklink /H \PATHTO\DIR1\file.txt \PATHTO\DIR2\file.txt
Now that’s how to symlink files– you need additional flags for directories. /D tells Windows that you are creating directory links, not file links. If you only use /D you will create a soft link. /J instructs the OS to create a “junction” which is (functionally) the equivalent of the hard links discussed above. In most cases I find myself using /D and /J before the link and target.
mklink /D /J \PATHTO\DIR1\ \PATHTO\DIR2\
Also remember, if you type “mklnk” at the command prompt, you will be returned with basic instructions for using the functions described above:
C:\Users\balloflightning>mklink
MKLINK [[/D] | [/H] | [/J]] Link Target
/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.
Link specifies the new symbolic link name.
Target specifies the path (relative or absolute) that the new link
refers to.
Hopefully the above helps demystify the procedure a bit, however.