在Windows 10中添加适用于Linux的Windows 子系统(Windows Subsystem)让包括我们在内的许多人感到惊讶。虽然它是一个主要供开发人员使用的工具,但普通用户似乎也对此功能感兴趣。我们认为,如果我们向您展示如何在Windows 10(Windows 10)上的Ubuntu上的Bash中运行命令来处理文件、文件夹和应用程序,这将是有意义的。有很多地方需要覆盖,所以让我们开始吧:
注意:(NOTE:)本教程基于11 月 10 日(November 10)更新(Update)的Windows 10。如果您使用的是旧版本的Windows 10,则某些命令可能无法正常工作,或者情况可能会有所不同。
如何在 Windows 上安装 Ubuntu
对于本教程,您应该在Windows 10计算机或设备(computer or device)上安装Linux Ubuntu on Windows。如果你还没有这样做,或者如果你需要一些关于这个问题的指导,我们在这里描述了整个安装过程(installation process):如何获得适用于Linux的(Linux)Windows 子系统(Windows Subsystem)?
其次,我们假设您从未使用过Linux,并且“您对此一无所知”,就像Jon Snow一样。🙂
什么是 Windows 上的 Ubuntu 上的 Bash
作为一个完整的初学者,您可能想知道您从Bash on Ubuntu on Windows中得到了什么。您可以在Windows(Windows) 10 计算机上获得整个Bash shell环境。如果您以前使用过Windows,无论是Windows 10、Windows XP还是这两者之间的任何东西,您可能都熟悉命令提示符(Command Prompt)和PowerShell环境。Bash是一个类似的 shell,但是是为Linux操作系统创建的。
就像Command Prompt一样,Bash是一个命令行环境。它运行在Linux 内核(Linux kernel)之上,并为您提供了一系列可运行的命令行实用程序。如果需要,您可以使用它来下载、安装和使用Linux应用程序。您在Bash中运行的所有实用程序都可以访问您的Windows 10文件系统(file system)。这意味着您可以访问、创建和删除任何驱动器上的文件和文件夹。同样重要的是要注意,即使有可能,在Windows上的(Windows)Ubuntu中运行图形应用程序也是一件相当复杂的事情,而且容易出错。那是因为(Ubuntu)创建Windows(Windows)上的Ubuntu是为了提供Linux 子系统并(Linux subsystem and access)为开发人员而非普通用户提供对其实用程序的访问。
如何在 Windows 10 上启动 Ubuntu
在Windows上安装(Windows)Ubuntu后,您要做的第一件事就是启动它,一个快速的方法是在开始菜单(Start Menu)中单击或点击它的快捷方式。
这是Ubuntu的界面在(Ubuntu)Windows 10中的样子:
作为命令行环境,您需要编写要在其中运行的文本命令。如果您以前使用过命令提示符(Command Prompt),那么您可能习惯于编写命令的整个路径以便能够运行它。在您使用Bash shell(Bash shell)时忘记这一点。在这里,您安装的任何命令、工具或应用程序都可以在任何地方运行,只需输入其名称即可。无需在其名称前添加其路径。现在让我们看看您需要学习的基本命令:
1. 如何在 Windows 10 PC 上更改目录,在Ubuntu上使用(Ubuntu)Bash,使用CD 命令(CD command)
Bash中的基本命令之一是允许您更改正在工作的文件夹的命令。要从 Windows 10 PC 导航到另一个文件夹,您只需运行命令cd [path]其中[path]是该文件夹的位置。
请注意,Windows 10 PC 上的所有驱动器都安装在/mnt/[the letter of the drive]中。例如,通常安装Windows 10的(Windows 10)C盘位于以下位置:/mnt/c。D驱动器(如果有的话)位于/mnt/d等等。举个例子:如果你想导航到D盘上的(D)Digital Citizen文件夹,你必须运行这个Bash 命令(Bash command):cd "/mnt/d/Digital Citizen"。
重要提示:(IMPORTANT:)如果您要使用的文件夹名称中有空格,例如Digital Citizen的情况,请确保将路径放在引号之间。此外,只使用直标记,不要使用弯引号或智能引号,因为它们不起作用。
2. 如何在 Windows 10 PC 上查看文件夹的内容,在Ubuntu上使用(Ubuntu)Bash,使用LS 命令(LS command)
在Bash中,另一个显示文件夹内容的重要命令是ls。输入它,按键盘上的Enter键,您应该会得到当前目录中找到的每个文件夹和文件的列表。(folder and file)
正如您在上一个屏幕截图中看到的那样,文件夹被突出显示,以便您可以将它们与文件区分开来。
3. 如何在 Windows 10 PC 上复制文件,在Ubuntu上使用(Ubuntu)Bash,使用CP 命令(CP command)
Bash还包括基本(Bash)文件管理(file management)所需的所有命令,例如复制、移动、重命名、删除。让您将文件从一个地方复制到另一个地方的命令是cp [source] [destination],其中[source]是要复制的文件的路径,而[destination]是要复制的地方的路径.
例如,假设我们想要将位于Digital Citizen文件夹中的名为incognito.xlsx的文件复制到(incognito.xlsx,)Security for Everyone子文件夹。为此,我们必须运行的命令是cp "/mnt/d/Digital Citizen/incognito.xlsx" "/mnt/d/Digital Citizen/Security for Everyone"。
然后将该文件复制到指定的文件夹。
4. 如何在 Windows 10 PC 上移动或重命名文件,在Ubuntu上使用(Ubuntu)Bash,使用MV 命令(MV command)
使用Bash(Bash)移动或重命名文件类似于复制文件。为此需要运行的命令具有以下语法:mv [source] [destination],其中[source]是被移动或重命名的文件的路径,而[destination]是它要去的地方的路径被移动。如果[destination]与[source] 相同,([source],)但您指定的文件名(file name)与原始文件名不同,则文件被重命名而不是移动。
使用与之前相同的示例,让我们将incognito.xlsx文件从我们的Digital Citizen文件夹移动到Security for Everyone子文件夹,然后将其重命名为web_incognito.xlsx。为此,我们必须运行以下命令:mv "/mnt/d/Digital Citizen/incognito.xlsx" "/mnt/d/Digital Citizen/Security for Everyone"然后mv "/mnt/d/Digital Citizen/Security for Everyone/incognito.xlsx" "/mnt/d/Digital Citizen/Security for Everyone/web_incognito.xlsx"。
5. 如何从 Windows 10 PC 中删除文件,在Ubuntu上使用(Ubuntu)Bash,使用RM 命令(RM command)
在rm [file](rm [file])命令的帮助下删除或删除Bash中的文件。(Bash)要删除我们之前创建的web_incognito.xlsx文件,我们首先使用命令cd "/mnt/d/Digital Citizen/Security for Everyone"导航到包含它的Security for Everyone文件夹。然后,我们运行命令rm web_incognito.xlsx。
6. 如何在 Windows 10 PC上使用MKDIR 命令(MKDIR command)在Ubuntu上使用(Ubuntu)Bash创建文件夹(Bash)
使用Bash(Bash)创建文件夹是使用mkdir [folder]命令完成的,其中[folder]是您要创建的文件夹的名称。举例来说,假设我们要在我们的Digital Citizen目录中创建一个名为Tests的文件夹。(Tests)为此,我们首先导航到Digital Citizen文件夹(cd "/mnt/d/Digital Citizen"),然后运行命令mkdir Tests。就这么简单。
7. 如何从 Windows 10 PC 中删除文件夹,在Ubuntu上使用(Ubuntu)Bash,使用RM -R 命令(RM -R command)
同样,使用Bash(Bash)删除或删除文件夹同样简单。导航(Navigate)到要删除的文件夹的父目录并运行命令(parent directory)rm -r [folder],其中[folder]是要删除的文件夹的名称。您可能已经注意到该命令还包含-r参数:您需要添加它,以便该文件夹中的所有内容也被递归删除。
举例来说,让我们删除我们之前在Digital Citizen文件夹中创建的(Digital Citizen)Tests文件夹。为此,我们首先必须使用以下命令导航到父文件夹(parent folder),即Digital Citizen:cd "/mnt/d/Digital Citizen"。然后,我们必须运行命令rm -r Tests。
8. 如何在Windows 10上下载和安装(Windows 10)Linux 应用程序(Linux apps),在Ubuntu上使用(Ubuntu)Bash,使用SUDO APT-GET INSTALL 命令(SUDO APT-GET INSTALL command)
Linux还允许您使用(Linux)SUDO 命令(SUDO command)以超级用户身份运行命令,该命令为您提供管理权限。语法很简单:sudo [application],其中[application]可以是安装在操作系统(operating system)上的任何程序。因为sudo命令赋予您对Linux中所有内容的管理权限,所以它与(Linux)管理员用户帐户在(administrator user account)Windows中可以执行的操作非常相似。如果要下载、安装和运行Linux应用程序,可以使用以下命令:
首先,运行sudo apt-get update。此命令从Ubuntu(Ubuntu)下载最新的软件列表。
然后,要下载并安装所需的Linux 应用程序(Linux app),请运行sudo apt-get install [application name]。例如,如果我们想安装w3m,它是一个基于文本的网络浏览器(web browser),我们必须运行sudo apt-get install w3m。如果应用程序需要其他软件包,您可能需要确认是否要继续,在这种情况下,请按(case press)键盘上的Y键。
安装应用程序后,您可以通过在Bash(Bash)中键入其名称来运行它,然后输入任何必要的参数。例如,如果我们想访问我们的网站,我们必须输入w3m digitalcitizen.life。
9. 如何在Windows(Windows)上的Ubuntu上获得Bash帮助,使用HELP 参数(HELP argument)
如果您想了解更多关于某个命令的作用,请将其写入Bash并添加参数"--help"。显示的信息(information displayed)应该更清楚地说明该命令的结构和用法(structure and usage)。以命令ls(ls,)为例,它列出了文件夹的内容。它可能看起来像一个简单的命令,但它为如何显示信息提供了很多选项。这是您获得的帮助的一部分:
你在(Did)Windows上的Ubuntu上尝试过Bash吗?
我们希望我们已经成功地向您介绍了Linux Bash的非常有趣的世界以及在(Linux Bash)Windows 10中使用它的基础知识。尝试我们展示的命令,并使用HELP 参数(HELP argument)了解有关每个命令的更多信息。如果您有任何问题或提示要分享,请随时在下面发表评论。
How to work with files, folders and apps in Bash on Ubuntu on Windows 10
The addition of the Windows Subsystеm for Linυx in Windows 10 camе as a surprise for many, uѕ included. Although it iѕ a tool intended to be uѕеd maіnly by developеrs, regular users seem to be interested in this feature too. We thought it would make sense if we show you how to run commands in Bash on Ubuntu on Windows 10 to work with files, folders, and apps. There is рlenty of ground to covеr, so let's get started:
NOTE: This tutorial is based on Windows 10 with November 10 Update. If you have an older version of Windows 10, some commands might not work, or things might look differently.
How to get Ubuntu on Windows
For this tutorial, you should have the Linux Ubuntu on Windows installed on your Windows 10 computer or device. If you haven't done it already, or if you need some guidance on this matter, we described the whole installation process here: How do I get the Windows Subsystem for Linux?
Secondly, we assume that you have never ever worked with Linux, and that "you know nothing" about it, just like Jon Snow. 🙂
What is Bash on Ubuntu on Windows
As a complete beginner, you might wonder what you're getting from Bash on Ubuntu on Windows. You get the entire Bash shell environment on your Windows 10 computer. If you used Windows before, be it Windows 10, Windows XP, or anything between those two, you are probably acquainted with the Command Prompt and the PowerShell environments. Bash is a similar shell, but created for Linux operating systems.
Just like Command Prompt, Bash is a command-line environment. It runs on top of a Linux kernel and offers you a whole range of command-line utilities to run. You can use it to download, install, and use Linux applications, if that's what you want. And all the utilities you run inside Bash get access to your Windows 10 file system. That means you can access, create, and delete files and folders on any of your drives. It is also important to note that, even though it's possible, running graphical applications in Ubuntu on Windows is both a fairly complex thing to do and prone to errors. That's because Ubuntu on Windows was created to provide a Linux subsystem and access to its utilities for developers, not for regular users.
How to start Ubuntu on Windows 10
The first thing you have to do after you've installed Ubuntu on Windows is to launch it, and a quick way to do that is to click or tap on its shortcut from the Start Menu.
This is what Ubuntu's interface looks like in Windows 10:
Being a command-line environment, you need to write the text commands that you want to run in it. If you've worked with Command Prompt before, then you're probably accustomed to writing the whole path of a command to be able to run it. Forget about that while you're using the Bash shell. Here, any command, tool, or app you install can be run from anywhere just by typing its name. There's no need to add its path before its name. Now let's see the basic commands that you need to learn to get started:
1. How to change the directory on your Windows 10 PC, with Bash on Ubuntu, using the CD command
One of the essential commands in Bash is the one that lets you change the folder in which you're working. To navigate to another folder from your Windows 10 PC, all you have to do is run the command cd [path] where [path] is the location of that folder.
Note that all the drives on your Windows 10 PC are mounted in /mnt/[the letter of the drive]. For example, the C drive where Windows 10 is usually installed is found at this location: /mnt/c. The D drive, if you have one, is found at /mnt/d and so on. Let's take an example: if you'd want to navigate to the Digital Citizen folder found on your D drive, you'd have to run this Bash command: cd "/mnt/d/Digital Citizen".
IMPORTANT: If the folder you want to work in has spaces in its name, such as is the case for the Digital Citizen, make sure to put the path between quotation marks. Also, use only straight marks, not curly or smart quotation marks, as they don't work.
2. How to see the contents of a folder on your Windows 10 PC, with Bash on Ubuntu, using the LS command
In Bash, another vital command, that shows you the contents of a folder, is ls. Type it in, press Enter on your keyboard, and you should get a list of every folder and file found inside the current directory.
As you can see in the previous screenshot, the folders are highlighted so that you can differentiate them from files.
3. How to copy files on your Windows 10 PC, with Bash on Ubuntu, using the CP command
Bash also includes all the commands required for basic file management, such as copy, move, rename, delete. The command that lets you copy a file from one place to another is cp [source] [destination], where [source] is the path to the file that's copied and [destination] is the path to the place where it's going to be copied.
For example, let's say that we want to copy the file called incognito.xlsx, found in our Digital Citizen folder, to the Security for Everyone subfolder. To do so, the command we'd have to run is cp "/mnt/d/Digital Citizen/incognito.xlsx" "/mnt/d/Digital Citizen/Security for Everyone".
The file is then copied to the specified folder.
4. How to move or rename files on your Windows 10 PC, with Bash on Ubuntu, using the MV command
Moving or renaming files with Bash is similar to copying files. The command you need to run for this purpose has the following syntax: mv [source] [destination], where [source] is the path to the file that's moved or renamed and [destination] is the path to the place where it's going to be moved. If the [destination] is the same as the [source], but the file name you specify is different from the original, the file is renamed instead of moved.
Using the same example as before, let's move the incognito.xlsx file from our Digital Citizen folder to the Security for Everyone subfolder, and then rename it to web_incognito.xlsx. To do so, we have to run these commands: mv "/mnt/d/Digital Citizen/incognito.xlsx" "/mnt/d/Digital Citizen/Security for Everyone" and then mv "/mnt/d/Digital Citizen/Security for Everyone/incognito.xlsx" "/mnt/d/Digital Citizen/Security for Everyone/web_incognito.xlsx".
5. How to delete files from your Windows 10 PC, with Bash on Ubuntu, using the RM command
Removing or deleting files in Bash is done with the help of the rm [file] command. To remove the web_incognito.xlsx file that we created earlier, we first navigate to our Security for Everyone folder that holds it, using the command cd "/mnt/d/Digital Citizen/Security for Everyone". Then, we run the command rm web_incognito.xlsx.
6. How to create folders on your Windows 10 PC, with Bash on Ubuntu, using the MKDIR command
Creating a folder with Bash is done using the mkdir [folder] command, where [folder] is the name of the folder you want to create. To exemplify, let's say that we want to create a folder called Tests inside our Digital Citizen directory. To do so, we first navigate to the Digital Citizen folder (cd "/mnt/d/Digital Citizen") and then run the command mkdir Tests. It's as simple as that.
7. How to delete folders from your Windows 10 PC, with Bash on Ubuntu, using the RM -R command
Similarly, removing or deleting a folder using Bash is just as easy. Navigate to the parent directory of the folder that you want to remove and run the command rm -r [folder], where [folder] is the name of the folder to delete. You might have noticed that this command also includes the -r argument: you need to add it so that everything inside that folder is also deleted, recursively.
To exemplify, let's delete the Tests folder that we previously created inside our Digital Citizen folder. For that, we first have to navigate to the parent folder, which is Digital Citizen, using the command: cd "/mnt/d/Digital Citizen". Then, we must run the command rm -r Tests.
8. How to download and install Linux apps on Windows 10, with Bash on Ubuntu, using the SUDO APT-GET INSTALL command
Linux also lets you run commands as a superuser using the SUDO command, which gives you administrative privileges. The syntax is simple: sudo [application], where [application] can be any program installed on the operating system. Because the sudo command gives you administrative powers over everything in Linux, it is very similar to what an administrator user account can do in Windows. If you want to download, install, and run Linux applications, you can use the following commands:
First, run sudo apt-get update. This command downloads the latest software lists from Ubuntu.
Then, to download and install the Linux app that you want, run sudo apt-get install [application name]. For example, if we'd want to install w3m, which is a text-based web browser, we'd have to run sudo apt-get install w3m. If the app requires additional packages, you might have to confirm that you want to continue, in which case press the Y key on your keyboard.
Once the app is installed, you can run it by typing its name in Bash, followed by any parameters necessary. For example, if we wanted to visit our website, we'd have to type w3m digitalcitizen.life.
9. How to get help in Bash on Ubuntu on Windows, using the HELP argument
If you want to find out more about what a certain command does, write it in Bash and add the parameter "--help". The information displayed should shed more light on the structure and usage of that command. Take, for instance, the command ls, which lists the contents of a folder. It might seem like a simple command, but it offers quite a lot of options for how it displays the information. Here's a part of the help you get for it:
Did you try Bash on Ubuntu on Windows?
We hope that we've managed to introduce you to the very interesting world of the Linux Bash and the basics of using it in Windows 10. Try the commands that we showcased, and find out more about each command using the HELP argument. If you have any questions or tips to share, don't hesitate to comment below.