I know that in MacOS, people use the open .
command to open the current directory in a file manager.
Does anybody know the appropriate command to do the same task in Bash on Windows?
I know that in MacOS, people use the open .
command to open the current directory in a file manager.
Does anybody know the appropriate command to do the same task in Bash on Windows?
On Windows with Git Bash, just type:
start .
it will open the current directory in the file explorer.
You can now call explorer.exe from the bash subsystem. I've set up an alias to use it. I've added the copy to clipboard alias as well for good measure.
Alias:
alias open="explorer.exe"
alias pbcopy="clip.exe"
Example:
cat ~/.ssh/id_rsa.pub | pbcopy
open .
open "D:\\Dir"
The open alias plays well with .
, but you'll need to pass it the Windows path if you want to specify a directory.
Right now Microsoft don't recommend to mix Windows explorer with the bash shell. In latest win10 Insider builds you could use from bash something like this
cmd.exe /c start .
If you are using Win10 Anniversary Edition you could try installing a Desktop Environment. Start reading this https://github.com/microsoft/bashonwindows/issues/637 After that you could open a window with the present folder content with
gnome-open .
I'm using this function:
open()
{
explorer.exe `wslpath -w "$1"`
}
So if you are in /mnt/c/Users/
and would like to open that folder, just type open .
wslpath
will resolve only paths from the Windows system, be aware. If you are looking to do something like open ~
it will not work, and you will get:
wslpath: /home/my-user: Result not representable
Command usage
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’
A proof that this works:
I have added
alias open='explorer.exe `wslpath -w "$1"`'
to the .bashrc
file
opens current folder when typed open
I'm using windows ubuntu subsystem.
try this
$ explorer .
This will work on bash command in windows. I was using R studio and was able to open the directory.
If start .
doesn't work for you, it's essentially the same as running explorer.exe .
so you can create an alias for it which is what I did.
alias start="explorer.exe"
Side note: Another useful one to have is BROWSER
. explorer.exe is capable of starting your default web browser. This comes in handy when you run scripts that open a web browser like starting a React.js development server.
export BROWSER="explorer.exe"
To work in all type of paths (Windows-Style and Linux-Style), do as following (the answer of mine to my own question on SU):
(Here my challenge was how I could open Explorer in current working directory with Linux-Style path for view purposes, if you are going into modification or do something else other than just viewing, this is at your own risk, please also read Do not change Linux files using Windows apps and tools):
explorer.exe "C:\Users\userNmae\AppData\Local\Lxss$(sed 's:/:\\:g' <<<"$PWD")"
this will open the Explorer exactly in your working directory. The only thing you need is now define a function to get it to work. You can add this into your .bashrc
and source it or re-open Bash.
xplor(){
explorer.exe "C:\Users\userName\AppData\Local\Lxss$(sed 's:/:\\:g' <<<"$PWD")";
}
Note: Replace userName
with your Windows User account name there.
In WSL2 it looks like the open
command is now doing what start
does in Windows command. I just put an alias in my .bashrc
start="open"
Now you can do either start .
or open .
depending on your preferences.
Using the MINGW64 shell that comes with Git for Windows, I created these functions in my .profile:
function towinpath {
{ cd "$1" && pwd -W; } | sed 's|/|\\|g'
}
function open {
path_to_open=""
if [ -f "$1" ]; then
filename=$(basename "$1")
win_dirname=$(towinpath "$(dirname "$1")")
path_to_open="$win_dirname\\$filename"
elif [ -d "$1" ]; then
path_to_open=$(towinpath "$1")
else
# Take our chances with windows explorer ...
path_to_open="$1"
fi
if [ -z "$path_to_open" ]; then
echo "Failed to open $1"
else
explorer "$path_to_open"
fi
}
This seems to work with just about everything you can throw at Windows File Explorer:
open . # Opens the current directory
open ../ # Opens the directory above this one
open /c/Windows # Opens C:\Windows
open ~/AppData # Opens my AppData folder
open ~/*.csv # Opens the first CSV file in my home directory in Excel
open https://stackoverflow.com # Opens StackOverflow in my browser!
# Opens paths with spaces in them too:
open /c/Program\ Files/Sublime\ Text\ 3/sublime_text.exe
open '/c/Program Files/Sublime Text 3/sublime_text.exe'
To open the current directory via windows subsystem for linux (WSL) use :
$ explorer.exe .
The easiest way is to edit .bash_aliases
nano ~/.bash_aliases
# add
alias open='explorer.exe $1'
# save and close: cltr-s, cltr-x
source ~/.bash_aliases
Now, we can use: open .
or open documents
Opening Windows Terminal in a directory and typing start bash will open a bash terminal in that directory.