SEARCH

How-To Geek

Using Symlinks in Windows Vista

One of the long-awaited features in Windows Vista was the ability to use symbolic links, the way you can in linux. Sadly, they don’t work quite as well as they could, but it’s a big upgrade from prior versions, and has solved a number of problems for me already.

Using the mklink Command

The command that you need to use is mklink, which you’ll use from the command line. Just type it on the command line to see the options:

C:\Users\geek>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.

For instance, if you wanted to make the folder C:\Users\Geek\TestFolder available from C:\TestFolder as well, you could use the following command.

C:\mklink /D C:\TestFolder C:\Users\Geek\TestFolder
symbolic link created for C:\TestFolder <<===>> C:\Users\Geek\TestFolder

Now if you look in C:\TestFolder directory, you’ll see whatever files were in the other directory.

Understanding the Options.

MKLINK link target

Using the command without any extra options creates a soft link to a file.

/D creates a symbolic link, or a soft link.

This essentially acts like a shortcut to a folder in prior versions of Windows, except you don’t have to use an actual shortcut.

/H creates a hard link, which points directly to the file.

This option can’t be used for folders directly for some reason, you’ll have to use the next option.

/J creates a “Directory Junction”

A Directory Junction is actually just a hard link to a directory. This is a feature that existed prior to Vista as well. If you are trying to symlink to a directory using a hard link, then you should use this option.

Understanding Hard vs Soft Links

Hard Link

A hard link directly points to the file, and acts to the operating system as if it is the file itself. You’ll want to use this option the majority of the time if you are trying to fake an application’s directory.

Soft Link

A soft link is essentially a shortcut to a file or folder – if you are using Windows explorer, you’ll be redirected to the directory if you double-click on a shortcut, it won’t pretend its part of the filesystem. You can still directly reference or open a file with the symlinked path, and it mostly works.

Using Symlinks from a Network Share

One of the things that’s been extensively discussed is that you cannot use the Vista symlinks from another operating system (not surprising), but you cannot use them from a network share either. This is troublesome if you expect to use this feature on a web server or a file server.

Deleting Symlinks

To delete a symlink, you can just delete the link. Just make sure you don’t delete the original file.

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 01/25/07

