使用 ChatGPT AI 从英文文本生成 Linux 命令
Linux 命令 AI
![](/data/attachment/album/202304/29/111749dhzuflynyxw8w8uu.jpg)


即使是专家级的 Linux 用户也不记得所有的 Linux 命令和它们的选项。这对我们人类来说是不可能的。


但是机器呢？尤其是人工智能驱动的机器？


想象一下，如果你可以“命令”你的终端“显示过去 12 小时内修改过的所有小于 100 MB 的文件”。当然，你可以使用 Linux 命令“命令”它，但是用普通的英语进行交互呢？


由于人工智能的进步，这实际上是可能的。下面是自动生成 Linux 命令以显示当前目录中所有小于 10 KB 的文件的示例。


![Shell Genie AI assistent in Linux terminal](/data/attachment/album/202304/29/111820uq7cbz0cji67c0zq.png)


我使用的工具叫做 [Shell Genie](https://github.com/dylanjcastillo/shell-genie?ref=itsfoss.com)。它是一个命令行工具，可让你以普通的英语与终端进行交互。


它可以生成命令、运行命令（如果需要），还可以向你解释生成的命令。


![Shell Genie explain commands](/data/attachment/album/202304/29/111820dsvy19qqswnkhhqi.png)


### Shell-Genie 的特点


* 将普通英语转换为 Linux 命令。
* 提供了一个需要 openAI 的 API 密钥的 openAI gpt3 后端，和一个可以免费使用的 free-genie 后端。
* 提示一个选项以运行你要求的命令。
* 解释生成的命令。


### 安装 Shell Genie


Shell-genie 在任何发行版的默认仓库中都不可用。你可以使用 `pipx` 安装它。


要安装它，你需要安装 Python 3.10+ 和 Pip。你可以参考我们关于 [如何在 Ubuntu 和其他 Linux 发行版中安装 pip](https://itsfoss.com/install-pip-ubuntu/) 的文章。


安装 `pip` 后，使用以下命令安装 `pipx`：



```
python3 -m pip install --user pipx
python3 -m pipx ensurepath

```

![An SVG animation showing pipx Installation steps](/data/attachment/album/202304/29/111821k49eodkw4kaaw9k4.svg)


现在，重启终端并运行以下命令安装 shell-genie：



```
pipx install shell-genie

```

这可能显示错误或需要依赖项。


![A dependency installation to install the shell-geie properly](/data/attachment/album/202304/29/111821lj0pvckj9z9bgqqg.png)


运行提示的命令来安装所需的依赖。在我的例子中：



```
sudo apt install python3.10-venv

```

之后，再次运行 `shell-genie` 安装命令，就可以安装了。


![The steps showing the installation of shell-genie](/data/attachment/album/202304/29/111821uxbxjh6m125gj1p5.svg)


安装完成后，运行以下命令：



```
shell-genie init

```

这将要求你选择后端，openAI 或 free-genie。如果你有 [openAI API](https://openai.com/product?ref=itsfoss.com)，你可以选择它或继续使用 free-genie。



> 
> ? free-genie 后端可能并不总是工作，因为它是由开发者托管的，他警告说可能会出现中断。
> 
> 
> 


然后它将请求允许报告反馈。用 `y/n` 来决定。


![Run shell-genie init commad to set up the shell-genie properly](/data/attachment/album/202304/29/111822da8dzaaviqhtjuic.svg)


现在就可以使用了。


### 使用 Shell-genie



> 
> ? 如果你要进行实验，请尽量不要使用带有 `sudo` 或删除文件的命令。不要将你的机器交到机器手中。
> 
> 
> 


如上所述，shell-genie 提供了两种工作模式：


* 从普通的英语获取命令
* 获取命令解释


#### 从普通英语中获取 Linux 命令


你可以使用 shell-genie 的 `ask` 选项从普通的英语中获取命令。例如：



```
shell-genie ask "Display only the folders of this directory"

```

这将显示正确的命令，并提示我们是否运行该命令。


![The working of shell-genie, that will print the required command from the provided plain text description. Also propt the user to execute the same or not](/data/attachment/album/202304/29/111822cfg7ctg7ctvsgkt6.svg)


#### 获取带解释的 Linux 命令


你可以使用 shell genie 来解释你要运行的一些命令。



```
shell-genie ask "display all files smaller than 10kb in here" --explain

```

上面的命令首先会显示需要的命令并进行解释，然后提示用户是否执行。


![The explain mode in shell-genie, where it will explain the command that was asked in the form of plain english and then prompts the user to execute it or not](/data/attachment/album/202304/29/111822adj88ji2midafpdk.svg)


### 总结


有像 [Explain Shell](https://explainshell.com/?ref=itsfoss.com) 这样的工具（试图）解释 Linux 命令。但是这个 Shell genie 通过从普通的英语生成命令将它提升到一个新的水平。


当然，不能一味依赖人工智能。如果你对 Linux 命令有一定的了解，可以使用 Shell Genie 生成适合你的命令。你不必为手册页或各种网站而苦恼。


它可以帮助你在终端中更快地做事，也可以减少你的知识储备。为什么？ 因为你越依赖它，你自己学的就越少。


这就是我所想的。 无论你同意或不同意我的观点，都可以在评论中发表你的看法。


*（题图：MJ/chatgpt commands linux cli illustration in high resolution, very detailed, 8k）*




---


via: <https://itsfoss.com/linux-terminal-ai/>


作者：[Sreenath](https://itsfoss.com/author/sreenath/) 选题：[lkxed](https://github.com/lkxed/) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

Even expert Linux users don't remember all the Linux commands and their options. It's not possible for us humans.

But what about the machine? Especially the AI-powered machine?

Imagine if you could 'order' your terminal to 'show all files under 100 MB in size that were modified in last 12 hours'. Sure, you can 'order' it using the Linux commands but what about interacting with in plain English?

Thanks to the advancement of AI, it is actually possible. Here's an example of automatically generating the Linux command to show all the files smaller than 10 KB in the present directory.

![Shell Genie AI assistent in Linux terminal](https://itsfoss.com/content/images/2023/04/shell-genie.png)

The tool I am using is called [Shell Genie](https://github.com/dylanjcastillo/shell-genie). It is a command-line tool that lets you interact with the terminal in plain English.

It can generate commands, run the commands (if you want to), and it can also explain the generated commands to you.

![Shell Genie explain commands](https://itsfoss.com/content/images/2023/04/shell-genie-explain.png)

## Features of Shell-Genie

- Convert plain English to Linux Commands.
- Provides an openAI gpt3 backend that requires an API key from openAI and a free-genie backend that is free to use.
- Prompt with an option to run the command you asked for.
- Explains the generated commands.

## Install Shell Genie

Shell-genie is not available in the default repositories of any distro. You can install it [using pipx](https://itsfoss.com/install-pipx-ubuntu/).

In order to install, you need to have Python 3.10+ and Pip installed. You can refer to our article on [how to install pip in Ubuntu and other Linux distributions](https://itsfoss.com/install-pip-ubuntu/).

Once `pip`

is installed, install `pipx`

with the following command:

```
python3 -m pip install --user pipx
python3 -m pipx ensurepath
```


`pipx`

Now, restart the terminal and install shell-genie by running:

```
pipx install shell-genie
```


This may show an error or the need for a dependency.

![A dependency installation to install the shell-geie properly](https://itsfoss.com/content/images/2023/03/needs-extensions.png)

Run the command that is prompted to install the required dependency. In my case:

```
sudo apt install python3.10-venv
```


After this, run the `shell-genie`

install command once more, and it will be installed.

Once installation is complete, run the following:

`shell-genie init`


This will ask you to select the backend, either openAI or free-genie. If you have [openAI API](https://openai.com/product), you can choose that or stay with the free-genie.

It will then ask permission to report feedback. Decide on that with a `y/n`

.

You are ready to go now.

## Working with Shell-genie

As said above, shell-genie provides two modes for working.

- Get command from plain English
- Get command explanation

### Get Linux commands from plain English

You can use shell-genie's `ask`

option to get commands from plain English. For eg.

```
shell-genie ask "Display only the folders of this directory"
```


This will display the proper command, and prompt us to run the command or not.

### Get Linux commands with explanation

You can use shell genie to explain some commands that you want to run.

```
shell-genie ask "display all files smaller than 10kb in here" --explain
```


The above command will first display the required command and explains it and then prompts the user to execute it or not.

## Wrapping Up

There are tools like [Explain Shell](https://explainshell.com/) that (try to) explain the Linux commands. But this Shell genie takes it to the next level by generating commands from plain English.

Of course, you cannot rely on artificial intelligence blindly. If you are familiar with the Linux commands a little, you can use Shell Genie to generate commands for your purpose. You won't have to struggle with the man pages or various websites.

It sorts of helps you do things faster in the terminal and also makes you less knowledgeable. Why? Because the more you rely on it, the less you will learn on your own.

That's what I think. Feel free to agree or disagree with me in the comments.