Hard Links and Junctions in Windows

Did you know that [tag]NTFS[/tag] supports [tag]hard links[/tag]? 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 [tag]fsutil[/tag] 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 [tag]Windows API[/tag] – it is called a [tag]junction[/tag]. Unfortunately, Microsoft does not provide any out of the box support for creating Junctions. The official Microsoft tool you can use is [tag]linkd.exe[/tag] which is part of the Windows Server 2003 Resource Kit. As with many other [tag]Microsoft[/tag] 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:


If you don’t want to be bothered downloading the whole resource kit, you can just grab the 16Kb [tag]sysinternals[/tag] 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…

This entry was posted in sysadmin notes and tagged , . Bookmark the permalink.

7 Responses to Hard Links and Junctions in Windows

  1. user ISRAEL Opera Windows says:

    so what exactly is the purpose of these hard links?

    Reply  |  Quote
  2. Luke UNITED STATES Mozilla Firefox Windows says:

    There is no specific purpose – just the added flexibility. For example I sometimes like to create some sort of a link that would allow my clueless users to easily back up their Outlook.pst file. By default that file sits in C:\Documents and Settings\User Name\Local Settings\Application Data\Microsot\Outlook.

    Please keep in mind that I had a user delete NTLDR once because it was cluttering his C: drive. I don’t want that guy to have to enable showing hidden files, and navigating through that path.

    So I may just create a Email folder in My Documents which would be a junction to the actual Outlook data directory.

    Or better yet – I could simply create a hard link to Outlook.pst in My Documents and call it Emails.pst. Then I could show him how to back it up every night by dragging it onto his thumb-drive, or CD-R icon.

    And if he accidentally deletes, or overwrites the hard link, his email will still be working properly because the original file won’t be affected.

    That’s just one windows-centric scenario of the top of my head. If you take a look at linux or other POSIX compliant systems, hard and soft links are used all over the place.

    Reply  |  Quote
  3. Fr3d UNITED KINGDOM Mozilla Firefox Windows says:

    You can use it like symlinks on linux :D

    I have a 5GB C:\ drive… but my C:\Documents and Settings\Fr3d folder is 2.98GB, and my Program Files is 4.78GB. How does this all fit on the same drive as windows, on a 5GB partition, where 2GB of that is free space :P

    The answer? I mapped the Docs & Settings folder and the program files folder to another larger drive :)

    And yes, it’s good for backups too as Luke mentioned – Some shitty application decided to erase my entire C:\Documents and Settings\Fr3d\ folder after I uninstalled it… So all I had to do was to use linkd again to re-map the folder – no files lost :D

    Reply  |  Quote
  4. Luke UNITED STATES Mozilla Firefox Windows says:

    I’m actually seriously thinking to make this a standard setup for company laptops:

    1. create 2 partitions – one for OS, and one for Data
    2. move the %userprofile% to the data partition
    3. create junction to new location of %userprofile%

    This way if Windows goes to shit, I can simply reinstall OS and recreate the junction as needed.

    Reply  |  Quote
  5. Jakob DENMARK Mozilla Firefox Windows says:

    Correction: Junctions are not hardlinks, they are symlinks (in the UNIX sense). Thus Windows incoherently supports hardlinks for regular files and symlinks for directories, but neither symlinks for files nor hardlinks for directories.

    For the Windows equivalent of the UNIX /dev /mnt and /proc folders, NT has always supported full symlink functionality, plus device node hardlinks, however none of that directory structure is visible outside lowest level development and debugging tools such as objdir.exe .

    P.S. File hardlinks are not new in Windows 2000, only the documentation and user interface.

    Reply  |  Quote
  6. mrunalini SWITZERLAND Internet Explorer Windows says:

    linkd works on xp prof and not on xp. – mrunalini

    Reply  |  Quote
  7. Luke Maciak UNITED STATES Mozilla Firefox Ubuntu Linux Terminalist says:

    [quote comment=”9996″]linkd works on xp prof and not on xp. – mrunalini[/quote]

    True. Although if you do have a copy of XP Pro you can easily just take linkd.exe and copy it to your XP Home machine and it will work.

    Reply  |  Quote

Leave a Reply

Your email address will not be published. Required fields are marked *