如何为适用于 Linux 的 Windows 子系统转换 Windows 路径

idl*_*erg 5 python bash node.js windows-10 windows-subsystem-for-linux

我正在寻找一种将 Win32 路径转换为 ​​POSIX 路径的方法,最好使用 Win32 工具。

背景

最新的 Windows 10 Insider Build 引入了适用于 Linux 的 Windows 子系统 (WSL),包括bash由 Ubuntu 背后的公司 Canonical 提供的本机。他们的实现在 Windows 上的 Ubuntu 上bash使用了相当复杂的Bash名称,我将bash.exe在下文中引用它。

访问Windows路径的等效C:\Users\me\Desktopbash.exeIS /mnt/c/Users/me/Desktop

我正在尝试bash.exe从 Windows 命令提示符(例如bash -c ls /mnt/me/Desktop)传递路径。由于这需要我传递 POSIX 路径,我想知道 Microsoft 是否提供任何工具以编程方式将 Win32 路径转换为 ​​POSIX 路径(就像cygpath在 Cygwin 中所做的那样。)

除非 Windows 附带任何翻译工具,否则我对确定路径的替代方法持开放态度,例如使用 Node 或 Python。

ToT*_*ire 10

WSL 实用程序包现在通过以下方式提供此功能wslpath

WSLU 默认随 Ubuntu 发行版一起安装,但可能需要手动添加到其他发行版。包安装说明位于上面链接的 Github 页面上。

Bash 或其他 POSIX shell 的用法示例:

cd $(wslpath 'C:\Users\<username>\Desktop')
Run Code Online (Sandbox Code Playgroud)

...将更改为/mnt/c/Users/<username>/Desktop.

也可以使用以下-w选项进行反向转换:

mspaint.exe $(wslpath -w ~/profile.jpg)
Run Code Online (Sandbox Code Playgroud)

\\wsl$\<distroname>\home\<username>\profile.jpg...将在画图应用程序中打开文件。