Rabu, 03 Juli 2024

Access WSL Files in Windows

Access WSL Files in Windows

Among its numerous advantages, one notable feature is the seamless accessibility of Linux files stored within WSL from the Windows operating system itself.

In this article, you'll explore various methods of accessing WSL files. This will enable smooth file management and interoperability between Windows and Linux environments.

You will look at both graphical and command-line ways of accessing WSL files inside Windows.

Access WSL files using Windows Explorer

The most basic way of accessing a file on Windows is by launching the Windows File Explorer. The file manager is present on the taskbar, and you can just open it to browse files. Wait! That is for Windows files, but for Linux?

Yes. WSL indeed stores the file inside Windows because it is the host. If you have set up the WSL correctly on Windows, you will see a Penguin icon on your File Explorer.

Access WSL Files in Windows

Just click on it.

You will see the name of the WSL distributions you have installed currently as folders. I have two; an Ubuntu and an OpenSUSE Tumbleweeed.

Access WSL Files in Windows

Double-click on the one you need to access. Now, you are inside the root directory of that system.

Access WSL Files in Windows
Root directory of WSL distribution

Go inside the home → username folder, and there it is.

Access WSL Files in Windows
Home directory of default user

You are now accessing the home directory of the default user. Double-click on any file to open it on a Windows app. You can see in the screenshot below, I have opened the .bashrc file on Notepad app.

Access WSL Files in Windows
Open .bashrc file in Notepad

Use the address bar of Explorer

You can use the address bar of Windows Explorer to go into the WSL folders. For this, click on the to address bar. When it is changed to editing, enter:

\\wsl$
Access WSL Files in Windows
Enter WSL location in the Address bar

This will bring you to the same folder, where all the WSL distributions are listed as folders. Click on any to enter into it.

Access WSL Files in Windows
Click on a Distribution Name

💡 Tip: Open the current Linux directory location in Windows

Imagine that you are using Linux through the Windows terminal and you are in a specific directory location. Now, you think it would be better if you could access it in the file explorer.

This is possible and quite easy. Enter the following command on the WSL terminal.

explorer.exe .

Here, the . refers to the current directory you are in. This means that your current directory will be opened in the Windows file explorer.

Access WSL Files in Windows
Open current directory in Explorer

Similarly, while inside the WSL, you can specify a directory path to open that folder in Windows Explorer.

Access WSL Files in Windows
Open a directory by specifying the path

As you can see, the mentioned directory is opened in Windows Explorer and you can access the files.

Open a WSL text file in Notepad

While there are terminal-based text editors available in Linux, you may not be comfortable with the idea of editing files in the command line.

Thankfully, you can easily open those Linux text files using Windows's Notepad.

notepad.exe /path/to/the/file/inside/wsl/distribution
Access WSL Files in Windows
Open a WSL file using Notepad

Accessing files between WSL and Windows

As you can easily guess by now, it is possible to access files between WSL and the Windows host operating system.

Here are a few tips on accessing files between Windows and Linux inside WSL.

If you are working on a project inside WSL, be sure to store the files inside the respective WSL system. Similarly, store all the files inside the Windows filesystem if you are working on a project using Windows, you can ensure maximum speed and performance.

Edit Windows files inside WSL

While inside WSL, you can open a file stored on the Windows filesystem using Linux tools like the Nano editor.

For this, we use the Mounted Drive concept.

nano /mnt/c/Users/username/Documents/filename
Access WSL Files in Windows
Edit a Windows file inside WSL

Here, the /mnt/c represents the C drive mounted to the WSL. So, what the command actually does is, open the specified file on the location specified in nano editor.

You can use /mnt/d, if you have a drive located with D: in Windows.

💡
If you are using Windows files from WSL, then read more about their permission settings from the official Microsoft guide.

Copy files between WSL and Windows

Inside the WSL, you can copy a file from WSL to a specified Windows folder. To do that, use the cp command:

cp /home/$USER/filename /mnt/d/Documents/

This will copy the file called filename from the home directory of the user to the Documents folder (if exists) on the D drive.

Access WSL Files in Windows
Copy a file from WSL to a Windows folder

Similarly, if you want to transfer a file from a Windows folder into the WSL system, use:

cp /mnt/d/Documents/filename /home/$USER/
Access WSL Files in Windows
Copy a file from Windows to WSL folder

That's it. You have copied the files.

🚧
Keep caution while managing files between the two operating systems. It can lead to file loses and corruption, if not handled properly.

Wrapping Up

Here, you learned about accessing files between Windows and Linux running inside WSL. The ability to access Linux files easily from Windows is super helpful for Windows users. Let me know if you have any questions.



from It's FOSS https://ift.tt/16iANmX
via IFTTT

Tidak ada komentar:

Posting Komentar