Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Open WSL 2 Files and Directories with Windows Programs

open-wsl-2-files-and-directories-with-windows-programs.jpg

For example opening a directory with Windows explorer or an image with your image editing tool of choice.

Quick Jump:

Before we get started, it will be expected that you’re running WSL 2. You can verify that by running wsl.exe --version from a terminal. If you get a command not found error then you’re running WSL 1 and should switch to WSL 2.

Here’s the results in my case. At the time of writing this post I’m using the latest stable release of WSL 2. You can find the current latest version on GitHub, if you want to update to the latest version you can run wsl.exe --update.

$ wsl.exe --version
WSL version: 2.3.24.0
Kernel version: 5.15.153.1-2
WSLg version: 1.0.65
MSRDC version: 1.2.5620
Direct3D version: 1.611.1-81528511
DXCore version: 10.0.26100.1-240331-1435.ge-release
Windows version: 10.0.19045.5011

Also, it will be expected that you haven’t disabled opening Windows programs from within WSL 2 or prevented your $PATH from being modified with Windows paths. By default everything will work without any custom configuration unless you went to your /etc/wsl.conf file and set both of these options to false:

[interop]
# Setting this key will determine whether WSL will support launching Windows processes.
enabled = true

# Setting this key will determine whether WSL will add Windows path elements to the $PATH env var.
appendWindowsPath = true

The above options are set to true by default based on Microsoft’s documentation so you don’t need to set them. I included them here in case you happened to set them to false in which case you’ll want to remove them so they default to true.

# Launching Windows Programs

Funny enough we already did when we checked the WSL 2 version but let’s say you want to do something else like open a WSL 2 directory with Windows explorer.

All you have to do is cd into the directory you want and run explorer.exe . which opens the current working directory. You can also tab complete these tools like any other WSL 2 CLI tool, for example if you type expl and hit TAB it should complete it.

Some programs work out of the box because if you run echo $PATH you’ll likely see a bunch of Windows paths mixed in with WSL 2 paths. In my case, here’s 2 that stand out:

/c/WINDOWS/system32
/c/WINDOWS

The explorer.exe binary is in your Windows directory and your system32 directory has tons of Windows tools, such as calc.exe.

What Happens When a Program Isn’t on Your Path?

If you tried to launch chrome.exe chances are it won’t work (command not found), that’s because it’s not on your WSL 2 system path.

If you installed Chrome in its default location you should be able to run /c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe to launch chrome. If not then replace /c with /mnt/c because I customized my mount location in /etc/wsl.conf with root = /.

That’s cool, but it would be annoying to type that every time so you could make a shell alias just for Chrome such as alias chrome="/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe" and then you can run chrome directly.

Still, we can do better because it would be tedious to make a custom alias for each non-Windows app you want to launch in a convenient way.

Inherit What’s on Your Windows Path Inside of WSL 2

You can run cmd.exe /C start <app> where <app> is a tool you want to run such as cmd.exe /C start chrome or in my case I’m a big fan of foobar2000 for listening to mp3s so I can run cmd.exe /C start foobar2000 since it’s on my Windows path.

The /C is unrelated to your C:\ drive, it’s a cmd.exe option which says to run a command and then terminate cmd.exe itself.

You probably noticed in your terminal that the cmd.exe tool throws a warning:

$ cmd.exe /C start chrome
'\\wsl.localhost\Ubuntu\home\nick'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported.  Defaulting to Windows directory.

We can silence that by running cmd.exe /C start chrome 2> /dev/null and since we’re spawning a Windows app, if you try to run a command that doesn’t exist then Windows will throw an error dialog so we can at least still see invalid commands.

Armed with all of the above knowledge, we can make this easy to use with a shell alias with alias wopen="cmd.exe /C start 2> /dev/null" and now you can run wopen chrome or whatever you want, including wopen explorer . instead of explorer.exe . if you wanted to.

This alias has been added to my dotfiles.

The video below demos running a few of these commands.

# Demo Video

Timestamps

  • 0:22 – Make sure you’re using WSL 2
  • 0:56 – Avoid disabling this in your WSL config
  • 1:52 – Your mount path might be different
  • 2:29 – Opening directories with Windows explorer
  • 3:23 – Looking at your WSL 2 PATH
  • 4:43 – Running non-Windows programs
  • 5:59 – Using cmd.exe to launch Windows apps
  • 6:54 – Putting the final solution into an alias
  • 7:38 – What about custom installed tools?

Which Windows programs do you typically launch from WSL 2? Let me know below.

Never Miss a Tip, Trick or Tutorial

Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per year (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.



Comments