Can I change directory to a Windows drive in Ubuntu Bash on WSL?
I am a super newbie for Ubuntu and basically need Ubuntu for a Python package.
I would like to run a Python file in say e:/username/folder1/folder2/python.py
. I tried every method online and the only reply is bash: cd./e: No such file or directory
. I am not sure whether it is because of the bash windows or virtualenv.
When I use bash on Ubuntu on Windows, ls
, it says VIRTUALENV_DIR
. cd
, it says /home/username
. The cd ..
etc changes the directory a little, but not another drive, nor the folder I would like it to find.
WSL stores your Windows drives in the /mnt
folder, with the name of the drive as a subfolder. For example your C:\
drive will be present at /mnt/c/
for you to use.
Keeping this in mind, you can swap to your specific folder like so:
cd /mnt/e/username/folder1/folder2
In windows explorer, navigate to e:/username/folder1/folder2/
,
type bash
and press Enter in the address bar, a bash
window with path in e:/username/folder1/folder2/
will appear.
This also work with command cmd
.
Reference
A utility called wslpath
that is shipped with both WSL and WSL2 can translate Windows paths to WSL paths (or translate WSL paths to Windows paths). For example, the command wslpath 'E:\username\folder1\folder2'
gives you the path in WSL /mnt/e/username/folder1/folder2
.
By using the wslpath
utility, I define the following function in my ~/.bashrc
to quickly cd
to Windows paths.
cdw() { cd `wslpath "$1"`; }
With this user defined function, I can easily cd
to E:\username\folder1\folder2
by simply typing
cdw 'E:\username\folder1\folder2'
Remember to wrap the Windows path by single quotes if it contains backslashes.
Yes, you can do that. I used shift + right click to get "Open Powershell here" whichever directory you want to bash and run 'bash' command, then run as usual enjoy. This is same as using Ubuntu bash as it uses same Python packages in the Windows Powershell after using 'bash' command.
Note: On windows 10 latest update maybe April 2018 with WSL and developers mode enabled.