Using Linux With WSL on Windows

Using WSL

Access WSL Files in Windows

Learn the essentials about accessing Linux files from 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.

Click on the penguin icon on the left sidebar of the File explorer

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.

Double click on any of the folders to open the root directory of that WSL distribution.

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

Inside the root directory of the distribution
Root directory of WSL distribution

Go inside the home β†’ username folder, and there it is.

Home directory of the default user in Windows file explorer
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.

Open the bashrc file of the WSL distribution using Windows Notepad application
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$
Enter the WSL location in the address bar of Windows file explorer
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.

Click on the folder with the name of the WSL distro on which you want to browse a file
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.

Opening the present directory you are visiting in a WSL distribution using the Windows file explorer.
Open current directory in Explorer

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

Open a particular WSL directory using Windows Explorer by providing the exact path too that directory
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
Open a text file inside a WSL distribution directory using Windows Notepad
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
Files in Windows is edited using the nano editor inside the WSL distribution
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.

Copy a file from WSL to a Windows folder
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/
Copy a file from Windows to WSL folder
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.