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.
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).
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.
I entered cd /mnt/c/ and then ls which showed the C: directory. Some of the files gave me a permission denied message.
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.
Accessing Windows Files from Linux
I had some bash scripts stored within a directory in Windows. I navigated to the directory by entering:
I could see all of the scripts which had a txt extension.
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.
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
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.
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.
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.
Tom Fenton has a wealth of hands-on IT experience gained over the past 25 years in a variety of technologies, with the past 15 years focusing on virtualization and storage. He currently works as a Technical Marketing Manager for ControlUp. He previously worked at VMware as a Senior Course Developer, Solutions Engineer, and in the Competitive Marketing group. 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 Twitter @vDoppler.