How to change default directory in Bash for Windows 10? [closed]

Solution 1:

If you want change the directory your bash prompt is starting in, you can edit your .bashrc file. At the bottom, add:

cd ~

This will go into your home directory. (you can actually do just cd, but I it's clearer to add the ~ IMO)


To edit, you can use vim. If you don't know how to use it, you can always use nano for the time being, but you really should have a look at it, it's really powerful.

$ nano ~/.bashrc

This will open nano in "full console". At the bottom, you have the few commands you can use (^ means control) Do your changes, hit ctrl+o to save the file (write the file). It'll ask you where to write, by default, it's the right location, just hit enter and the .bashrc file will be saved. Then, you can press ctrl+x to exit.

Solution 2:

Steps to set default directory for Bash on Ubuntu on Windows to a folder -

  1. Open Bash on Ubuntu on Windows.
  2. cd ~ to go to home directory of Ubuntu
  3. Type edit .bashrc and enter at the Bash. This will open the file in vim.
  4. Use Down Arrow or Page Down key on keyboard to go to the end of the file (there's a helpful progress bar at the bottom right corner of the Bash). At the end of this file, you will find cd ~, replace cd ~ with your desired location.
  5. Save the .bashrc file. To save the file, click esc and then type :wq and click enter.

Note:

  • To access your hard disk location make sure you include the mount directory first.
  • So if want your Bash to open at C:\dev whenever you open the Bash. You need to replace the cd ~ with cd /mnt/c/dev at .bashrc file at Ubuntu home directory.

Solution 3:

Just enter echo "cd ~" >> ~/.bashrc. This will append "cd ~" to your .bashrc.

.bashrc is executed everytime you start a(n interactive) bash instance.