35

This web page implies that it is possible to make symbolic links with relative paths using mklink.

I have tried all sorts of ways to make relative symbolic links, but I always end up with an absolute path.

How is it done?

paradroid
  • 22,761
  • 10
  • 76
  • 114
  • MSDN has a strange way of explaining this. I had a hard time understanding how to make a symbolic link, since there are no actual examples. – iglvzx Nov 27 '11 at 08:06
  • The only reason I know this "strange way" is because MSDOS use this method. – surfasb Nov 27 '11 at 08:50
  • I don't understand why my question has 20,000 views now, when the problem was caused by a bug in TCC/LE. Symbolic links are made relative by default usually. – paradroid Feb 27 '17 at 14:01

2 Answers2

52

Symbolic links are relative by default. You have to explicitly write a drive letter to make any part of the link absolute.

The general syntax for a symbolic link is:

mklink link destination

So, to create a relative symbolic link: link is going to be a path relative to your working directory, and destination is going to be a path relative to link.

Examples:

1. mklink link.txt ..\destination.txt

This creates a symbolic link for link.txt which points to destination.txt one folder up.

You can move link.txt around, and it will always point to destination.txt one folder up.

2. C:\>mklink A\Link.txt ..\Destination.txt

This creates a symbolic link C:\A\Link.txt for C:\Destination.txt

paradroid
  • 22,761
  • 10
  • 76
  • 114
iglvzx
  • 23,459
  • 13
  • 85
  • 122
  • 1
    This is it. Rather than beginning with a drive letter or a backslash, you just begin with a directory. eg `mklink destination.txt "documentation\readme.txt"` will point to a child folder called *documetation" and a file in that folder called *readme.txt*. – surfasb Nov 27 '11 at 08:47
  • @surfasb, @iglvzx: At first I was confused, as that is exactly what I have been trying. But as you both confirm that this works, I have realised that it does work in `CMD`, but not in `TCC/LE`, which is what I have been using. I am surprised that it is altering (expanding) path arguments for external programs. – paradroid Nov 27 '11 at 09:20
  • @surfasb: This problem with TCC/LE has been fixed with the new v13, but happened with 12.11.76, which I had been using. – paradroid Nov 28 '11 at 09:58
  • 2
    @paradroid: I'm glad it is working out. I remember fondly the old old version of TCC. How long have you been using it now? On another not, yeah, the nuances of PATH enumeration boggles my mind. And the obligatory [blog post]:(http://blogs.msdn.com/b/oldnewthing/archive/2005/11/22/495740.aspx) about how past MS-DOS decisions towards compatibility **still** affects us today. – surfasb Nov 28 '11 at 14:25
0

To make relative link to a directory use /D switch

For example:

mklink /D lib\foo ..\foo

Links directory foo from parent directory as lib\foo.

When the link is moved to another directory, it will still point to ..\foo in a relative sense.

Junctions created using /J switch can have relative path specified at the time of creation, however this path is resolved and junction will always point to an absolute path.

infi
  • 9
  • 2
  • This is actually incorrect, as the `/J` switch is for making junctions, which are hardlinks, not symbolic links and has nothing to do with relative paths. – paradroid Feb 02 '23 at 19:57
  • You are right - my bad. The `/J` switch makes the link absolute even when at the time of creation relative path was given. – infi Feb 16 '23 at 03:53
  • Junctions aren't hardlinks, they're also a type of symbolic links (with slightly different semantics from "Unix symlinks" that `/d` creates), but it is true that they always use an absolute path. You can use `fsutil reparsePoint query` to see what path is stored inside a symlink or a junction. (Real hardlinks would not be reparse points at all.) – u1686_grawity Feb 16 '23 at 14:13