Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to translate a Windows path for Windows Subsystem for Linux

I'm looking for a way to translate Win32 paths into POSIX paths, preferably using Win32 tools.

Background

The latest Windows 10 Insider Build introduced the Windows Subsystem for Linux (WSL) including a native bash provided by Canonical, the company behind Ubuntu. Their implementation of bash goes by the rather complicated name of Bash on Ubuntu on Windows, which I will refer to as bash.exe in the following.

The equivalent of accessing the Windows path C:\Users\me\Desktop in bash.exe is /mnt/c/Users/me/Desktop.

I'm trying to pass a path to bash.exe from the Windows Command Prompt (e.g. bash -c ls /mnt/me/Desktop). Since that requires me to pass a POSIX path, I was wondering if Microsoft offers any tools to translate Win32 paths programmatically into POSIX paths (like cygpath does in Cygwin, or winepath on Wine)

Alternatives

Unless Windows ships with any tools for translation, I'm open to alternatives to determine the path, e.g. using Node or Python.

like image 689
idleberg Avatar asked Oct 19 '22 06:10

idleberg


2 Answers

The WSL Utilities package now provides this capability via the wslpath command.

WSLU is installed by default with the Ubuntu distribution, but may need to be added manually to other distributions. Instructions for package installation are on the Github page linked above.

Example usage from Bash or other POSIX shells:

cd $(wslpath 'C:\Users\<username>\Desktop')

... will change to /mnt/c/Users/<username>/Desktop.

The reverse conversion is also possible with the -w option:

mspaint.exe $(wslpath -w ~/profile.jpg)

... will open the file \\wsl$\<distroname>\home\<username>\profile.jpg in the Paint application.

like image 73
ToTamire Avatar answered Oct 21 '22 16:10

ToTamire


I have written a small shell script at [0] which is a beginning and I want to improve over time. I guess "sed" is a good tool to do some string replacements.

Here the current status:

linuxify() {
    windowspath=$1
    temppath="$(echo $windowspath | sed -E 's/([A-Z]):/\/mnt\/\L\1/g')"  # C: -> /mnt/c, E: -> /mnt/e
    temppath="$(echo $temppath | sed 's/\\/\//g')"  # backslash -> forward slash
    linuxpath=$temppath
    echo $linuxpath
}

Then you can use it like this

cd "`linuxify "E:\Marvin Kastner\Documents\Uni\Master\gitrepos\masterarbeit_neu"`"

[0] https://gist.github.com/1kastner/723a52f352c3eead42988c26b4ade5d0

like image 41
mkastner Avatar answered Oct 21 '22 14:10

mkastner