Windows 10 – How to Convert UNC Path to WSL Path

bashpathuncwindows 10windows-subsystem-for-linux

I created an alias as part of this question. It functions as expected except that whenever it's run from the home directory it outputs a non-fatal error:

'\\wsl$\Ubuntu-18.04\home\hashim'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported.  Defaulting to Windows directory.

According to a prominent WSL contributor, this is fixed by simply changing from the UNC path that the WSL home directory is located at to anything under /mnt/:

alias lsd="builtin cd $(wslpath $(cmd.exe /c "echo %USERPROFILE%") | tr -d '\r') && cmd.exe /c 'wmic diskdrive get Model,Manufacturer,Size,DeviceID,Status,InterfaceType'"

The above works to convert the Windows user profile path to a WSL path and CD into it before running the command, but it comes with the disadvantage that it requires a cd to an arbitrary path – the Windows user profile in this case – and can't be brought back to the previous directory.

I've had a quick go at both:

  • Converting the current UNC path at ~ to a /mnt/ path using wslpath
  • Using pushd and popd to manually change to an arbitrary /mnt/ directory and then change back to the original directory

…but I didn't manage to get either of these solutions working.

Is what I want possible?

Best Answer

As a workaround you can add && builtin cd - which is the bash equivalent of cd $OLDPWD

It has a disadvantage though: you lose the previous value of $OLDPWD. Personally I would find it very annoying and not a good solution for a command that's run often. I think in this case it's acceptable.

Hopefully someone else comes up with a better solution!

Related Question