Skip to content
Home » Piping Windows and Linux Commands with WSL

Piping Windows and Linux Commands with WSL

Introduction to piping and how it works:

Piping is a way to redirect the output of one command to the input of another command, using the “|” symbol. For example:

command1 | command2

This will take the output of command1 and use it as the input for command2. This allows you to chain together multiple commands and create more powerful scripts and command-line utilities.

Here is a simple example of piping in action:

$ ls -l | grep ".txt"
-rw-r--r-- 1 user group 0 Jan 1 00:00 file1.txt
-rw-r--r-- 1 user group 0 Jan 1 00:00 file2.txt
-rw-r--r-- 1 user group 0 Jan 1 00:00 file3.txt

In this example, the ls -l command is used to list the files in the current directory, and the grep ".txt" command is used to filter the output and only show lines that contain “.txt”.


Running Windows commands in a Linux environment using WSL

To run Windows commands in a Linux environment, you can use WSL (Windows Subsystem for Linux).

WSL is a feature of Windows 10 that allows you to run native Linux command-line tools directly on Windows, alongside your traditional Windows desktop and modern store apps. To install WSL, follow these steps:

  1. Open the Start menu and search for “Turn Windows features on or off”.
  2. In the “Windows Features” dialog, scroll down and check the box next to “Windows Subsystem for Linux”.
  3. Click “OK” and restart your computer when prompted.

Once WSL is installed, you can use the Linux terminal to run Windows commands. For example:

$ wsl dir
 Volume in drive C is Windows
 Volume Serial Number is 0123-4567

 Directory of C:\\

01/01/2000  12:00 AM    <DIR>          .
01/01/2000  12:00 AM    <DIR>          ..
01/01/2000  12:00 AM                 0 file1.txt
01/01/2000  12:00 AM                 0 file2.txt
01/01/2000  12:00 AM                 0 file3.txt
               3 File(s)              0 bytes
               2 Dir(s)  123,456,789,012 bytes free

Examples

$ wsl dir | grep ".txt"
01/01/2000  12:00 AM                 0 file1.txt
01/01/2000  12:00 AM                 0 file2.txt
01/01/2000  12:00 AM                 0 file3.txt

In this example, the dir command is used to list the files in the current directory, and the grep ".txt" command is used to filter the output and only show lines that contain “.txt”.

$ wsl type file1.txt | grep "text"
This is some sample text in file1.txt

In this example, the type command is used to display the contents of file1.txt, and the grep "text" command is used to filter the output and only show lines that contain “text”.

$ wsl powershell Get-Process | grep "chrome"

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    667      45    13104      10328  105     1.49   8872 chrome
   1075      60    17764      14156  139     3.12  11564 chrome
    656      44    14656      11280  122     1.78  12656 chrome

In this example, the powershell Get-Process command is used to list all running processes, and the grep "chrome" command is used to filter the output and only show processes with “chrome” in the name.

$ wsl powershell Get-Content C:\\file1.txt | grep "text"
This is some sample text in file1.txt

In this example, the powershell Get-Content C:\\file1.txt command is used to display the contents of file1.txt, and the grep "text" command is used to filter the output and only show lines that contain “text”.

You can also use the | symbol directly in a PowerShell command by enclosing the entire command in quotes, like this:

$ wsl "powershell Get-Content C:\\file1.txt | grep 'text'"
This is some sample text in file1.txt

This can be useful if you need to pass arguments to a command that contain spaces or other special characters.

Leave a Reply

Your email address will not be published. Required fields are marked *

9 + 15 =