Software & AppsOperating SystemLinux

Changing Directory to a Windows Drive in Ubuntu Bash on WSL

Ubuntu 10

In this article, we will delve into the process of changing the directory to a Windows drive in Ubuntu Bash on Windows Subsystem for Linux (WSL). This is a useful technique for developers and system administrators who work with both Windows and Linux environments.

Quick Answer

To change the directory to a Windows drive in Ubuntu Bash on WSL, you can use the cd command followed by the path in the /mnt directory. For example, to navigate to E:\username\folder1\folder2, you would use the command cd /mnt/e/username/folder1/folder2. Additionally, you can use the wslpath utility to convert Windows paths to their WSL counterparts and change the directory. Another option is to open Bash from Windows Explorer and navigate to the desired folder.

Understanding WSL

WSL is a compatibility layer for running Linux binary executables natively on Windows 10 and Windows Server 2019. It allows developers to use a GNU/Linux environment — including most command-line tools, utilities, and applications — directly on Windows, unmodified, without the overhead of a traditional virtual machine or dual-boot setup.

Accessing Windows Drives in WSL

In WSL, all the Windows drives are automatically mounted under the /mnt directory. Each drive is represented as a subfolder with the name of the drive letter. For instance, the C:\ drive in Windows can be accessed in WSL at /mnt/c/.

To navigate to a specific folder on a Windows drive, you can use the cd command followed by the path in the /mnt directory. The cd command stands for “change directory” and is used to change the current directory in Linux and UNIX systems.

Here’s an example:

cd /mnt/e/username/folder1/folder2

This command will change the current directory to folder2 which is located at E:\username\folder1\ in Windows.

Using wslpath Utility

WSL provides a utility called wslpath that can convert Windows paths to their WSL counterparts. This utility is available in both WSL and WSL2.

Here’s how you can use wslpath:

cd $(wslpath 'E:\username\folder1\folder2')

This command will convert the Windows path E:\username\folder1\folder2 to a WSL path and then change the current directory to that path.

You can also define a function in your ~/.bashrc file to quickly change the directory to Windows paths. The ~/.bashrc file is a shell script that Bash runs whenever it is started interactively.

Here’s an example of the function definition:

cdw() { local d=`wslpath "$1"`; cd "$d"; }

This function, cdw, takes a Windows path as an argument, converts it to a WSL path using wslpath, and then changes the current directory to that path.

With this function, you can easily change the directory to E:\username\folder1\folder2 by typing:

cdw 'E:\username\folder1\folder2'

Remember to wrap the Windows path in single quotes if it contains backslashes.

Opening Bash from Windows Explorer

Another way to change the directory to a Windows path is by using Windows Explorer. Navigate to the desired folder, type bash or cmd in the address bar, and press Enter. This will open a Bash or Command Prompt window with the path set to the desired folder.

Conclusion

In this article, we have covered various ways to change the directory to a Windows drive in Ubuntu Bash on WSL. Whether you’re using the cd command, the wslpath utility, or opening Bash from Windows Explorer, you have several options to access your Windows files from WSL.

Remember to always keep your system updated and to follow the best practices when working with system files. Happy coding!

How do I access a Windows drive in Ubuntu Bash on WSL?

To access a Windows drive in Ubuntu Bash on WSL, you can navigate to the /mnt directory, where all the Windows drives are automatically mounted. Each drive is represented as a subfolder with the name of the drive letter. For example, the C:\ drive in Windows can be accessed in WSL at /mnt/c/.

How can I change the current directory to a folder on a Windows drive?

To change the current directory to a folder on a Windows drive, you can use the cd command followed by the path in the /mnt directory. For instance, if you want to change the current directory to folder2 located at E:\username\folder1\ in Windows, you can use the command cd /mnt/e/username/folder1/folder2.

Is there a utility available in WSL to convert Windows paths to their WSL counterparts?

Yes, WSL provides a utility called wslpath that can convert Windows paths to their WSL counterparts. You can use the wslpath utility by running the command wslpath followed by the Windows path. For example, wslpath 'E:\username\folder1\folder2' will convert the Windows path to a WSL path.

How can I quickly change the directory to a Windows path in WSL?

You can define a function in your ~/.bashrc file to quickly change the directory to Windows paths. The ~/.bashrc file is a shell script that Bash runs whenever it is started interactively. Here’s an example of the function definition:

cdw() { local d=`wslpath "$1"`; cd "$d"; }

With this function, you can easily change the directory to a Windows path by typing cdw 'E:\username\folder1\folder2'. Remember to wrap the Windows path in single quotes if it contains backslashes.

Can I open a Bash window with the path set to a specific folder in Windows Explorer?

Yes, you can open a Bash window with the path set to a specific folder in Windows Explorer. Simply navigate to the desired folder in Windows Explorer, type bash or cmd in the address bar, and press Enter. This will open a Bash or Command Prompt window with the path set to the desired folder.

Leave a Comment

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