如何在Windows中通过Cygwin使用Linux命令

如何在Windows中通过Cygwin使用Linux命令

Windows command-line tools have advanced a lot with PowerShell, but Linux has had a much more usable terminal for many years. Here’s how you can get the best of both worlds with Cygwin, a ‘nix-style terminal for Windows PCs.

Windows命令行工具在PowerShell中有了很大的进步,但是Linux拥有很多可用的终端了很多年。 以下是如何使用Cygwin(这是Windows PC的“ nix风格”终端)获得两全其美的方法。

In today’s lesson, we’ll explain how to get Cygwin running, but stay tuned for future articles where we’ll explain how to use it for all sorts of different things.

在今天的课程中,我们将说明如何使Cygwin运行,但请继续关注以后的文章,在其中我们将说明如何将其用于各种不同的事情。

安装Cygwin (Install Cygwin)

Cygwin comes with a normal setup.exe to install in Windows, but there are a couple steps you will need to pay attention to, so we will walk you through the installation.

Cygwin带有在Windows中安装的常规setup.exe,但是您需要注意几个步骤,因此我们将指导您完成安装。

如何在Windows中通过Cygwin使用Linux命令

To keep the installation small while saving bandwidth for you and Cygwin, the default installer will download only the files you need from the internet.

为了使安装规模较小,同时又为您和Cygwin节省了带宽,默认安装程序将仅从Internet下载所需文件。

如何在Windows中通过Cygwin使用Linux命令

The default install path is C:\Cygwin but if you don’t like to have programs installed on the root of your C: drive you can change the path or make a symbolic link from C:\Cygwin to your program files.

默认安装路径为C:\ Cygwin,但如果您不希望在C:驱动器的根目录上安装程序,则可以更改路径或建立从C:\ Cygwin到程序文件的符号链接

如何在Windows中通过Cygwin使用Linux命令

Click next until you come to a download mirror selection. Unfortunately, the installer does not say where the mirrors are located so in most cases you might as well just guess which mirror works best.

单击下一步,直到出现下载镜像选择。 不幸的是,安装程序没有说明镜像的位置,因此在大多数情况下,您最好只猜测哪个镜像最有效。

如何在Windows中通过Cygwin使用Linux命令

After you have selected a mirror, the installer will download a list of available packages for you to install. Here is where things get a bit more intimidating.

选择镜像后,安装程序将下载可用软件包的列表供您安装。 在这里,事情变得更加令人生畏。

There will be hundreds of packages available separated by multiple different categories. If you don’t know what the package is you can leave the default selection and install additional packages later by running the installer again.

将有数百种可用的软件包,这些软件包由多个不同的类别分隔。 如果您不知道软件包是什么,则可以保留默认选择并稍后通过再次运行安装程序来安装其他软件包。

如何在Windows中通过Cygwin使用Linux命令

If you know what package you need, you can search for it and the results will be automatically filtered.

如果知道所需的软件包,则可以搜索它,结果将被自动过滤。

如何在Windows中通过Cygwin使用Linux命令

Once you click next, it will take a little while to download all the selected tools and then finish the installation.

单击下一步后,将花一些时间下载所有选定的工具,然后完成安装。

将Cygwin路径添加到Windows环境变量 (Add Cygwin Path to Windows Environment Variable)

After the installation you will have a Cygwin icon on your desktop that you can launch to open the Cygwin terminal.

安装后,您的桌面上将出现一个Cygwin图标,您可以启动该图标来打开Cygwin终端。

如何在Windows中通过Cygwin使用Linux命令

This terminal starts in the C:\Cygwin\home\<user> folder but that isn’t particularly useful because you probably don’t have any files stored there. You can use all of the basic Linux commands but if you want to get back to your C: drive you have to change directory to /cygdrive/c.

该终端从C:\ Cygwin \ home \ <user>文件夹启动,但这并不是特别有用,因为您可能没有存储任何文件。 您可以使用所有基本的Linux命令,但是如果要返回到C:驱动器,则必须将目录更改为/ cygdrive / c。

To make Cygwin work in your normal Windows command prompt you need to add Cygwin to your Windows Environment Variables.

要使Cygwin在正常的Windows命令提示符下工作,您需要将Cygwin添加到Windows环境变量中。

Start by opening your system properties with either Win+Pause/Break or right click on computer and select properties.

首先使用Win + Pause / Break打开系统属性,或右键单击计算机并选择属性。

In the left column click on advanced system settings to open the system properties window we are looking for.

在左列中,单击高级系统设置以打开我们正在寻找的系统属性窗口。

如何在Windows中通过Cygwin使用Linux命令

From the advanced tab, click on environment variables at the bottom.

在高级选项卡上,单击底部的环境变量。

如何在Windows中通过Cygwin使用Linux命令

Then in the system variables, locate the path variable and click edit.

然后在系统变量中,找到路径变量,然后单击“编辑”。

如何在Windows中通过Cygwin使用Linux命令

At the end of the variable value option, add the Cygwin bin location like so.

在变量值选项的末尾,添加Cygwin bin位置,如下所示。

;C:\Cygwin\bin

;C:\Cygwin\bin

Note: Make sure you add a semicolon to separate it from the other values.

注意:确保添加分号以将其与其他值分开。

如何在Windows中通过Cygwin使用Linux命令

Click OK to close out of the window and then open a command prompt to test out a few Linux commands.

单击确定以关闭窗口,然后打开命令提示符以测试一些Linux命令。

As you can see from the below picture both pwd and ls work in the normal Windows command prompt. You can also see that /cygdrive/c is automatically added to the location.

从下图可以看到,pwd和ls都可以在正常的Windows命令提示符下工作。 您还可以看到/ cygdrive / c已自动添加到该位置。

如何在Windows中通过Cygwin使用Linux命令

There is a lot more you can do with Cygwin installed and we will show you some more of the useful tools in future articles.

安装Cygwin可以做很多事情,我们将在以后的文章中向您展示更多有用的工具。

Cygwin homepage

Cygwin主页

翻译自: https://www.howtogeek.com/howto/41382/how-to-use-linux-commands-in-windows-with-cygwin/