How-To

How to Access Files on Windows Subsystem for Linux

Tom installs and uses Edge DX to monitor his WSL Linux instance and explains how to access files from and to Windows systems.

Windows Subsystem for Linux (WSL) allows you to run Linux on Windows without having to install a hypervisor and run a Linux instance in a virtual machine (VM). To enable WSL and install Linux on your Windows system, you only need to enter a few commands in the command line and reboot your system -- that's all there is to it.

Having this integration with Windows allows you to access the rich set of the tools that Linux offers. However, Windows also has a rich set of tools, and in fact many tools for manipulating and presenting data will only run on Windows. In this article, I will l take a look at how easy it is to read and write files to and from WSL and the Windows system on which it runs.

If you're interested in learning how to install WSL using the command line and then access files from the base Windows system, be sure to check out my previous article here. I've also written extensively on WSL in the past, including articles you can find here and here.

Third-Party Monitoring
Before looking at files on WSL, I wanted to see if third-party software could work with it and, if so, what type of resources (for example, CPU, RAM and so on) it would consume. As I work for ControlUp, I was also curious as to whether or not Edge DX, their endpoint monitoring tool, would work with it. I was able to install the Edge DX user-mode agent without any difficulty as well as the Ubuntu WSL instance. You can read more about Edge DX in this series of articles.

The WSL instance was presented on the Edge DX portal without any issues. I did notice that the instance had ~ 3GB less (13GB) RAM than the Windows system (16GB), but it had the same number of CPU cores (4).

[Click on image for larger view.]

Using the Filesystem
Files are presented from/to the WSL Linux system in different ways depending on the distribution that you are using. The examples below are with Ubuntu, and if you are using a different distribution your results may differ.

One of the nice features is the WSL Ubuntu automatically mounts the C: drive under /mnt/c. I saw what filesystems were mounted and what type of filesystems they were by entering mount. This showed a lot of filesystems with the C:\ being of type 9p. 9p is an interesting way to mount the C: drive and it would take an entire blog to dissect its nuances; for the purposes of this article, however, you only need to know that it works.

[Click on image for larger view.]

I entered cd /mnt/c/ and then ls which showed the C: directory. Some of the files gave me a permission denied message.

[Click on image for larger view.]

The df command reports on filesystem disk space usage. To see how large the filesystem was and how much free space was left on it, I entered df - h /mnt/c. This jived with what I saw on my Windows system.

[Click on image for larger view.]

Accessing Windows Files from Linux
I had some bash scripts stored within a directory in Windows. I navigated to the directory by entering:

cd /mnt/c/Users/Fenton/Dropbox/ControlUp/CUp_2022/CUp_2022_05/EDx_Scripts.

I could see all of the scripts which had a txt extension.

[Click on image for larger view.]

When I tried to execute one of the scripts, I got the following error message:

/bin/bash^M: bad interpreter: No such file or directory

The ^M at the end of the path indicated to me that Windows put a carriage return at the end of the line.

I verified this by entering cat -v Linux_List_Printers.txt.

[Click on image for larger view.]

To correct the issue, I brought the script up in Notepad++ and saved it with only a line feed, and then I was able to execute it without any issues. As I now had Linux on the system, I found that I could also remove the CR by entering either of the following:

sed 's/\r$//' file.txt > file.sh

or

tr -d '\r' < file.txt > file.sh

Accessing Linux Files from Windows
I had no idea where my Linux files were presented to Windows, but since I know that many Windows commands are accessible from WSL, I entered explorer.exe on the WSL command line and, somewhat to my surprise, File Explorer did not launch. However, after I exited out of my Linux root user and re-entered the command, I was in the user's home directory: \\wsl$\Ubuntu\home\user01.

[Click on image for larger view.]

I noticed that the path to the directory had a \\wsl$ prefix, so I brought up another File Explorer and entered \\wsl$ in the path window. This brought up the Ubuntu folder, and I could then navigate to my Linux files.

[Click on image for larger view.]

In this article, I installed and used Edge DX to monitor my WSL Linux instance and looked at how to access files from and to Windows systems. In previous releases of Windows, I had jump through some pretty big hoops to get a graphical interface to work with WSL. I will look and see if any improvements have been made with the latest release of WSL.

About the Author

Tom Fenton has a wealth of hands-on IT experience gained over the past 30 years in a variety of technologies, with the past 20 years focusing on virtualization and storage. He currently works as a Technical Marketing Manager for ControlUp. He previously worked at VMware in Staff and Senior level positions. He has also worked as a Senior Validation Engineer with The Taneja Group, where he headed the Validation Service Lab and was instrumental in starting up its vSphere Virtual Volumes practice. He's on X @vDoppler.

Featured

Subscribe on YouTube