Skip to content

Instantly share code, notes, and snippets.

@judge2020
Last active May 26, 2019 14:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save judge2020/4450bc62df8adeeb29f02bd5524ab3db to your computer and use it in GitHub Desktop.
Save judge2020/4450bc62df8adeeb29f02bd5524ab3db to your computer and use it in GitHub Desktop.
Open explorer at folder - bash script for Bash on Ubuntu On Windows
#!/bin/bash
#
# START CONFIG
#
# Change driveletter below if windows is installed on something other than the C drive
driveletter=c
#
# END CONFIG
#
#
prefix=/mnt/
# Check for microsoft in uname -v, to see if they're on BoUoW
if [[ $(uname -v) != *"Microsoft"* ]]; then
echo WARNING: This script is intended for use on Microsoft\'s Bash on Ubuntu On Windows \(Linux Subsystem\) and will likely not work otherwise!
fi
if [ ! -f /mnt/$driveletter/Windows/explorer.exe ]; then
echo FATAL: Unable to find the explorer EXE at /mnt/$driveletter/Windows/explorer.exe. You might want to modify the Windows Driveletter by editing this executable \(eg \"nano $(dirname $0)\/explorer\"\)
exit
fi
# See if desired directory is current working directory
if [[ $1 == "" || $1 == "." ]]; then
destin=$PWD
else
destin=$(readlink --canonicalize $1)
fi
# Remove the /mnt
path=${destin#$prefix}
# Append the :\
path=${path:0:1}:\\${path:2}
# Replace slashes with backslashes
path=${path//\//\\}
# If it's a directory, open inside that dir
if [[ -d $destin ]]; then
/mnt/$driveletter/Windows/explorer.exe "$path"
exit
fi
# If file, open to folder and select that file
if [[ -f $destin ]]; then
/mnt/$driveletter/Windows/explorer.exe /select, "$path"
exit
fi
echo FATAL: Argument 1 is not a file or directory!

To install explorer in Bash on Ubuntu on Windows

  1. open BoUoW
  2. type these commands:
sudo wget -O /usr/bin/explorer https://gist.githubusercontent.com/judge2020/4450bc62df8adeeb29f02bd5524ab3db/raw/explorer.sh
sudo chmod +x /usr/bin/explorer

Done!

To use, just type explorer to open the current directory in windows explorer. You can also point it to certain folders like you would in PS/cmd, ex explorer ../..

You can pass a directory or a file name (including relative paths) and it'll open explorer appropriately

@hughc
Copy link

hughc commented Jul 31, 2018

this doesn't seem to work for directories within the Ubuntu subsystem's file structures, ie /home/xxxxx - executing it in the user home folder opens the current user's Documents folder instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment