【运维经】第41章——从 Windows 命令行运行 Linux 工具

运维经–目录


从 Windows 命令行运行 Linux 工具

使用wsl.exe <command>从 Windows 命令提示符(CMD 或 PowerShell)运行 Linux 二进制文件。
以这种方式调用二进制文件:

  1. 使用当前 CMD 或 PowerShell提示符中提到的同一工作目录。
  2. 以 WSL 默认用户的身份运行。
  3. 拥有与调用方进程和终端相同的 Windows 管理权限。

例如:

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

wsl.exe 后面的 Linux 命令的处理方式与 WSL 中运行的任何命令的处理方式类似。 可以执行 sudo、管道处理和文件重定向等操作。
使用 sudo 的示例:

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

混合 WSL 和 Windows 命令的示例:

C:\temp> wsl ls -la | findstr "foo"
-rwxrwxrwx 1 root root     14 Sep 27 14:26 foo.bat

C:\temp> dir | wsl grep foo
09/27/2016  02:26 PM                14 foo.bat

C:\temp> wsl ls -la > out.txt

传入 wsl.exe 的命令将按原样转发到 WSL 进程。 文件路径必须以 WSL 格式指定。
使用路径的示例:

C:\temp> wsl ls -la /proc/cpuinfo
-r--r--r-- 1 root root 0 Sep 28 11:28 /proc/cpuinfo

C:\temp> wsl ls -la "/mnt/c/Program Files"
<- contents of C:\Program Files ->
发布了82 篇原创文章 · 获赞 14 · 访问量 9147

猜你喜欢

转载自blog.csdn.net/xk_xx/article/details/104650833