Start WSL Ubuntu in specific or current folder on Windows

WindowsWindows Subsystem-for-Linux

Windows Problem Overview


When installing Subsystem for Linux and Ubuntu from store on his development machine I can switch (or start) to Ubuntu shell by simply

enter image description here

But the Ubuntu shell start in /home/techsupp folder by default. Is it possible to force it to start in same folder than the one I use my Ubuntu command?

So in my example I should be in

/mnt/h

Thank you.

What I already tried:

H:\>ubuntu help
Launches or configures a linux distribution.

Usage:
    <no args>
      - Launches the distro's default behavior. By default, this launches your default shell.

    run <command line>
      - Run the given command line in that distro, using the default configuration.
      - Everything after `run ` is passed to the linux LaunchProcess call.

    config [setting [value]]
      - Configure certain settings for this distro.
      - Settings are any of the following (by default)
        - `--default-user <username>`: Set the default user for this distro to <username>

    clean
      - Uninstalls the distro. The appx remains on your machine. This can be
        useful for "factory resetting" your instance. This removes the linux
        filesystem from the disk, but not the app from your PC, so you don't
        need to redownload the entire tar.gz again.

    help
      - Print this usage message.

I also discover this request on uservoice: https://wpdev.uservoice.com/forums/266908-command-prompt-console-windows-subsystem-for-l/suggestions/13421103-let-us-right-click-open-bash-here-from-explorer?tracking_code=8a8bc624c72a8336565fcd6d5737d712

Please vote for it.

Windows Solutions


Solution 1 - Windows

I'm on Windows 10 Home with May Update and have Ubuntu 18.04 for WSL installed, I can open the console in any folder with Shift + Right Click and selecting the Open Linux shell here option

Solution 2 - Windows

If you check in Task Manager how the explorer "Open Linux shell here" option opens wsl, you can see that there's a "--cd" option.

Solution 3 - Windows

ubuntu run

> run <command line>
>    Run the provided command line in the current working directory. If no
>    command line is provided, the default shell is launched.

As far as I can tell the effect is the same as running ubuntu, except that it starts in the current directory.

This command works even as a right click open here command in explorer. Adapting from this article:

  • RegEdit to HKEY_CLASSES_ROOT\Directory\Background\shell and create a new key
  • edit (Default) to "Open Ubuntu here" or similar.
  • add a key named command
  • edit (Default) to ubuntu run

Now you have a functional "Open Ubuntu here" right click menu in explorer.


The problem I have with wsl is that the wsl bash window doesn't have the Ubuntu icon, even if it starts the Ubuntu distribution.

Solution 4 - Windows

If you use wsl.exe, it will start WSL in current directory. But the difference between wsl and ubuntu is that if you have installed two or more distros (e,g, Ubuntu and Fedora), the default one will get started. You can set the default to Ubuntu by running wslconfig /setdefault Ubuntu.

Reference: [1] https://docs.microsoft.com/en-us/windows/wsl/wsl-config

Solution 5 - Windows

seems that there is a very simpel solution.

just add the following in Ubuntu configuration in the profiles.json file

"commandline": "wsl ~ -d Ubuntu",

Solution 6 - Windows

The method that has worked for me is to use the Windows Terminal app and configure it to start in the desired location.

Example based on WSL2 and Ubuntu 20.04:

settings.json - Add this line to "Ubuntu-20.04" section:

	"startingDirectory": "//wsl$/Ubuntu-20.04/home/<username>/"

Change <username> to be the name of the user you created on install of Ubuntu 20.04.

Solution 7 - Windows

Modern solution

  1. Install windows terminal , from Microsoft store

  2. Go on settings enter image description here

  3. Once you have ubuntu installed you will see it on the list of profiles and then click on open json enter image description here

  4. Open cmd , type wsl , type cd, type pwdwslc then copy the path [! enter image description here

  5. And put it in the json file, on ubuntu profile, startingDirectory enter image description here

Note: I have used linux home directory as default directory used by ubuntu. So if you feel like you have other directory you want to use you can replace it with /home/claranceliberi to your own prefereble directory

Solution 8 - Windows

Step 1: Install Windows Terminal

Step 2: Go to the folder you want to open ubuntu in and open Windows Terminal by right-clicking.

Step 3: Go to Windows Terminal Settings and navigate to your Linux distro (in my case Ubuntu 20.04) and check this option.

enter image description here

Now every time you open ubuntu through the Windows terminal it will open in that directory.

Solution 9 - Windows

with this little script you can execute every command from the directory your Powershell is currently in. It only works if the drive is already mounted to /mnt/ in the subsystem. It doesn't work with network resources.

function tux {$dl = (pwd).Path | wsl eval "cut -c 1 | tr 'A-Z' 'a-z'"; $wd = (pwd).Path | wsl eval "cut -d : -f 2 | tr '\\' '/' 2> /dev/null" ; wsl eval "cd '/mnt/$dl$wd' && eval '$args'"}

after you have executed it, you can use it like this:

tux vim test.txt

Solution 10 - Windows

First, you have to check whether wsl is using ubuntu as default or not. For me it was not. Write the below command on cmd:

wsl -l

Like this

If not then Write wsl -s Ubuntu-20.04 to set it as default. Now you can run ubuntu bash from any directory by doing shift + Rightclick as shown Here.

Solution 11 - Windows

if your default terminal is Ubuntu, then you can open Ubuntu in the current dir easily.

Go to the address bar of the file explorer by clicking on it enter image description here

or you can use keyboard shortcut alt+d and

there type

wt -d .

and if the default is Ubuntu, it will open Ubuntu on that path or you can also type

ubuntu run 

and it doesn't matter if the default terminal is Ubuntu or not, it will open ubuntu there regardless of anything.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBastien VandammeView Question on Stackoverflow
Solution 1 - WindowsEmmanuel GuerraView Answer on Stackoverflow
Solution 2 - WindowstallesView Answer on Stackoverflow
Solution 3 - WindowsbolovView Answer on Stackoverflow
Solution 4 - WindowsX. LiuView Answer on Stackoverflow
Solution 5 - WindowsMartin VangView Answer on Stackoverflow
Solution 6 - WindowsJubilsoft-ScottView Answer on Stackoverflow
Solution 7 - WindowsNtwari Clarance LiberisteView Answer on Stackoverflow
Solution 8 - WindowsShreyas VedpathakView Answer on Stackoverflow
Solution 9 - Windowspt1997View Answer on Stackoverflow
Solution 10 - WindowsJogendraView Answer on Stackoverflow
Solution 11 - WindowsKoushik DasView Answer on Stackoverflow