27

I use WSL almost exclusively, and only switch to main windows for browsing and running Windows native programs. I have a git repository located at /mnt/c/myrepo. In order to "install" the code inside /mnt/c/myrepo I need to move it to /mnt/c/otherlocation/renamed. Instead of executing cp -r /mnt/c/myrepo /mnt/c/otherlocation/renamed every time I do a git pull from /mnt/c/myrepo, I would like to symlink /mnt/c/myrepo to /mnt/c/otherlocation/renamed. However when I do this, the program which consumes /mnt/c/otherlocation/renamed isn't able to view the "contents" of renamed as a directory.

I have been all over the WSL github repo and issue tracker trying to find a solution to this issue. I see a lot of exclamations about how symlinks "just work". I have enabled every Windows 10 developer feature I can find, I even followed some reddit thread where someone claimed that purchasing Pengwin and creating a symlink from Pengwin would ensure this compatibility, but I still can't seem to make this work.

The basic usage I need, is allow me to view "renamed" as a directory from windows, or for windows to recognize the symlink as a symlinked directory.

from wsl:

ln -s /mnt/c/myrepo  /mnt/c/otherlocation/renamed

from windows:

  1. open file explorer
  2. navigate to c:\otherlocation
  3. open mydir and view the contents as if it were a native directory

How do I do this?

2
  • Apparently, the correct answer is that the symlink path must be relative. From otherlocation the command is ln -s ../myrepo ./renamed
    – Nathan
    Commented Aug 20, 2019 at 19:45
  • 1
    Using relative paths doesn't work for me either, also, I have this issue in WSL/Windows 11 but not in WSL/Windows 10 Commented Sep 14, 2022 at 13:06

6 Answers 6

18

Do the symlink in Windows, in cmd.exe:

mklink /d C:\otherlocation\renamed C:\myrepo

It doesn't make sense creating the symlinks in WSL if both directories are in Windows.

This symlink will work in WSL as well.

5
  • 5
    This appears to work for local paths, but UNC paths and mapped drives just give "Input/output error" when you try to access them in WSL. Commented Mar 10, 2020 at 20:37
  • Give me an example Commented Mar 11, 2020 at 5:53
  • 5
    I used New-Item -ItemType SymbolicLink -Path "C:\path\to\your\renamed" -Target "C:\path\to\original" in Powershell rather. It supports UNC paths so I used this to symlink a \\wsl$\ubuntu\var\www\ directory to allow PowerShell to navigate to it and run npm from windows.
    – Mikepote
    Commented Sep 29, 2022 at 13:08
  • 2
    Since Powershell doesn't support relative paths with New-Item -ItemType SymbolicLink , it's easiest to invoke cmd from Powershell, eg cmd /c mklink /d native windows-amd64 . This will create a link that works in WSL, too. Remember to elevate your shell or use the fancy config to avoid needing to elevate. Commented Oct 9, 2022 at 0:55
  • The top-level answer does not answer the question that is stated in the title. See stackoverflow.com/a/76181147/12094608 and my addition at stackoverflow.com/a/78500345/12094608 if you came here through Google
    – KamielDev
    Commented May 18 at 16:34
15

The solution to this problem is simply to use the relative path when declaring the link. If you want to link from windows to windows, you should relatively path from the current directory and then you can link anywhere you wish.

From the example, instead of this:

ln -s /mnt/c/myrepo  /mnt/c/otherlocation/renamed

Do this:

cd /mnt/c/otherlocation
ln -s ../../myrepo ./renamed
2
  • 6
    That doesn't appear work for me using wsl2 and windows 11. The symlink works as expected within WSL2, but is invalid in Windows. Commented Dec 2, 2021 at 15:43
  • Unfortunately, this won't work as well. Windows displays symlinks as invalid junctions. It's not possible to neither step into them or get information about them. Commented Apr 9, 2022 at 16:37
11

Since this seems to be the only SO post that has any googlejuice when searching for Symlink from Windows to WSL I thought I'd add the answer to that question, ie,

how to make a "symlink" from the windows filesystem to the wsl filesystem, that works for windows apps?

answer was in the comments, credit to @Mikepote

  1. first open Powershell with Run as Administrator

  2. then run

 New-Item -ItemType SymbolicLink -Path "windows-path\to\symlink" -Target "\\wsl$\Ubuntu\home\yourusername\path\to\target-thing" 

substitute your distro name for Ubuntu above if necessary, and yourusername obviously too.

2
  • 1
    Unfortunately, while this answer works when making a symbolic link in a Windows directory that targets a WSL directory, attempting to do it the other direction errors out with "New-Item: Symbolic links are not supported for the specified path." in Windows 11. That is, -Target "windows\path" -Path "\\wsl$\Ubuntu\path" will fail. (Btw, your use of "from" and "to" originally made me think you were talking about the reverse, but rather than argue those semantics, I'll just point out the issue with as clear language as I can)
    – Shenk
    Commented Dec 6, 2023 at 13:20
  • This also does not help when one receives a large software package (parhaps as a tarball or even a repo) that is Linux based and contains lots of soft links inside it. Being able to copy that out of the Linux (WSL) environment so that it is in the Windows (NTFS) environment has eluded me for a while. [this is different than the case where things are create i the Wndows environment] Commented Feb 27 at 13:18
0

If the folder is in a drive, the latter can be mounted inside WSL:

mkdir /mnt/z
mount -t drvfs Z: /mnt/z

/etc/fstab can be updated accordingly.

0

To add to https://stackoverflow.com/a/76181147/12094608

If you want to make a symbolic link for a Windows program to a WSL file, but want to make this link from the WSL side instead of opening Powershell as an elevated user yourself, (for example, because your .dotfiles setup/install script is ran from WSL) then you can write a simple bash script such as this:

ps_command='New-Item -ItemType SymbolicLink -Path $env:APPDATA\alacritty\alacritty.toml -Target \\wsl$\Ubuntu\home\kamiel\.dotfiles\alacritty\alacritty.toml'

powershell.exe -Command "Start-Process PowerShell -Verb RunAs -ArgumentList '$ps_command'"

We make a variable ps_command that holds the command from the referenced answer (using a variable will ensure that powershell retains the special characters in the command, otherwise you'd have to escape characters such as the \ because PowerShell is weird.)

Then we start powershell.exe as an elevated user (this will prompt you with a UAC popup, AFAIK there's no way to do this unattended due to security reasons), and execute the command to create the symlink.

To elaborate on what happens in my exact example is that Alacritty on Windows looks for the config file in %APPDATA%\alacritty\alacritty.toml. The file actually lives in WSL, at ~/.dotfiles/alacritty/alacritty.toml. So we create a symlink on Windows' side to the actual location of the .toml in a WSL dir.

0

Creating a hard link between directories using cmd with administrator privileges worked for me:

mklink /H C:\Users\NameNoSpace "C:\Users\Name With Spaces"
4
  • /J creates a directory-junction (like /H for files) which is NOT symbolic (learn.microsoft.com/en-us/windows-server/administration/…) .. also put "name with spaces in doublequotes"
    – Bernhard
    Commented Oct 19, 2023 at 5:45
  • Quite right, thanks, corrected
    – Mike Slinn
    Commented Oct 20, 2023 at 12:12
  • /j creates a junction point rather than a hard link. /h is for hard links (multiple references on the same volume to a single file). Commented Jun 14 at 8:58
  • Corrected again. I wonder why and how it reverted back to /J?
    – Mike Slinn
    Commented Jun 14 at 16:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.