10

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.

idleberg
  • 12,634
  • 7
  • 43
  • 70
  • 2
    It's clear to me what is being asked, although maybe I'm reading between the lines. This will eventually be useful to anyone porting cygwin scripts to WSL, ideally in the form of a script or binary that implements similar command line arguments as cygpath. – philwalk Dec 01 '16 at 21:34

5 Answers5

8

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.

ToTamire
  • 1,425
  • 1
  • 13
  • 23
3

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

mkastner
  • 103
  • 10
  • Please don't just post some tool or library as an answer. At least demonstrate [how it solves the problem](http://meta.stackoverflow.com/a/251605) in the answer itself. – Papershine Oct 04 '17 at 10:07
  • I thought the same way like you do and edited my answer even without seeing your comment and minus remark. I was a bit fast to click on the answer button before. Whether the current answer is still worth a minus mark is now up to you. – mkastner Oct 04 '17 at 10:17
  • It seems that wslpath cannot convert backslash (that is the default path sep in windows), so I need your tool. – Timo Jan 26 '21 at 18:23
  • See https://stackoverflow.com/questions/53474575/how-to-translate-the-wslpath-home-user-to-windows-path and https://devblogs.microsoft.com/commandline/windows10v1803/ The command line tool `wslpath` does the job now. – mkastner Jan 27 '21 at 14:59
2

Following up on @ToTamire's answer. While I've already edited it to add some information on the usage of wslpath, the focus in that answer (as well as all of the previous answers) was in using it from inside WSL.

The question that was originally asked here was how to do it in Windows (rather than WSL) and "pass a path to bash.exe", so let's look at some (new, since the question was originally asked) options for that. I would still use wslpath, but through the wsl.exe command (which also wasn't available when the question was asked, but replaces the bash.exe command for WSL).

Since the question says "from the Windows Command Prompt", we'll use CMD, although I would prefer PowerShell due to its more modern process substitution and quoting/escaping rules.

In CMD:

  • Example 1: Convert a Windows Path to WSL/Linux format

    • Command: wsl wslpath "C:\\"
    • Result: Outputs /mnt/c
  • Example 2: Convert a Windows Path to WSL/Linux format via for

    • Command: for /f %p in ('wsl wslpath "C:\\"') do echo %p
    • Result: Also outputs /mnt/c
  • Example 3: Use touch in WSL to create a file in your Windows home/profile directory (starting from CMD)

    • Command:
      for /f %p in ('wsl ~ wslpath "%userprofile%"') do set wsl_path=%p
      wsl ~ -e sh -c "touch \"%wsl_path%/StackOverflowTest\""
      
    • Result: Creates the file StackOverflowTest in your Windows home/profile directory
NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70
0

It turns out that NodeJS has a built-in module for this kind of stuff, called path. Although it doesn't completely fix the issue at hand, it's a valid workaround (to me). Just require("path") and decide on the block or one liner.

Here's the block:

var p = require("path")
var path = "C:\\Users\\me\\Desktop"
var sepa = path.split(p.win32.sep)
var newS = [].concat([sepa[0].toLowerCase()], sepa.slice(1))
var newP = "/mnt/" + p.posix.join.apply(p.posix, newS).replace(":", "")
// newP == "/mnt/c/Users/me/Desktop

As a one liner:

var p = require("path")
var d = "/mnt/" + p.posix.join.apply(p.posix, [].concat(["C:\\Users\\me\\Desktop".split(p.win32.sep)[0].toLowerCase()], "F:\\Users\\me\\Desktop".split(p.win32.sep).slice(1))).replace(":", "")
TheBrenny
  • 528
  • 3
  • 19
-2

The path for your Ubuntu location in the Linux subsystem for Windows is located here:

C:\Users\<user>\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc
Kris Wheeler
  • 279
  • 4
  • 8
  • Do **not** under any circumstances use that path in WSL1. [Per Microsoft](https://devblogs.microsoft.com/commandline/do-not-change-linux-files-using-windows-apps-and-tools/) it can cause corruption of the WSL instance. – NotTheDr01ds Oct 10 '21 at 00:36