Soft links, hard links, junctions, oh my! Symlinks on Windows, a how-to.

Dev

Soft links, hard links, junctions, oh my! Symlinks on Windows, a how-to.

by Joshua Poehls
//

First, a quick definition of terms. There are three kinds of ”symlinks” on Windows.

  • soft links (also called symlinks, or symbolic links)
  • hard links
  • junctions (a type of soft link only for directories)

Soft links can be created for files or directories.

Hard links can only be created for files.

Both soft and hard links must be created on the same volume as the target. i.e. You can’t link something on C: to something on D:.

This is where the difference between soft and hard links is most evident.

Deleting the target will cause soft links to stop working. What it points to is gone. Hard links however will keep right on working until you delete the hard link itself. The hard link acts just like the original file, because for all intents and purposes, it is the original file.

Junctions

Windows also has another type of link just for directories, called Junctions.

Junctions look and act like soft links. The key difference is that they allow you to link directories that are located on different local volumes (but still on the same computer). You can’t create a junction to a network location.

Create a soft link to a directory.

c:symlink_test> mklink symlink_dir real_dir
symbolic link created for symlink_dir > real_dir

Create junction link to a directory.

c:symlink_test> mklink /J junction_dir real_dir
Junction created for junction_dir > real_dir

Create a soft link to a file.

c:symlink_test> mklink symlink_file.txt real_file.txt
symbolic link created for symlink_file.txt > real_file.txt

Create a hard link to a file.

c:symlink_test> mklink /H hardlink_file.txt real_file.txt
Hardlink created for hardlink_file.txt > real_file.txt

What they look like.

c:symlink_test> dir
Volume in drive C is OS
Volume Serial Number is 7688-08EC

Directory of c:symlink_test

06/07/2012  10:32 AM              .
06/07/2012  10:32 AM              ..
06/07/2012  09:51 AM                15 hardlink_file.txt
06/07/2012  09:59 AM         junction_dir [c:symlink_testreal_dir]
06/07/2012  09:47 AM              real_dir
06/07/2012  09:51 AM                15 real_file.txt
06/07/2012  10:00 AM         symlink_dir [real_dir]
06/07/2012  10:31 AM          symlink_file.txt [real_file.txt]
               3 File(s)             30 bytes
               5 Dir(s)  145,497,268,224 bytes free

Note for PowerShell users:
MKLINK isn’t an executable that you can just call from PowerShell. You have to call it through the command prompt.

cmd /c mklink /D symlink_dir real_dir

Alternatively, you can use this module I wrote that has native PowerShell wrappers for MKLINK.

Read about MSLINK on MSDN.

Using FSUTIL

FSUTIL is another way to create hard links (but not soft links). This is the same as mklink /H.

c:symlink_test> where fsutil
C:WindowsSystem32fsutil.exe

c:symlink_test> fsutil hardlink create hardlink_file.txt real_file.txt
Hardlink created for c:symlink_testhardlink_file.txt > c:symlink_testreal_file.txt

Read about FSUTIL on MSDN.

Using Junction

Junction is a tool provided by Sysinternals and provides another way to create junctions. Same as mklink /J. It also has some other tools for working with junctions that I won’t cover here.

c:symlink_test> junction junction_dir real_dir
Junction v1.06 - Windows junction creator and reparse point viewer
Copyright (C) 2000-2010 Mark Russinovich
Sysinternals - www.sysinternals.com

Created: c:symlink_testjunction_dir
Targetted at: c:symlink_testreal_dir

Download the Junction tool from Sysinternals.


Read more about hardlinks and junctions on MSDN.

More About the Author

Joshua Poehls

Software Architect
Tableau via PowerShell, Part 2: Saving Changes This is part 2 of my mini-series on exploring Tableau workbooks with PowerShell. If you missed it, you should read Part 1: Opening ...
Tableau via PowerShell, Part 1: Opening Workbooks In this mini-series I’m going to show you some cookbook style examples for using PowerShell to explore your Tableau workbooks. ...

See more from this author →

InterWorks uses cookies to allow us to better understand how the site is used. By continuing to use this site, you consent to this policy. Review Policy OK

×

Interworks GmbH
Ratinger Straße 9
40213 Düsseldorf
Germany
Geschäftsführer: Mel Stephenson

Kontaktaufnahme: markus@interworks.eu
Telefon: +49 (0)211 5408 5301

Amtsgericht Düsseldorf HRB 79752
UstldNr: DE 313 353 072

×

Love our blog? You should see our emails. Sign up for our newsletter!