Can I change directory to a Windows drive in Ubuntu Bash on WSL?
Solution 1
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
Solution 2
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
.
Solution 3
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() { local d=`wslpath "$1"`; cd "$d"; }
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.
Changelog: June 11, 2022 - Improve the cdw
function to make it work for space-containing paths such as C:\Program Files
.
Solution 4
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.
Solution 5
You can try doing this :
cd E:/username/folder1/folder2/
and then run
python python.py
. It always works in my case on Windows machine running bash.
Related videos on Youtube
StephanieCoding
Updated on September 18, 2022Comments
-
StephanieCoding over 1 year
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 isbash: 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 saysVIRTUALENV_DIR
.cd
, it says/home/username
. Thecd ..
etc changes the directory a little, but not another drive, nor the folder I would like it to find. -
dufte over 7 yearsSure you want to
cd
to a file? looks likecd /mnt/e/username/folder1/folder2/
makes more sense -
Kaz Wolfe over 7 years@dufte Good catch. Yay for copy-paste!
-
solfish over 6 yearsfor ubuntu 16.04, it is inside of /media folder
-
AnotherLongUsername about 4 yearsThis is the neatest answer here! Thanks
-
NotTheDr01ds about 3 yearsWow. Usually when I see a question this old with 5 answers already, and a new user comes along with an answer, it's pretty bad. But not this time! Bravo for providing an answer that actually contributes something useful that was not covered in the others!