WSL: Using A WSL symlink folder from Windows
Asked Answered
R

6

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?

Reeve answered 20/8, 2019 at 19:31 Comment(2)
Apparently, the correct answer is that the symlink path must be relative. From otherlocation the command is ln -s ../myrepo ./renamedReeve
Using relative paths doesn't work for me either, also, I have this issue in WSL/Windows 11 but not in WSL/Windows 10Contango
T
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.

Tragedienne answered 21/8, 2019 at 15:5 Comment(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.Abomination
Give me an exampleTragedienne
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.Oruntha
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.Propitiatory
The top-level answer does not answer the question that is stated in the title. See https://mcmap.net/q/508298/-wsl-using-a-wsl-symlink-folder-from-windows and my addition at https://mcmap.net/q/508298/-wsl-using-a-wsl-symlink-folder-from-windows if you came here through GoogleGeter
R
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
Reeve answered 26/5, 2020 at 21:11 Comment(2)
That doesn't appear work for me using wsl2 and windows 11. The symlink works as expected within WSL2, but is invalid in Windows.Goliath
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.Bleeder
L
12

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.

Locative answered 5/5, 2023 at 10:18 Comment(2)
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)Barry
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]Ejaculation
K
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"
Kampala answered 24/7, 2023 at 15:57 Comment(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"Hoban
Quite right, thanks, correctedKampala
/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).Wishbone
Corrected again. I wonder why and how it reverted back to /J?Kampala
O
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.

Ourselves answered 13/2 at 15:28 Comment(0)
G
0

To add to https://mcmap.net/q/508298/-wsl-using-a-wsl-symlink-folder-from-windows

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.

Geter answered 18/5 at 16:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.