How to Mix And Match Windows and Linux With WSL


Summary

  • You can access Linux and Windows files through Windows Explorer and WSL Linux shell windows.
  • You can run Windows commands from Linux by simply appending “.exe” to them.
  • You can run Linux commands from Windows PowerShell using the “wsl” command.

Windows Subsystem for Linux, or WSL, is a way to run Linux distributions in Windows without dual-booting or installing virtual machines, but its real power comes from the ability to launch Windows and Linux apps simultaneously.

Accessing Linux Files from Windows Explorer

Ubuntu WSL root directory in Windows Explorer.

You can access files for any Linux distros installed with WSL in the Windows Explorer. Look on the left-hand sidebar and you’ll see an icon of Tux, the Linux mascot, and then click the triangle to expand it to see all the WSL distros you’ve installed. You’ll then be able to navigate the Linux filesystem through Explorer.

You can also enter “\\wsl$” in Explorer to see your distros. You can also navigate directly to your distro by appending a \ (backslash) and the name of the distro. For example, Ubuntu would be “\\$wsl\ubuntu\”. You can also specify pathnames this way. Just remember to change the forward slashes (/) to backslashes since this is how Windows separates paths.

When you’re working in the WSL command line, you can open up Explorer in the current working directory with this command:

        explorer.exe .
    

(You can also do this from PowerShell, but this shows how well WSL and PowerShell interoperate).

Accessing Windows Files from Linux

Running ls command on /mnt/c in WSL.

You can also access Windows files from the Linux side of your machine. The letter drives on Windows will show up as /mnt/[drive letter] in the Linux file system.

For example, the “C:\” drive will be mounted at “/mnt/c”. The C:\Windows directory would be “/mnt/c/Windows”.

This will also apply to any graphical Linux apps you have installed.

Running Windows Commands from Linux

Running Notepad from WSL

To run Windows commands from Linux, you can just type them from the Linux terminal and append “.exe” to them. You already saw an example with Explorer earlier.

You can also run other commands. If you wanted to open a file in Notepad, you can type this command:

        notepad.exe file
    

You can also run Windows commands in Linux pipelines, processing the output of Windows commands. You can search Windows output with the Linux grep command.

Suppose we wanted to find mention of Windows in a directory listing from PowerShell:

        
powershell.exe dir "C:/" | grep 'Windows'

If you’re mixing Windows and Linux commands, bear in mind that while Windows commands are case-insensitive, Linux commands are case-sensitive. This means that Linux commands depend on having the right combination of upper-case and lower-case letters to run correctly.

When you run Windows commands from Linux, they will have the same Windows permissions as the WSL process and the active user. If you run ls /mnt/c, you’ll notice that the permission to list files like pagefile.sys, the Windows pagefile, is denied, as that’s usually reserved for administrative users. If you use sudo, you’ll be able to list all files, as the WSL will now have full permissions:

        sudo ls /mnt/c
    

Running Linux Commands from PowerShell

Running WSL Ubuntu Linux ls command from Windows PowerShell.

You can also go the other way by running Linux commands from Windows PowerShell. You can do this using the wsl command followed by any Linux commands you want to run.

To list the “/bin” directory in Linux:

        
wsl ls /bin

Going from Windows to Linux, the processes will have the same permissions as the session that started the Linux command. For example, a command issued by a regular user will have the same permissions, while a command issued with sudo will be escalated to superuser.

Running Linux GUI Programs in Windows

GNU Emacs launched from WSL

With WSL on Windows 11, you can run Linux GUI programs from Windows. There are two ways to do this: from the command line or from the Start menu.

To launch a GUI program from the shell, you can just invoke it from the command line. To run GNU Emacs:

        
emacs

One problem you may noticed when you run a command this way is that it can tie up your terminal. You won’t get your prompt back unless you either close the program or put in the background. To launch a program as a background process, append an & (ampersand) to it:

        
emacs &

To put an already running program in the background, use the shell’s job control functions. Press Ctrl+z to suspend the program. This is lowercase, since keyboard combinations are case-sensitive like Linux commands are. The system will print a message with the job number and a message that the process is suspended. Then type this command to put it in the background:

        
bg
Ubuntu WSL GUI in Windows 11 Start menu.

You can also access GUI programs from the Start menu. Any Linux distros will show up as a folder in the Start menu if you have any GUI programs installed. For example, Ubuntu will be “Ubuntu.” To start a program, you can click on it to launch it as you would with a regular Windows program.



Source link

Previous articleBitcoin Price Nears All-Time High Above $103K as Dogecoin and XRP Rise
Next articleBitcoin, Ethereum Price in Red Amid $252 Market Sell-Off, What Indicators Say