TransWikia.com

How can I "open" a file from WSL with the default application?

Super User Asked by Cheezmeister on January 5, 2021

Most systems provide an open command (alternately known as start, cygstart, xdg-open etc.) which opens a file in the desktop environment’s “default” application, whatever that is.

For example, from Powershell when I enter:

PS> start form.pdf

The document pops open in Edge.

Can Bash-on-Ubuntu-on-Windows do this?

11 Answers

Since the Windows Linux interop started working you can now call:

cmd.exe /C start <file>

Correct answer by Martijn Laarman on January 5, 2021

A recent answer for WSL 2: Just do wslview slides.pdf.

This is a part of the wslu collection and was pre-installed in my Ubuntu. Unfortunately, the documentation is a bit sparse.

All the cmd.exe variants didn't work for me, because it doesn't want to open inside a path like \wsl$…

Answered by primfaktor on January 5, 2021

Try this:

function open {
  cmd.exe /C start "$1" /C bash
}

If you want it work in every time just add it to the .bashrc:
enter link description here

Answered by Nullcito on January 5, 2021

to expand on Martijn's answer, you can put

alias start='cmd.exe /C start'

in your .bashrc to get expected windows behavior, eg start . opens explorer in current dir.

Answered by MyrionSC2 on January 5, 2021

You can call powershell's Start-Process command from within WSL:

powershell.exe -Command Start-Process file

For making this also work with absolute paths, you can use the wslpath -wa command to translate the path to a windows-path.

powershell.exe -Command Start-Process `wslpath -wa /absolute/path/to/file`

This has an advantage over the cmd.exe solution: for mounted network shares, wslpath produces an UNC path like \servershare. These UNC paths can't be handled by cmd.exe.

Answered by geronimo on January 5, 2021

This worked much better for me:

explorer.exe `wslpath -aw <path>`

Answered by Ngo on January 5, 2021

eopen can open various files (, directories and URI) within WSL.

https://github.com/ko1nksm/eopen-ecd

Examples

# Open directory with (latest used) Explorer
eopen ~/.config/

# Open directory with new instance of Explorer
eopen -n ~/.config/

# Opens with Windows default application
eopen image.png

# Opens with Windows text editor
eopen -e ~/.bashrc

# Use sudo to edit the unowned file
eopen -e --sudo /etc/hosts

# Opens with Windows default browser
eopen http://google.com

# Open files and directories under Windows
eopen C:/Windows

# Open files and directories under Network shared folder
eopen //server/shared

# Others
eopen mailto:[email protected]   # Mail protocol
eopen calculator:               # Application
eopen shell:Personal            # Shell commands
eopen :MyComputerFolder         # Shorthand for shell:
eopen shell:::{2559a1f8-21d7-11d4-bdaf-00c04f60b9f0} # CLSID
eopen :                         # Current Explorer location
eopen :/workspace               # Relative path from current Explorer location

Answered by Koichi Nakashima on January 5, 2021

Try using wsl-open. It opens files with their standard-windows-application and can be downloaded here: https://github.com/4U6U57/wsl-open.

Answered by marv on January 5, 2021

As Martijn noted this is the correct way to execute/open a Windows application/file.

cmd.exe /C start <file>

I found it very useful to work this into a bash script that I keep in a folder that is in my system path. I name it start and do chmod 0744 to the file to make it executable. The $* means it will pass all of the command line arguments you provided to the script to cmd.exe.

#!/bin/bash
cmd.exe /c start "Launching from BASH" "$*"

With this command in my system path I can commands like this in Linux that open in Windows:

  1. start FileXYZ.pdf // Opens the PDF in the default assigned PDF viewer in Windows
  2. start explorer . // Opens current WSL folder in the Windows Explorer
  3. start MyApp.exe // Launches the Windows application

Answered by pseudosavant on January 5, 2021

I've found that explorer.exe works pretty well at finding the correct resolved path (even of mounted network directories) and launching the default tool. The one gotcha is that you can't have paths in the filename, so you need to create a little helper function/script to launch explorer correctly, e.g.:

win() { 
    # get full unsymlinked filename 
    file=`readlink -e $1` 
    dir=$(dirname "$file") 
    base=$(basename "$file") 
    # open item using default windows application 
    (cd "$dir"; explorer.exe "$base")
}

Update: Ngo pointed out another script, wslpath, that does the path conversion, so you can call explorer.exe directly on the path (after converting). Then the function above becomes trivial and can easily be made an alias.

Answered by mattgately on January 5, 2021

This depends on whether you want to A) launch a linux program inside WSL or B) you want to launch a windows program from a bash shell prompt.

If B) then yes if you install cygwin/bash. For example install git for windows and you have a system running under windows with bash. Then you can just run start, actually it is included as a script:

$ cat /usr/bin/start
#!/usr/bin/env bash
# Copyright (C) 2014, Alexey Pavlov
#   mailto:[email protected]
# This file is part of Minimal SYStem version 2.
#   https://sourceforge.net/p/msys2/wiki/MSYS2%20installation/
# File: start

cmd //c start "${@//&/^&}"

If A) then it gets much more difficult especially if you want to launch a linux program to display a .pdf in a GUI window. Note that Windows knows to associate a default application to open a pdf file but WSL doesn't have the information. So even if you did get a Desktop running under WSL you would need to associate a linux GUI app to open the pdf.

Note to clarify, within WSL you execute linux executables not windows executables:

(WSL):~# file /bin/gzip
/bin/gzip: ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=68cc3c090405cf6d40e97d2ff58085fd26940602, stripped

(WSL):~# file /mnt/c/Program Files/Internet Explorer/iexplore.exe
 /mnt/c/Program Files/Internet Explorer/iexplore.exe: PE32+ executable (GUI) x86-64, for MS Windows

(WSL):~# /mnt/c/Program Files/Internet Explorer/iexplore.exe
 bash: /mnt/c/Program Files/Internet Explorer/iexplore.exe: cannot execute binary file: Exec format error

Answered by Gilbert on January 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP