WSL:使用 Windows 中的 WSL 符号链接文件夹

Nat*_*han 9 symlink windows-subsystem-for-linux

我几乎只使用 WSL,并且只切换到主窗口来浏览和运行 Windows 本机程序。我有一个 git 存储库位于/mnt/c/myrepo. 为了“安装”里面的代码,/mnt/c/myrepo我需要将它移动到/mnt/c/otherlocation/renamed. cp -r /mnt/c/myrepo /mnt/c/otherlocation/renamed我不想每次执行git pullfrom时都执行/mnt/c/myrepo,而是希望符号链接/mnt/c/myrepo/mnt/c/otherlocation/renamed. 但是,当我这样做时,使用的程序/mnt/c/otherlocation/renamed无法查看重命名为目录的“内容”。

我一直在 WSL github 存储库和问题跟踪器中试图找到解决此问题的方法。我看到很多关于符号链接如何“正常工作”的感叹。我已经启用了我能找到的所有 Windows 10 开发人员功能,我什至关注了一些 reddit 线程,有人声称购买 Pengwin 并从 Pengwin 创建符号链接将确保这种兼容性,但我似乎仍然无法完成这项工作。

我需要的基本用法是允许我从 Windows 中查看“重命名”为目录,或者让 Windows 将符号链接识别为符号链接目录。

来自 wsl:

ln -s /mnt/c/myrepo  /mnt/c/otherlocation/renamed
Run Code Online (Sandbox Code Playgroud)

从窗口:

  1. 打开文件浏览器
  2. 导航到 c:\otherlocation
  3. 打开 mydir 并像查看本机目录一样查看内容

我该怎么做呢?

Car*_*rez 9

Do the symlink in Windows, in cmd.exe:

mklink /d C:\otherlocation\renamed C:\myrepo
Run Code Online (Sandbox Code Playgroud)

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

This symlink will work in WSL as well.

  • 我在 Powershell 中使用了 `New-Item -ItemType SymbolicLink -Path "C:\path\to\your\renamed" -Target "C:\path\to\original"` 。它支持 UNC 路径,因此我使用它来符号链接 \\wsl$\ubuntu\var\www\ 目录,以允许 PowerShell 导航到它并从 Windows 运行 npm。 (5认同)
  • 这似乎适用于本地路径,但当您尝试在 WSL 中访问 UNC 路径和映射驱动器时,它们只会给出“输入/输出错误”。 (2认同)
  • 由于 Powershell 不支持 New-Item -ItemType SymbolicLink 的相对路径,因此最简单的方法是从 Powershell 调用 cmd,例如 `cmd /c mklink /d native windows-amd64` 。这将创建一个也可以在 WSL 中使用的链接。请记住提升您的 shell 或使用花哨的配置来避免需要提升。 (2认同)

Nat*_*han 6

解决这个问题的方法很简单,就是在声明链接时使用相对路径。如果你想从windows链接到windows,你应该从当前目录相对路径,然后你可以链接到你想要的任何地方。

从示例中,而不是这样:

ln -s /mnt/c/myrepo  /mnt/c/otherlocation/renamed
Run Code Online (Sandbox Code Playgroud)

做这个:

cd /mnt/c/otherlocation
ln -s ../../myrepo ./renamed
Run Code Online (Sandbox Code Playgroud)

  • 这对我使用 wsl2 和 windows 11 来说似乎不起作用。符号链接在 WSL2 中按预期工作,但在 Windows 中无效。 (6认同)

hwj*_*wjp 5

由于这似乎是唯一一篇在从 Windows 到 WSL 搜索符号链接时具有任何 googlejuice 的 SO 帖子,我想我应该添加该问题的答案,即

如何创建适用于 Windows 应用程序的从 Windows 文件系统到 wsl 文件系统的“符号链接”?

答案在评论中,归功于@Mikepote

  1. 首先以管理员身份运行打开Powershell

  2. 然后运行

 New-Item -ItemType SymbolicLink -Path "windows-path\to\symlink" -Target "\\wsl$\Ubuntu\home\yourusername\path\to\target-thing" 
Run Code Online (Sandbox Code Playgroud)

Ubuntu如果有必要,显然也可以用您的发行版名称替换上面的名称yourusername