Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call Linux commands through WSL in Windows command line?

I would like to use ls, and many other Linux tools, from the command line on Windows 10. I know that there is GnuWin32 and other binary replacements, but none of those works as elegantly as the Windows Subsystem for Linux (WSL); including the update mechanisms.

With WSL, one can call ls ... from the command line via bash -c "ls ...", which works great. Ideally, however, I would just type ls on the command line. I can create an ls.bat which basically does @bash -c "ls %*" - but I would have to do that for every single command I want to export. Is there a more direct way of exporting ls (and many others) to the command line?

like image 603
bers Avatar asked Sep 20 '17 08:09

bers


3 Answers

Apparently this was the most requested feature for WSL, and Microsoft now supports this feature. To use linux commands from within Command Prompt (or PowerShell), just prefix the command with wsl. So, for example, here's how you run ls from CMD.

C:\temp> wsl ls
<- contents of C:\temp ->

Or here's how you update package lists.

C:\temp> wsl sudo apt-get update
[sudo] password for username:
Hit:1 http://archive.ubuntu.com/ubuntu xenial InRelease
Get:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [94.5 kB]

More details can be found in the official docs. I'm not sure if you are looking for a solution which doesn't involve any kind of prefixing. If that's the case, I don't know of any solution yet.

like image 56
nikhilweee Avatar answered Sep 28 '22 22:09

nikhilweee


Microsoft doesn't directly support what you're asking for, however...

  • You can create a "Console Alias" using doskey.exe, but these Console Aliases are not persistent (when you open a new Command Prompt window they're all gone).
  • CMD /? informs us of the following two Registry keys: HKey_Local_Machine\Software\Microsoft\Command Processor\AutoRun and HKey_Current_User\Software\Microsoft\Command Processor\AutoRun, which (as long as the /D option wasn't specified) will both be checked for contents to run (in that order) when a new Command Prompt window is opened.

If we put these two things together with a FOR loop, and a nice, easily customizable file in your user folder, we can get a pretty close approximation of what I think you want.
I'm going to put this in HKLM, and reference a file in %USERPROFILE%, called exportlinuxcommands, so that it's available to all users, but easily customizable on a per-user basis, as well as only functional for users who set it up. If you only want it for one user, or don't want it to be customizable on a per-user basis, you'll have to modify the instructions a bit.

  1. Open the Registry Editor
  2. Go to HKey_Local_Machine\Software\Microsoft\Command Processor
  3. Right-click on an empty portion of the window, and choose "New" > "String Value" (or "Expandable String Value", but the expansions seem a little pointless when you realize that the Command Processor will expand them after it reads the value anyway.)
  4. Name it "AutoRun" (without the quotes)
  5. Double-click on the new value you just created
  6. Enter IF EXIST %USERPROFILE%\exportlinuxcommands FOR /F %i in (%USERPROFILE%\exportlinuxcommands) DO @doskey %i=bash -c "%i $*" as the "Value data".

Now you just need to make a file in your %USERPROFILE% folder (i.e. C:\Users\YourUserName) called exportlinuxcommands, with each Linux command you want an alias to on a separate line. For example:

ls
rm
cat

Want to remove one? Remove it from the file, and it won't be there in the next Command Prompt window you open.
Want to add another later? Just add it to the file on it's own line, and the next time you open up a Command Prompt window, it'll be there.

like image 31
3D1T0R Avatar answered Sep 28 '22 21:09

3D1T0R


Try to use windowsBash

Run the following command inside Windows Bash to generate shortcuts for commands

wget https://raw.githubusercontent.com/aleppos/windowsBash/master/windowsBash -P /usr/bin && chmod 0777 /usr/bin/windowsBash && windowsBash

Then just add the following directory to your path in Windows * C:\windowsBash

When you want to update the commands list just run the Shell file again

windowsBash

like image 37
Emad Hajjar Avatar answered Sep 28 '22 20:09

Emad Hajjar