Hard Links and Junctions in Windows
Wednesday, September 6th, 2006Did you know that NTFS supports hard links? This is an interesting tidbit about windows that not many people know about. But let me quickly explain what is a hard link to the clueless windows people.
Imagine the following file C:\temp\a.txt. Where is that file really located? If you said the temp folder
then you are only partially right. Yes, the logical location of that file is in that folder. But the real physical location of this file can probably be best expressed in terms of track and sector it occupies on the hard drive. Your file system (NTFS for example) maps the physical location, to the logical location for your convenience. This is usually accomplished via some sort of lookup table (FAT for example).
So what happens when two or more entries in that table point to the same physical file? Nothing spectacular really - you simply get several logical pointers (or hard links) to the file that behave exactly the same. If you change permissions on one of them, all other pointers will be affected. When you delete a hard link, you simply remove one entry from the table. The physical file is only deleted when all the links are gone.
Unix users have been utilizing this nifty functionality for ages now, but Windows crowd only got it recently in the NTFS file system. So how do you make a hard link? You use the fsutil command:
fsutil hardlink create LINK TARGET
In the above LINK is the name of the created hard link, and TARGET is the file you are linking to.
There is one disadvantage in this method. You can’t create hard links to directories. But once again, this functionality is present in Windows API - it is called a junction. Unfortunately, Microsoft does not provide any out of the box support for creating Junctions. The official Microsoft tool you can use is linkd.exe which is part of the Windows Server 2003 Resource Kit. As with many other Microsoft utilities, this one will work just fine when used on XP box - so don’t be scared by the server part. The usage is very simple:
linkd LINK TARGET
If you don’t want to be bothered downloading the whole resource kit, you can just grab the 16Kb sysinternals Junction app. It has the same functionality as linkd, but it is free and comes with a complete source code. The syntax is almost identical:
junction LINK TARGET
Unlike hardlinks, junctions can be easily identified by using the dir command. They will show up on the list marked as <JUNCTION> rather than <DIR>:
09/06/2006 10:50 PM <DIR> TEMP
09/06/2006 10:49 PM <JUNCTION> test
09/06/2006 10:53 PM <JUNCTION> test2
09/22/2005 10:31 PM <DIR> Themes
This leaves out soft links, which for now still seem to be exclusive tho the unix/linux world. Windows does implement a similar functionality with it’s shortcuts. Unfortunately, shortcuts can’t be used on the command line which makes them only marginally useful. Perhaps in a few years Microsoft will be able to figure out that one too…
