Software & AppsOperating SystemLinux

How To Set Default Directory in Ubuntu on Windows Terminal

Ubuntu 3

In this article, we will explore how to set the default directory in Ubuntu on Windows Terminal. This can be particularly useful if you frequently work within a specific directory and want to avoid navigating to it manually each time you launch the terminal.

Quick Answer

To set the default directory in Ubuntu on Windows Terminal, you can either modify the command line in the Windows Terminal settings or add a cd ~ command to the .bashrc or .profile file in WSL.

Understanding the Default Directory

By default, when you open a new terminal window in Ubuntu on Windows Terminal, you start in the root directory (/). However, you might prefer to start in your home directory (/home/username) or another directory of your choice.

Method 1: Modify the Command Line in Windows Terminal Settings

One way to change the default directory is by modifying the command line in the Windows Terminal settings. Here’s how to do it:

  1. Open the Windows Terminal settings by pressing Ctrl + ,. This will open the settings.json file in your default text editor.
  2. Locate the Ubuntu profile in the profiles section. This section contains a list of all terminal profiles, including Ubuntu.
  3. Change the commandline value to wsl.exe ~ -d Ubuntu. Here, wsl.exe is the Windows Subsystem for Linux executable, ~ is a shorthand for the home directory, and -d Ubuntu specifies the distribution.
  4. Save the settings file and close your text editor.
  5. Restart Windows Terminal to apply the changes.

Now, every time you open a new Ubuntu terminal, you will start in your home directory.

Method 2: Modify the .bashrc File

Another way to change the default directory is by adding a cd ~ command to the .bashrc file. This file is a script that runs every time you open a new terminal. Here’s how to do it:

  1. Open WSL and type nano ~/.bashrc to open the .bashrc file in the nano text editor.
  2. Add the following line at the bottom of the file: cd ~. This command changes the current directory to your home directory.
  3. Save and close the file by pressing Ctrl + X, then Y to confirm saving changes, and finally Enter to exit.
  4. Restart WSL to apply the changes.

Method 3: Modify the .profile File

If you prefer, you can add the cd ~ command to the .profile file instead. This file is similar to .bashrc, but it runs when you log in, not when you open a new terminal. Here’s how to do it:

  1. Open WSL and type nano ~/.profile to open the .profile file in the nano text editor.
  2. Add the following line at the bottom of the file: cd ~.
  3. Save and close the file by pressing Ctrl + X, then Y to confirm saving changes, and finally Enter to exit.
  4. Restart WSL to apply the changes.

Conclusion

Setting the default directory in Ubuntu on Windows Terminal can save you time and make your workflow more efficient. Whether you choose to modify the Windows Terminal settings, the .bashrc file, or the .profile file, the process is straightforward and quick. Choose the method that best fits your needs and happy coding!

How do I open the Windows Terminal settings?

To open the Windows Terminal settings, press Ctrl + , on your keyboard. This will open the settings.json file in your default text editor.

What is the purpose of the Ubuntu profile in the Windows Terminal settings?

The Ubuntu profile in the Windows Terminal settings allows you to customize the behavior and appearance of the Ubuntu terminal. You can modify settings such as the command line, font, color scheme, and more.

How do I change the default directory in Ubuntu on Windows Terminal to my home directory?

There are two methods to change the default directory to your home directory. The first method is by modifying the command line in the Windows Terminal settings. Locate the Ubuntu profile in the settings.json file and change the commandline value to wsl.exe ~ -d Ubuntu. The second method is by adding a cd ~ command to the .bashrc file or the .profile file.

What is the purpose of the .bashrc file?

The .bashrc file is a script that runs every time you open a new terminal. It allows you to customize your shell environment by adding aliases, setting environment variables, and defining functions.

Leave a Comment

Your email address will not be published. Required fields are marked *