Symbolic links on Windows

One of the most useful things you'll find on any Linux/UNIX variant is the ability to create symbolic links. Rather than copying a file or directory you can create a pointer to it. It's very similar to a Shortcut in Windows, but much more powerful because a symlink behaves like a normal file.

Shortcuts vs links

For example, assume you use a program that will only store it's files in C:\MUSIC. If you wanted to move the MUSIC directory to a different drive, you couldn't. If you create a shortcut called MUSIC on the C:\ drive to D:\MUSIC that won't work because the shortcut is actually a file called "MUSIC.lnk"

You could create a symbolic link from C:\MUSIC to D:\MUSIC and the program would function as expected, allowing you to move the MUSIC directory to a new drive.

Symlinks on Windows

Support for symbolic links on Windows is all over the board. Out of the box on Windows 2000 and XP you can only create links to files, and you need a separate program for linking to directories. Vista provides a new utility, but you still need to specify if you are linking to a directory or file.

fsutil

fsutil is available on Windows 2000, XP and Vista. There's actually quite a bit you can do with it, but for creating links the syntax is:

fsutil hardlink create new_filename existing_filename

If you wanted to create a link to c:\secret\passwords.txt on your desktop you'd run the following command

fsutil hardlink create %userprofiledir%\desktop\passwords.txt c:\secret\passwords.txt

You must provide the filename of the link.

Junction

Junction was created Mark Russinovich, and allows you to create links (actually junctions) to directories. It runs on Win2k, XP and Vista. To create a junction the syntax is:

junction junction directory junction target

So to create a link (junction) to c:\lodo_conversations\ on your desktop you would run:

junction %userprofile%\desktop\lodo_conversations\ c:\lodo_conversations\

junction.exe must be in your PATH and you must provide the name of the link.

To delete a junction:

junction -d %userprofile%\desktop\lodo_conversations\

mklink

mklink was introduced with Vista and allows you to create links to both directories and files, with a switch.

To link to c:\lodo_conversations\ on your desktop you would run:

mklink /d %userprofile%\desktop\lodo_conversations\ c:\lodo_conversations\

To remove that link

rmdir %userprofile%\desktop\lodo_conversations\

To create a link to c:\secret\passwords.txt on your desktop you'd run the following command

mklink %userprofiledir%\desktop\passwords.txt c:\secret\passwords.txt

 

Tags: , , , ,

Leave a Reply


© 2007-2009, Corey Gilmore | Posts RSS Feed | Comments RSS Feed | Contact

 

The views expressed on these pages are mine alone and not those of any past or present employer. All information presented on this site was obtained lawfully and not through disclosure under the terms of an NDA.