A symlink is a special type of file that contains a pointer to another file or directory on the filesystem. Historically, symlinks date back to the early 80′s where they were introduced on the Unix platform. They were designed to operate transparently to the user, where all programs access the symlink like a regular file, reading and writing as appropriate; unlike the commonly known Windows version, the Shortcut. Windows Shortcuts (.lnk) are somewhat like symlinks, allowing the user to jump around the filesystem in Windows Explorer, but the paradigm quickly breaks down when other applications attempt to use Shortcut files – they are just regular files at that point.

It surprised me to learn that, starting with Windows Vista and Windows Server 2008, the NTFS filesystem

supports the notion of symlink through the command line tool mklink. With mklink, a symbolic link can point to a file, directory, or even a remote network share. Where would this be handy?

  • Space saving - Harddrive space is ever expanding, as is the size and amount of content we store on them. Everyone understands that adding a second drive to your computer will allow you to move files off of the initial drive to free up space. But what about programs/config files/registry entries that expect to find files in their original location? A symlink to the new location is a great way to address this.
  • Convenience – I come from Unix and used DOS before the days of the Windows GUI, so I’m always drawn to the  command line. Do the spaces in directory names drive you nuts? Why not symlink c:\program files\ to c:\progs\ ?

How does it work? From a blog post at MSDN, open a command prompt in Vista under Administrator rights.

C:\test>mklink
Creates a symbolic link.

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.

C:\test>mklink foo c:\Windows\system32\notepad.exe
symbolic link created for foo <<===>> c:\Windows\system32\notepad.exe

C:\test>dir
Volume in drive C has no label.
Volume Serial Number is 2211-7428

Directory of C:\test

04/14/2006  11:24 AM    <DIR>          .
04/14/2006  11:24 AM    <DIR>          ..
04/14/2006  11:24 AM    <SYMLINK>      foo [c:\Windows\system32\notepad.exe]
1 File(s)              0 bytes
2 Dir(s)  69,238,722,560 bytes free

C:\test>mklink /d bar c:\windows
symbolic link created for bar <<===>> c:\windows

C:\test>dir
Volume in drive C has no label.
Volume Serial Number is 2211-7428

Directory of C:\test

04/14/2006  11:24 AM    <DIR>          .
04/14/2006  11:24 AM    <DIR>          ..
04/14/2006  11:24 AM    <SYMLINKD>     bar [c:\windows]
04/14/2006  11:24 AM    <SYMLINK>      foo [c:\Windows\system32\notepad.exe]
1 File(s)              0 bytes
3 Dir(s)  69,238,722,560 bytes free

C:\test>del foo

C:\test>dir
Volume in drive C has no label.
Volume Serial Number is 2211-7428

Directory of C:\test

04/14/2006  11:24 AM    <DIR>          .
04/14/2006  11:24 AM    <DIR>          ..
04/14/2006  11:24 AM    <SYMLINKD>     bar [c:\windows]
0 File(s)              0 bytes
3 Dir(s)  69,238,722,560 bytes free

C:\test>rd bar

C:\test>dir
Volume in drive C has no label.
Volume Serial Number is 2211-7428

Directory of C:\test

04/14/2006  11:24 AM    <DIR>          .
04/14/2006  11:24 AM    <DIR>          ..
0 File(s)              0 bytes
2 Dir(s)  69,238,722,560 bytes free

Death to Shortcuts!