Comments (47)

  1. Mike Scott

    Excellent post, that really laid it out for me. I figured the gist of what they were, but didn’t fully appreciate the differences. Thanks.

  2. Alexandre

    Good post, but I have something to add to network shares: with Windows Longhorn Server you will be able to create link to network shares.

    Cheers
    Alexandre

  3. The Geek

    Alexandre,

    That’s very interesting, I hadn’t heard that. I’m going to have to look into that more.

  4. brett

    Ya, but System Restore will NOT work properly. Temporary user accounts appear, and all user settings seem to be lost. Argh! :(

  5. Bernard Kerckenaere

    To be a bit more correct:

    * shortcut: on the operating system level (to applications who wish to read/write the link, it’s just a meaningless file)

    * soft link (or symbolic link): like a shortcut, but on the filesystem level (applications reading/writing the link, will actually read/write the file linked to)
    -> unfortunately this only works with folders on Windows, not with files (which is the reason I’m searching around for information at the moment, I need file symlinks, like in *nix.)
    -> this will work across partitions, or drives

    * hard link: only for files, what happens is that there are multiple file entries that point to the same physical data, when you delete one entry, the other will still work, the data won’t be gone until all entries are deleted (if with a soft link you delete the original directory, the link won’t work anymore!)
    -> you can obviously only create hard links to a file on the same partition

  6. Knight

    I recommend this app: http://schinagl.priv.at/nt/hardlinkshellext/hardlinkshellext.html

    Easy Drag and Drop creation of hard links, symbolic links, etc for Vista and XP.

  7. Blue

    Concept nicely explained and simplified. Thanks.

    And now for a good laugh: try creating a link, within a folder, to itself, as in…

    1. We have a folder called C:\TestFolder
    2. Within TestFolder, we create a link:
    mklink /D myself C:\TestFolder
    3. We now click on the new link and start laughing…. or crying ( your choice! )

    I just wonder: Should it be considered a bug ?

  8. Steve Steele

    Could this be used to ‘move’ the C:\Users folder to D:\Users (or another drive) by making C:\Users a hard link to D:\Users ???

    I’m thinking that C:\Users would still exist but would link to and store files in D:\Users

    And ‘safer’ then the rather messy registry hack (which I’ve heard has some issues and limitations)

  9. Ryan Beesey

    Yes Steve, you can do that. Actually Vista does something very similar when it installs to give legacy applications a psuedo Pre-Vista environment. Open a command prompt and cd to the root. Type dir. You’ll see Users as expected, but notice that Documents and Settings is no where to be found. Try to cd to Docu[tab]… it comes right up. Press ESC and now type dir /a. You’ll see that Documents and Settings has been made into a Junction. Try exploring your User directory, you’ll be surprised at how many places Microsoft has used Junctions and how few people know.

  10. Frank

    Hi, I did not get the point about using Hardlinks on network shares.

    What I just tried:
    1) created a network share BACKUP1 on E:\BACKUP1 (on vista)
    2) cmd > e: > cd backup1 > mklink /J TEST1 W:\Music (on vista)
    3) used mount -t smbfs **someoptions** //192.168.2.2/BACKUP1 /tmp/testsmb on my linux box
    4) cd /tmp/testsmb/TEST1/**somedeeperstructure**/ (on linux box)
    5) less a.txt (on linux box)

    Result: worx perfectly.

    BTW: no SP1 applied to Vista.
    Cheers, Frank

  11. Style14

    I’m having an error with this:

    ‘C:\mklink’ is not recognized as an internal or external command, operable program or batch file.

    Running Vista Ultimate with SP1.

    Any help?

  12. Jack

    Thanks to Bernard for pointing out that Windows symlinks != shortcuts

  13. Mark

    I’m familiar with the concept of hard and soft (symbolic) links from Unix/Linux, and it’s good to see Microsoft attempting to add such a useful feature to Windows.

    It’s also the case in Unix/Linux that you can create soft links to a folder, but not a hard link — and there’s a logical reason for it. Whenever you create a new file or folder, it is automatically hard-linked to, anyway, from its directory entry. If you create additional hard links to a file, they’re additional directory entries that are just as “valid” as the original directory entry that was formed with the file’s creation. That’s why, as Bernard points out, a file pointed to by multiple hard links doesn’t get finally removed until the last hard link to it is deleted. A soft link, on the other hand, is not as “valid” as a hard link. You can create a soft link that points to a non-existent file or folder. You could remove a file/folder pointed to by a soft link, and the existence of the soft link will not prevent it happening. After the removal of the file/folder pointed to by a soft link, the latter will remain, uselessly pointing into fresh air.

    The logical problem with multiple hard links to a folder is that, in a hierarchical structure, there can only be one path upward from a folder (as when you do a “cd ..” command). If there are multiple hard links to a folder, then which one of them would be the appropriate path to follow upwards? After all, all hard links to a file/folder are equally “valid”.

    Again, as Bernard remarks, hard linking can only be done to a file within the same partition, as the directory entry will refer to what it points to by an internally-used number (called an “inode” in Unix/Linux, but I don’t know what the Windows term is). Such internal numbering will be unique *within* a partition, but can’t be unique *across* multiple partitions.

  14. Mark

    Meant to add …

    Soft links don’t point to an “inode” or similar. That’s why they’re not “hard”. They simply include a path to whatever they’re meant to point to, and hope for the best. Because they don’t rely on a unique “inode” number (or similar in Windows), there is no logical impediment to soft links pointing to files/folders in other partitions.

  15. Peter

    Has anyone had any success circumventing the windows update failures with symlinks?
    I have my program files, users, etc moved onto different partitions, but occasional windows updates fail because of the file copy mechanism they use (which doesn’t support symlinks) Occurs with the recent KB952287 update (if people want to try it), and the Hold Em poker game in Ultimate.

    Apart from that, the symlinks have been excellent

  16. jdackle

    Question:
    Will Windows symlinks (NOT shortcuts) be recognised as such by Linux with the NTFS-3G driver (or any other driver)?

    E.g.:
    Vista symlink: C:\Testfolder -> C:\Users\me\Testfolder
    In Linux: cd /mnt/vista/Testfolder = cd /mnt/vista/Users/me/Testfolder ?

  17. Peter

    I can almost guarantee you that they wont. (Not natively), across a network I think they do, because it’s still Vista serving them, but I vaguely remember reading that they work completely differently internally to linux ones. Sorry :D

  18. jd2066

    I have a dual boot with Linux and the symlinks are not recognized.
    It seems like it should be possible for the NTFS-3G driver to do so. I can only guess it’s ether not a priority or is a hard thing to do.
    Also NTFS-3G lets you store Linux symlinks on an NTFS partition but Windows Vista doesn’t recognize them.

  19. jdackle

    I asked this same question on the NTFS-3G forum and the answer by its lead developper was:
    “Only Interix style Windows symlinks are supported at the moment (Windows Services for Unix) and junction support is in the work.”

    Windows Services for Unix is not available for Vista mainly because a big part of it already ships with Vista, Interix included (according to the Wikipedia). :)

  20. Fair DeLune

    On Vista Home Premium.

    As soon as I discovered symlinks I wanted to shift my Documents folder to D: So I followed some tutorial and it moved just fine. Except some program was still using one file in the original so the original Documents folder remained undeleted. So I had two entries in Explorer and their contents are different.

    I renamed one of them to “Documents on C” emptied that folder. So I should be able to delete that pesky original Documents (renamed). But no. Deleting that deletes the D: version, EEEK!! No worries I got it back from the bin.

    I will open a command window and make the necessary changes. I just wanted to warn folks symlinks could have lost me a lot of data.

    Thanks for the info.

  21. abhishek

    hi Blue,
    Nice finding , well one more thing which is definetly going to be considered as a bug.
    if you create a symlink (or so called soft link) , of a directory inside that directory itself .Then we won’t be able to delete it also !!!!!
    OoPPs!!!!!!!

  22. Rene Lindsay

    Is there any way of making executable folders in Windows or Linux?
    I have always hated symlinks and shortcuts, ever since I switched from RiscOS to Windows 95.
    Executable folders in RiscOS was a far more elegant solution than the Windows Shortcuts, and Im looking for a way to bring this great feature to Windows.

    For those who dont know, an executable folder is an application folder which also has the features of a shortcut, all in one. This lets you treat a complex application as if it was a stand-alone executable.
    Let me explain:
    A simple application such as notepad.exe is completely portable and atomic. It consists of a single relocatable customised icon which can be placed on your desktop, or anywhere and just runs when clicked. There’s no need for an installer/uninstaller, since you just copy it to make more, or press delete to uninstall.

    The problems start if your application requires some external data files. Now you need a folder to keep your exe and all its data files together. Of course its still messy to have to open the folder to locate and run the exe among all its data files. So, you create a shortcut link to the exe, so you can run it without having to open its folder. This is where it all goes wrong: Now you have two seperate entities, the shortcut, and the folder. If only the folder could also act as the shortcut to the exe within, such as RiscOS does, then you wouldnt need to split them up.
    By convention, the shortcut gets placed on your desktop or start menu, and the folder gets hidden in C:\Program Files. This causes more problems than it solves. The folder can no longer be moved, without breaking the shortcut, and now you need an installer/uninstaller to manage your app.
    And the shortcut concept is not very intuitive for new windows users. How often have you seen new users delete a shortcut to uninstall an app, not knowing that the real app is still hidden in Program Files? Also, if they copy the shortcut to another computer, it will no longer work.

    An executable folder solves all these problems. It groups the app, its data files and its shortcut together in a single atomic icon, which wont break if you copy it to a new location, and can be uninstalled simply by pressing delete. Its so much simpler to use and more intuitive for beginners.
    The executable folder takes on the icon of the exe, and runs the app when clicked. Or, you can still open it as a folder, by selecting “Explore” from its right-click context menu.

    So far I have found 2 windows features which ALMOST reproduces this functionality:
    You can place an Autorun.inf file on a cd, to specify what icon to display on the CD drive icon, and what exe to run when its clicked. Unfortunately this only works in the root of a filing system, such as a cd or a Usb key, and it wont work on application folders on your desktop.

    I got even better results from junction points:
    I created a folder, and set its system bit with: chmod +s MyFolder
    I then placed notepad.exe, and a shortcut to notepad.exe in it, and called the shortcut “Target”
    Finally I create a Desktop.ini file containing the following text:
    [.ShellClassInfo]
    CLSID={0AFACED1-E828-11D1-9187-B532F1E9575D}

    Suddenly MyFolder takes on the notepad icon, and its properties shows that it is now a sortcut to the Notepad.exe within. The only problem is, if you click the shortcut/folder, nothing happens. This type of Symlink only works if “Target” was linked to a folder, and not a file. Maybe its possible to make it work if I use the correct GUID in desktop.ini, or change some regitry settings?
    Or maybe I need to write the COM class myself, but then I need to know more about exactly how the folder shortcut class works.
    Does anyone know were I can find more info on this?
    Any help would be greatly appreciated.

  23. Chris

    Here’s my question:

    When I open up properties on a folder containing hard-links, the file size matches the size of the linked files. Does this mean that the OS believes that much extra hard-disk space is missing? i.e. 2x the original file size?

  24. Peter

    No it doesn’t, otherwise my system would be screwed.

  25. Ryan

    Thank you so much!!!!! I recently had to work with Smarty templates in a web design project and every time I went into the templates directory, the images would be broken. Symbolic links and your post saved the day!

  26. Phil

    Hi,

    I tried doing that with Windows 7 and for some reason the following error keeps appearing:
    “Cannot create a file when that file already exists”.
    Does anyone know why?

    Regards.

  27. R

    @Bernard Kerckenaere

    Of course you can create softlinks to files, simply remove the /D. mklink is to create softlinks to files by default.
    e.g.:
    C:\mklink C:\TestFolder\File.exe C:\Users\Geek\TestFolder\File.exe

  28. Justin

    Is it possible to make a link a directory on another disk, but not have the link report the file size? I have moved the User, Program Files, and ProgramData folders to an SD card on my EeePC which worked. But they still are considered eating up the SSD space that the Win7 is installed on.

  29. Steve

    Nice one! This solved a problem I’ve had for quite a while!

  30. Artem Russakovskii

    Thanks, finally figured out how those new symlinks without .lnk were created after upgrading from XP to 7.

  31. Recon

    Excellent post. I just reinstalled Sandboxie now that it supports 64 bit and I was working on getting my browser to download to my normal Downloads folder. Good ol symbolic links did it again. I knew they were there, but I just needed a guide since I forgot how to use em. Short and to the point. Thanks.

  32. Richard

    One point: Junctions are NOT hard links for directories. A “hard link” for a file is a second name for the contents of a file. Delete the first name and the contents still remain accessible using the second name. That is, given a file abc.txt with contents “This is a test”, doing “mklink test.txt abc.txt” and then deleting abc.txt will NOT delete the actual file.It is still accessible via test.txt, and will still show “This is a test”.

    However, there is no way to create a hard link for a directory. For both junction points and sym links (/j and /d options) if you delete the original directory, it is gone. Both junction points and sym links “break” because the directory is gone. Try this:
    mkdir a
    mklink /d b a
    mklink /j c a
    rmdir a
    At this point, both cd b and and cd c will fail because a is gone.

    The difference between a junction point and a sym link for a directory has to do with the kind of thing that can be referred to. Junction points are “more expensive”, computationally, than a sym link, but provide more flexibility. If you’re just trying to create a second path to a directory on the same hard drive, use a sym link.

  33. Khue

    Hi all,
    Is there anyway to get it work on webserver?
    I have a web site displaying an image whose source is it softlink on the server machine. When deploying to the server, the image won’t display.
    I badly need it to work over webserver, Is there any software tool that can do it?

  34. Heliac

    LOL @ Blue… Logically it makes sense; albeit amusing, I don’t think it’s a bug :)

  35. Tony

    Thanks for sure, for letting me know Symbolic Link actually exists in windows. I had no idea and it solved the problem I was having with one of my programs instantly!!

  36. Francesco

    hi I have mklinked a folder to dropbox. Now the problem is that if I try to delete a subfolder or a file then it will remove the file or folder from the original location as well. Is that normal? Is so how can I just remove the linked folder without consequences for the original?
    Thanks

  37. Ran

    Hello,

    I’m trying to get rid of my old environment variables, that I use via the Run command prompt to open folders in my varying external drives.

    Frankly, I’m just sick of having to hit the % button every time I want to open one of those folders.

    Is there a way to create a symlink or somesuch so that I can access the subfolders via the Run command prompt?

    Say I linked (via either /d or /j, I haven’t noticed a difference) folder d:\a to the following “c:\Users\{username}\a”. If i enter “a” into the command line, it won’t recognize it as a folder until I press the OK button, and If I attempted to introduce subfolders, i.e. “a\b”, it won’t recognize it at all.

    However, if I enter “\Users\{username}\a” it not only recognizes it as a folder, but the autocomplete recognizes the subfolders, which I can then open unhindered.

    Is there any solution to this?

  38. Paul

    @Ran

    You can use the regular shortcuts for that. (right click > New > Shortcut)
    Say you have a drive called x:
    Create a shortcut pointing to x: and call it x, then place it in your system32 folder.
    Since system32 is in your system path by default, if you open the run box, and type: x enter
    it will open up the x: drive in an explorer window.

    If you were to do this over the network, you might need a Directory Junction, but a sym link should work fine. I have 3 media drives on my server and i link them (via regular shortcuts) to my “c:\share” folder, and they work fine from another box while mapped to “\\server\share”

    anyway…
    For your example, entering “a” into the run prompt does nothing. Try entering “d:\a”.

    to get the results you want, you can do:

    mklink /d %systemroot%\system32\X x:\whatever\folder\your\heart\desires

    now when you type X in the run box it will open your folder, no questions asked because it’s in the system path and can find it by default. Of course the address bar will say c:\windows\system32\x\some\folder\.. and that just looks unsettling.. i would just make a new folder and add it to your system path variable (mycomputer > properties > advanced system settings > advanced tab > environment variables > system variables > doubleclick path. do not erase anything just press the End key then add a ; followed by the directory you want to add.

    I made a folder called c:\apps
    Added: ;c:\apps
    to my system path variable
    and then make all my shortcuts there to apps / directories etc..
    then in the run box, you can open whatever you want, with minimal typing or waiting for indexing to find programs for you via start menu find box.

    I think i know what you mean tho about not recognizing subfolders.. i have a program called starter.exe in my apps folder, but its in a sub-folder called starter, you would think i could type “starter\starter.exe” but it wont recognize… IMO its easier just to have 1 folder for all your links, that is in the system search path.. no real data.. just links.. if you want shortcuts for a and a\b, just create them calling them a and ab lol sorry man, wish i could be more helpful

  39. Paul

    Correction:
    those regular shortcuts i was talking about in c:\share are actually junctions.. i just tried to create links using explorer and mklink /d and both failed over a network so junctions are needed to use a network resource transparently. I apologize for the confusion, I’m still trying to get a full understanding of this stuff as well.

  40. Jakob Bohm

    Factual corrections:

    Junctions are soft links just like directory symlinks, except that junctions work all the way back to Windows 2000 (and maybe NT 4.0 too). The only new thing for Junctions in Vista was that the command line tool to create them was no longer hidden on an unsupported “bonus” CD (the “Resource Kit”). Actually there are two kinds of junctions: Regular junctions (= symlinks to dirs) and special “DFS junctions” used on Windows Servers with the DFS feature.

    Hard links for files have been in NTFS since day one (32 bit NT 3.10), again the only new feature in vista is the inclusion of the command line tool in the supported OS packages.

    Vista adds two new types of softlinks: “Directory symlinks” and “File symlinks” the only difference is that some Windows code needs to know if it points to a directory or file without following the link first.

    The bad deletion behavior of deleting the original when you try to delete a symlink to a directory in the GUI is because they forgot to teach the GUI about symlinks, so the stupid thing thinks it is a real directory, does the equivalent of “rm -r” on it. Ditto for some GUI interfaces counting linked files twice when computing directory total sizes.

  41. Visa Processing Services

    Find Indian, Dubai, UAE, Chinese, Hong Kong, Egypt tourist visa, business visa, long term visa, and business travel visa with Findavisa.com. Apply online for Switzerland, Portugal and France Schengen visa services.

  42. Joe

    This is perfect! Now I can move my SteamApps folder to another drive!!!! YES!!!

  43. bob

    Joe –
    thats exactly how I found this site. I’ll even try to relocate only some games to a new location… let’s hope it works. I knew about linux symlinks, but always thought, windows didn’t know about those.
    I’m very happy that it kinda does!

  44. James

    RE: Rene Lindsay

    Years later, here is a response you likely will never read…

    What you describe is exactly how Mac OS X works! Very similar to RiscOS. Not sure which invented the executable folder first; RiscOS or NeXTStep but it was a darn good idea!
    (NeXTStep <= Mac OS X ; were much of NeXTStep / OpenStep remains in Mac OS X).

    NeXTStep was very cross platform and the executable folder actually had different executable binaries, one for each hardware platform. Mac OS X does the same with PowerPC and Intel binaries.

    Plus Mac OS X is a real Unix under the hood so symbolic and hard links work as expected to both folders and files. They are actually used a lot in Time Machine to reference older untouched files that haven't changed in the latest backup. New files in a dated backup are the actual files and not just links.

    I think you should seriously consider Mac OS X, it will keep you flashing back to those RiscOS days of lore!

  45. ToastyMozart

    So to clarify, a Soft link is like a sign pointing to where the file is, and a Hard link is like an Aperture Science portal to the file’s location.

  46. Terri

    Thanks so much for this! Now I can keep my local websites in my Dropbox account synced on all my computers and I created a symlink to it so Apache doesn’t complain.

    I did notice that for directories that have spaces I needed to surround the link or target with “double quotes”

  47. Adrian Ratnapala

    Thanks for the post, it works for me – sort of. It turns out I can only do this from an administrator shell. I can’t see why this should be, I can happily create ordinary files in the directory where the symlink resides. I have the same problem regardless of whether the symlink is within that directory or if it points somewhere else.

Enter Your Email Here to Get Access for Free:

Go check your email!