Clip Man


Daniel Einspanjer's journal

Data warehousing, ETL, BI, and general hackery

Previous Entry Share Next Entry
How To Use Hardlinks And Junctions In Windows
Clip Man
If you are working in Windows and you miss the simple usefulness of the link utility ln from the UNIX world, this post is for you.

I imagine all my geek friends already know about this, but I just typed this up for work and figured it wouldn't hurt to put it here too.

In the modern Windows platform (e.g. Windows XP or Windows 2000 or greater using NTFS), it is possible to create hardlinks of files and directories. The ability to create file hardlinks is built into XP/W2k with the program fsutil.exe.
fsutil hardlink create
Usage : fsutil hardlink create <new filename> <existing filename>
   Eg : fsutil hardlink create c:\foo.txt c:\bar.txt
Anything written to foo.txt will appear in bar.txt and vice versa. If one file is deleted, the other remains.
(Note that some editors have a backup feature that can interfere with hardlinks. e.g. vim. When you make changes to a file with an editor using the backup feature, the editor actually renames the existing file and then writes a completely new file with the updated contents. This will make your hardlink non-functional. The hardlink is still there and working fine, but it is now a hardlink to the backed up file and the changes you just made will not be written to the other hardlinks of that file. This behavior in vim can be fixed by setting backupcopy=yes)

Directory hardlinks in Windows are called Junctions (a special type of Reparse Point). Support for junctions is built into XP/W2k, but they don't have a utility such as fsutil that can create them installed by default.
(Note that deleting a junctioned folder in Windows Explorer is BAD. Explorer will traverse the folder deleting all files within that folder which will result in those files being deleted from the other folder! If you want to get rid of the junction, use the delete function of the junction or linkd utility.)
Two command line utilities that can create junctions are:
  • junction.exe -- Available from the Sysinternals Website
  • Create Usage: junction newdirname existingdirname
    Delete Usage: junction -d newdirname
    Show Target Usage: junction newdirname
  • linkd.exe -- Available from the Windows 2003 resource kit (but runs on XP too)
  • Create Usage: linkd newdirname existingdirname
    Delete Usage: linkd newdirname /D
    Show Target Usage: linkd newdirname

There is also a Windows Explorer shell extension that allows you to create hardlinks and junctions from within Explorer.
Hardlink Shell Extension

  • 1
Don't forget junction mounts which allow mounting without the stuipd drive letters. Windows is amazing, it only took it a 20+ years to figure out these great *nix features. Of course I am sure they claim credit for having even thought of the concepts...

Mountvol is nice enough, but I've never really had much use for it since it can only mount physical volumes. I want the ability to mount a share!

And then there is the opposite of Mountvol, subst. You give subst a drive letter and a path to a directory and it creates a virtual drive letter for that path.

At least you can use subst with a share...

Link to Junction download


  • 1

Log in