WSL icon indicating copy to clipboard operation
WSL copied to clipboard

Allow WSL to recognize Windows path names?

Open alexanderwhatley opened this issue 10 years ago • 25 comments

A useful feature of the windows command prompt is to be able to copy and paste the path from the file explorer into the command prompt and then change directories using cd. Right now, there doesn't seem to be a way to do this in WSL, besides manual entering. Could you maybe consider adding a feature for WSL to recognize windows paths and convert them to linux paths?

alexanderwhatley avatar Jun 14 '16 03:06 alexanderwhatley

Definitely an interesting idea, I agree it'd be nice if there was a tool inside WSL that could translate a windows path. I'd suggest filing a request on our User Voice page for this.

benhillis avatar Jun 14 '16 15:06 benhillis

If anyone wants a workaround, here's a quick hack that achieves this purpose:

https://gist.github.com/aseering/a06219e74c7f96ccea5ec65d5b2483b5

Use as, for example:

$ ls "$(winpath.sh $'C:\Program Files\Windows NT')"
Accessories  TableTextService

The script treats the Windows path as case-sensitive; that wouldn't be especially hard to fix, but it'd take more than a simple sed one-liner :-)

aseering avatar Jun 15 '16 03:06 aseering

hi @aseering I have very basic skills: how to apply your workaround and have the auto conversion from "C:\Users\me\Documents\GitHub" to "/mnt/c/Users/me/Documents/GitHub".

Thank you

aborruso avatar Jul 21 '16 06:07 aborruso

Babun shell has such a helper built in, though it uses a different prefix (/cygdrive/). One can paste a windows path into Babun terminal and it is converted to Cygwin-style mount compatible path. The path has to be quoted before when it contains space characters.

strarsis avatar Feb 23 '17 18:02 strarsis

Don't do this. It is one of my main issues with cygwin. Sometimes it works, sometimes it does not. Maybe some programm you are calling does not understand the unix or windows path format and you are never quite sure which one works. Stay consistent. Stay with Ubuntu userland. Unix paths. Don't do some half baked compatibility layer in between that translates Windows paths.

kwinz avatar Mar 30 '17 16:03 kwinz

I agree, don't make the terminal recognize Windows paths by default. Instead I'd like to see a tool like cypath that can be used to convert Windows paths into the correct WSL counterparts. Maybe a fork of cygpath? Using regular expression to replace slashes with backslashes and /mnt/x with x: won't be enough though. You also need to handle the special directories that are mounted within lxss:

LXSS = C:\Users\<WINUSER>\AppData\Local\lxss
/mnt/d -> D:
/mnt/D -> <LXSS>\mnt\D
/home/<NIXUSER> -> <LXSS>\home\<NIXUSER>
/root -> <LXSS>\root
/usr -> <LXSS>\rootfs\usr

Update:

Here's a shell script that should handle all the above mentioned cases: https://gist.github.com/darealshinji/61d97abf462c1dff9c582f3d94e16c58

darealshinji avatar May 01 '17 11:05 darealshinji

Having a standard tool that always does the correct mapping would be the best option here. Then it can be used in a subshell to convert Windows path to WSL (or back) in the middle of the command, if and when needed.

int19h avatar May 04 '17 18:05 int19h

Although a convert utility like cygpath.exe would be handsome, I'd like it much better if the WSL kernel functions (exec..., chdir, ...) could just accept valid windows paths (if there is no directory named C:, etc. in the current location) - cygwin allows this.

This also seems to be, what "Anjan" suggests at uservoice.

With this, at least bash should be able to cd into a windows directory - a quick strace shows chdir("C:\\Windows") = -1 ENOENT (No such file or directory) if I try to cd 'C:\Windows' in bash.

As this feature might add an ambiguity, it should be possible to enable or disable this behavior. (Cygwin only allows to warn about Windows-style paths ((no)dosfilewarning), but I think it should be possible to disable the feature completely.)

TSlivede avatar Aug 29 '17 21:08 TSlivede

This bugs for quite some time now. Strange that this is still open. I just tried out the new VS 2017 build linux command line tools feature using WSL itself as gdbserver and compiled this: https://gist.github.com/larsonmars/bf9b691c359bbd4be31e1e1e36277bbf worked for me, but there should be a better option...

larsonmars avatar Nov 02 '17 13:11 larsonmars

An equivalent of cygpath is suggested on uservoice. Vote for it there! https://wpdev.uservoice.com/forums/266908-command-prompt-console-bash-on-ubuntu-on-windo/suggestions/19380883-small-collection-of-official-wsl-utilities-akin-to

brianpeiris avatar Nov 03 '17 21:11 brianpeiris

Windows Insider Build 17046 contains new 'wslpath' functionality that supports path translations. See the release notes for more details.

Brian-Perkins avatar Nov 22 '17 18:11 Brian-Perkins

Can someone close this issue?

aaccioly avatar Jan 23 '18 19:01 aaccioly

how use wslpath to make WSL to recognize Windows path names?

Thank you

aborruso avatar Jun 06 '18 17:06 aborruso

@aborruso , from the release notes mentioned above:

   wslpath usage:
     -a    force result to absolute path format
     -u    translate from a Windows path to a WSL path (default)
     -w    translate from a WSL path to a Windows path
     -m    translate from a WSL path to a Windows path, with ‘/’ instead of ‘\\’

     EX: wslpath 'c:\users'

aaccioly avatar Jun 06 '18 17:06 aaccioly

@aaccioly thank you.

Is there a way (an helper) to drag & drop a windows path on WSL and to have it automatically converted in Linux path (in babun it works in this way)?

aborruso avatar Jun 06 '18 17:06 aborruso

@aborruso, AFAIK there is no such utility yet. Issue #1501 may be of interest.

aaccioly avatar Jun 06 '18 17:06 aaccioly

I know that issue, but it's closed.

Thank you for all

aborruso avatar Jun 06 '18 17:06 aborruso

No, it's not closed sorry and thank you again

aborruso avatar Jun 06 '18 17:06 aborruso

Hi @aaccioly seems lots of people hope wsl can support this feature: drag a file into wsl bash terminal (maybe it is running tmux), for example: c:\test\test.py, then wsl will automatically show /mnt/c/test/test.py

that is very helpful!

moreover, I want to remind as a security researcher. do not let wsl bash terminal support c:\test\test.py, that could cause unknown security issue!

yiwiz-sai avatar Dec 19 '18 02:12 yiwiz-sai

If you want to change to a directory using a #Windows10 style path in a #WSL console, you can define the following alias in your ~/.bashrc file:

alias cdw='f(){ cd wslpath $1; unset -f f; }; f'

After restarting your terminal, you can do:

$ cdw "c:\the\windows\target\path"

rcpacheco avatar Dec 05 '19 17:12 rcpacheco