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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With