Hostwinds Tutorials

Search results for:


Table of Contents


Getting started with WSL
WSL and Windows File Systems
WSL and Windows du command
Mixing Linux and Windows commands

How to Use Windows Subsystem for Linux (WSL)

Tags: Cloud Servers,  Dedicated Server 

Getting started with WSL
WSL and Windows File Systems
WSL and Windows du command
Mixing Linux and Windows commands

The Windows Subsystem for Linux (WSL) allows the running of Linux binary executables natively on Windows 10 and higher as well as Windows Server 2019 and higher. WSL uses a true Linux kernel through a subset of Hyper-V features. To learn how to install WSL for Windows, see Windows Subsystem for Linux: How to Install & Configure WSL.

In this tutorial, we'll start with some straightforward examples and culminate with using Linux, PowerShell, and DOS (cmd.exe) commands together.

Getting started with WSL

To start, open your installed Linux distribution as follows:

Step 1: Select the Start menu, then Select the Linux distribution you installed - for this example we've installed Ubuntu.

You should now be at the Linux command prompt for your distribution:

One thing to immediately notice is the number of running processes - 12 in this case. This highlights the fact that, as far as Linux is concerned, it's running on its own "machine". Windows, in this case, is actually running 236 processes (including WSL):

This example highlights the fact that you can run PowerShell commands from the WSL Linux environment. In the above, note the quotes around the PowerShell command (not all WSL PowerShell commands require quotes, as in powershell.exe Get-Process). This virtual machine behavior can also be seen by running the Linux top command:

On line four of the above, we see that there's 11911.9 MiB (or legacy MB) of "physical" memory. To determine the actual physical memory of the system, you can run the following Windows commands from within WSL:

systeminfo.exe | finstr.exe

This shows that there's actually 15,289 MB of physical memory:

In other words, some percentage of Windows' physical memory has been allocated to the Linux virtual machine.

Next, we'll examine the WSL Linux and Windows files systems and how they're exposed to each other.

WSL and Windows File Systems

From the WSL, run the following commands:

cd /
ls -lah

For example:

This is a typical Linux root directory listing. However, if we look within the mnt directory, we can see how the Windows file system is exposed through WSL (note commands):

In the above, we see standard Windows folders such as Program Files, Users, Windows, etc.

Now, on the Windows side of things, we can access the WSL file system as follows:

1. Open Windows Explorer and in the address bar, type the network drive \\wsl$, as shown:

Note that the above image implies that you can have multiple Linux distributions installed at the same time, which is indeed the case.

2. Double-click the Linux distribution you installed (Ubuntu in this example). This provides access to the WSL file system:

WSL and Windows du command

Next, lets examine a Linux command that Windows really should have - the du command. In Windows, it can be challenging to sort both files and directories by size. For Linux, this task is relatively straightforward:

du -aSh 2>&1 | sort -n -r | head -n 5

An example follows:

For the above:

  • du -aSh 2>&1 returns file/directory size information, ignore any permission errors.
  • sort -n -r sorts the file/directory list by size, from smallest to largest.
  • head -n 5 returns the top 5 largest files/directories.

Now wouldn't it be nice if Windows could do this? Well, thanks to WSL, it now can. From an elevated Windows command prompt (cmd.exe), run the following:

wsl du -aSh 2>&1 | wsl sort -n -r | wsl head -n 5

This results in output similar to:

Note the wsl preceding each Linux command. This is required to let Windows know that we're running a Linux command (from your installed distribution) and not a malformed Windows command.

Next, let's dig in a little deeper with respect to combining Linux, PowerShell, and DOS (cmd.exe) commands.

Mixing Linux and Windows commands

Here's an example of mixing commands at the WSL Linux command prompt. This combines Linux, PowerShell and DOS:

powershell.exe Get-ChildItem -Recurse -ErrorAction SilentlyContinue | findstr.exe "karlito" | awk '{print $2}'

We first run the Get-ChildItem PowerShell command, then pipe that into the DOS findstr command, and finally pipe that into the Linux awk command. To better understand the command flow, we'll start with representative Get-ChildItem output:

We then use the findstr DOS command to select the output containing "karlito":


Finally, we select the second column using the Linux awk command:

Next, we'll run a similar command from the PowerShell command prompt, as shown:

Get-ChildItem -Recurse -ErrorAction SilentlyContinue | findstr.exe "karlito" | wsl awk '{ print \$6 }'

Again, we'll look at this step-by-step, starting with representative output of Get-ChildItem:

We next find the files/directories containing "karlito:

Note the six columns above (the PM/AM data is considered a column). Because of this, we need to change the awk command as follows:

Observe the \ character before the $6 in the awk command. Because the $ character has special meaning in PowerShell, so we must escape using \, as shown.

The last example runs the same command but from the Windows command prompt (cmd.exe):

Note the use of powershell.exe to run the PowerShell command and wsl to run the Linux command.

To conclude, by using WSL to attach a Linux distribution to Windows, you extend the power and capabilities of Windows as well as having a handy Linux shell at your fingertips. And, hopefully the information presented in this tutorial will improve your command savvy and efficiency.

Written by Karlito Bonnevie  /  June 17, 2022