Tuesday, March 4, 2008

Creating a Junction (symlink) in Windows

Ever wonder how to create the windows equivalent of *nix's "symbolic links"? For those unfamiliar with the symlink, they are files whose sole purpose in life is to point to another location. Think of them as the operating system's equivalent of a shortcut; they work in the same manner, but symlinks are visible to the file system directly, whereas shortcuts are only available within the Windows GUI.
You can create these yourself with the following program:
http://www.sysinternals.com/utilities/junction.html
First, make sure the junction.exe file is somewhere in your path; that could be as simple as moving the executable to the directory you wish to create the junction in, or if you feel you'll be using this utility more often than just this one time, you can always put it into c:\windows (c:\winnt for Windows 2000 users).
Let's say we wanted to create a junction in:
D:\Inetpub\vhosts\samplewebsite.com\httpdocs
That pointed to:
D:\Inetpub\common\sharedimages
To accomplish this, run the following command:
junction D:\Inetpub\vhosts\samplewebsite.com\sharedimages D:\Inetpub\common\sharedimages
You'll see something like the following output:
Junction v1.03 - Win2K junction creator and reparse point viewerCopyright (C) 2000-2002 Mark RussinovichSystems Internals - http://www.sysinternals.com
Created: D:\Inetpub\vhosts\samplewebsite.com\sharedimages Targetted at: D:\Inetpub\common\sharedimages
Now I have a symlinked folder in my publicly-accessible directory structure that uses the files in the target directory. This will come in handy if you have several different sites that wish to use the same resources.
This is what the new link will look like when issuing the “dir” command in that new directory:
11/26/2005 12:55 AM sharedimages
As you can see, it labels it as a junction, and not as a directory, but they act the same. In Windows Explorer, you won’t be able to tell the difference between that and a directory.
NOTE The only exception is that there is a known issue in all versions of Windows that will prevent you from moving a file across a junction point into the recycle bin. If, when trying to delete a file, you see an error such as this:
Cannot delete Foldername: Access is denied. The source file may be in use.
that is what is going on. You will need to either bypass the recycle bin (shift+del), or navigate to the actual (non-junctioned) location of the file and delete it through windows explorer from there.
http://support.microsoft.com/kb/243514/EN-US/

No comments: