前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Linux】《how linux work》第二章 基本命令和目录层次结构(第一部分)

【Linux】《how linux work》第二章 基本命令和目录层次结构(第一部分)

原创
作者头像
阿东
发布2024-04-21 07:51:36
930
发布2024-04-21 07:51:36
举报
文章被收录于专栏:公众号:懒时小窝

第 2 章 Basic Commands and Directory Hierarchy(基本命令和目录层次结构)

This chapter is a guide to the Unix commands and utilities that will be referenced throughout this book. This is preliminary material, and you may already know a substantial amount of it. Even if you think you’re up to speed, take a few seconds to flip through the chapter just to make sure, especially when it comes to the directory hierarchy material in 2.19 Linux Directory Hierarchy Essentials.

本章是对Unix命令和实用工具的指南,这些内容将在本书中被引用。

这是初步材料,你可能已经了解了其中相当多的内容。

即使你认为自己已经掌握了,也请花几秒钟翻阅本章,以确保无误,尤其是在涉及2.19 Linux目录层次结构要点的部分。

Why Unix commands? Isn’t this a book about how Linux works? It is, of course, but Linux is a Unix flavor at heart. You’ll see the word Unix in this chapter more than Linux because you can take what you learn straight over to Solaris, BSD, and other Unix-flavored systems. I’ve attempted to avoid covering too many Linuxspecific user interface extensions, not only to give you a better background for using the other operating systems, but also because these extensions tend to be unstable. You’ll be able to adapt to new Linux releases much more quickly if you know the core commands.

为什么要学习Unix命令?这不是一本关于Linux如何工作的书吗?当然,它是的,但Linux本质上是一种Unix系统。

在本章中,你会看到比Linux更多的Unix这个词,因为你所学到的东西可以直接应用于Solaris、BSD和其他Unix系统。

我尽量避免涵盖太多Linux特定的用户界面扩展,不仅是为了给你使用其他操作系统提供更好的背景,而且因为这些扩展往往不稳定。

如果你了解核心命令,你将能够更快地适应新的Linux版本。

NOTE For more details about Unix for beginners than you’ll find here, consider reading The Linux Command Line (No Starch Press, 2012), UNIX for the Impatient (Addison-Wesley Professional, 1995), and Learning the UNIX Operating System, 5th edition (O’Reilly, 2001). 注意:如果你想要了解更多关于Unix初学者的细节,请考虑阅读 《Linux命令行》(No Starch Press,2012)、 《UNIX for the Impatient》(Addison-Wesley Professional,1995)和 《Learning the UNIX Operating System, 5th edition》(O'Reilly,2001)。

## 快乐的 Linux 命令行

2.1 The Bourne Shell: /bin/sh(伯恩 shell /bin/sh)

The shell is one of the most important parts of a Unix system. A shell is a program that runs commands, like the ones that users enter. The shell also serves as a small programming environment. Unix programmers often break common tasks into little components and use the shell to manage tasks and piece things together.

Shell是Unix系统中最重要的部分之一。

Shell是一个运行命令的程序,就像用户输入的命令一样。

Shell还充当一个小型的编程环境。

Unix程序员经常将常见任务分解为小组件,并使用Shell来管理任务和组合事物。

Many important parts of the system are actually shell scripts—text files that contain a sequence of shell commands. If you’ve worked with MS-DOS previously, you can think of shell scripts as very powerful .BAT files. Because they’re important, Chapter 11 is devoted entirely to shell scripts.

许多系统的重要部分实际上是Shell脚本——包含一系列Shell命令的文本文件。

如果你之前使用过MS-DOS,可以将Shell脚本视为非常强大的.BAT文件。

由于它们很重要,第11章完全专门讲解了Shell脚本。

As you progress through this book and gain practice, you’ll add to your knowledge of manipulating commands using the shell. One of the best things about the shell is that if you make a mistake, you can easily see what you typed to find out what went wrong, and then try again.

随着你在本书中的学习和实践,你会不断增加使用Shell操作命令的知识。

Shell最好的一点是,如果你犯了一个错误,你可以轻松地看到你输入的内容,找出问题所在,然后再试一次。

There are many different Unix shells, but all derive several of their features from the Bourne shell (/bin/sh), a standard shell developed at Bell Labs for early versions of Unix. Every Unix system needs the Bourne shell in order to function correctly, as you will see throughout this book.

有许多不同的Unix Shell,但它们都从Bourne Shell(/bin/sh)派生了一些特性,这是贝尔实验室为早期Unix版本开发的一个标准Shell。

每个Unix系统都需要Bourne Shell才能正常运行,这一点你将在本书中看到。

Linux uses an enhanced version of the Bourne shell called bash or the “Bourne-again” shell. The bash shell is the default shell on most Linux distributions, and /bin/sh is normally a link to bash on a Linux system. You should use the bash shell when running the examples in this book.

Linux使用了一个增强版的Bourne Shell,称为bash或“Bourne-again” Shell。

bash Shell是大多数Linux发行版的默认Shell,/bin/sh通常是Linux系统上bash的链接。

在运行本书中的示例时,应使用bash Shell。

NOTE You may not have bash as your default shell if you’re using this chapter as a guide for a Unix account at an organization where you’re not the system administrator. You can change your shell with chsh or ask your system administrator for help.注意如果你在使用本章作为Unix账户的指南,并且你不是系统管理员,则bash可能不是你的默认Shell。你可以使用chsh命令更改你的Shell,或者向系统管理员寻求帮助。2.2 Use Shell(使用 Shell)

When you install Linux, you should create at least one regular user in addition to the root user; this will be your personal account. For this chapter, you should log in as the regular user.

安装 Linux 时,除了 root 用户外,还应创建至少一个普通用户,这就是 你的个人账户。

在本章中,你应以普通用户身份登录。

2.2.1 The Shell Window(shell 窗口)

After logging in, open a shell window (often referred to as a terminal). The easiest way to do so from a GUI like Gnome or Ubuntu’s Unity is to open a terminal application, which starts a shell inside a new window. Once you’ve opened a shell, it should display a prompt at the top that usually ends with a dollar sign ($). On Ubuntu, that prompt should look like name@host:path$, and on Fedora, it’s name@host path$. If you’re familiar with Windows, the shell window will look something like a DOS command prompt; the Terminal application in OS X is essentially the same as a Linux shell window.

登录后,打开一个shell窗口(通常称为终端)。

在类似Gnome或Ubuntu Unity的图形界面中,最简单的方法是打开一个终端应用程序,它会在一个新窗口中启动一个shell。

一旦你打开了一个shell,它应该在顶部显示一个提示符,通常以美元符号($)结尾。

在Ubuntu上,该提示符应该看起来像name@host:path$,而在Fedora上,它是name@host path$。

如果你熟悉Windows,shell窗口看起来会像DOS命令提示符;

在OS X中的终端应用程序本质上与Linux的shell窗口相同。

This book contains many commands that you will type at a shell prompt. They all begin with a single $ to denote the shell prompt. For example, type this command (just the part in bold, not the $) and press ENTER:

这本书包含了许多你将在命令行提示符下输入的命令。

它们都以一个单独的$符号开头,用来表示命令行提示符。

例如,输入这个命令(只输入加粗部分,不包括$符号),然后按回车键:

代码语言:sh
复制
$ echo Hello there.

NOTE Many shell commands in this book start with #. You should run these as the superuser (root). These commands usually require extra caution.注意本书中的许多 shell 命令都以 # 开头。您应该以超级用户(root)身份运行这些命令。这些命令通常需要格外小心。

现在输入以下命令

代码语言:sh
复制
$ cat /etc/passwd

This command displays the contents of the /etc/passwd system information file and then returns your shell prompt. Don’t worry about what this file does right now; you’ll learn all about it later, in Chapter 7.

该命令将显示 /etc/passwd 系统信息文件的内容,然后返回 shell 提示。

现在不用担心这个文件的作用,稍后你将在第 7 章中了解到它的全部内容。

2.2.2 cat

The cat command is one of the easiest Unix commands to understand; it simply outputs the contents of one or more files. The general syntax of the cat command is as follows:

cat命令是最容易理解的Unix命令之一;

它简单地输出一个或多个文件的内容。

cat命令的一般语法如下:

代码语言:sh
复制
$ cat file1 file2 ...

When you run this command, cat prints the contents of file1, file2, and any other files that you specify (denoted by ...), and then exits. The command is called cat because it performs concatenation when it prints the contents of more than one file.

当你运行这个命令时,cat会打印文件1、文件2以及你指定的其他文件(用...表示)的内容,然后退出。

cat命令之所以被称为cat,是因为当它打印多个文件的内容时,它执行了连接操作。

2.2.3 Standard Input and Standard Output(标准输入和标准输出)

We’ll use cat to briefly explore Unix input and output (I/O). Unix processes use I/O streams to read and write data. Processes read data from input streams and write data to output streams. Streams are very flexible. For example, the source of an input stream can be a file, a device, a terminal, or even the output stream from another process.

我们将使用cat命令简要探索Unix的输入和输出(I/O)。

Unix进程使用I/O流来读取和写入数据。进程从输入流读取数据,然后将数据写入输出流。

流非常灵活。

例如,输入流的源可以是文件、设备、终端,甚至是另一个进程的输出流。

To see an input stream at work, enter cat (with no filenames) and press ENTER. This time, you won’t get your shell prompt back because cat is still running. Now type anything and press ENTER at the end of each line. The cat command repeats any line that you type. Once you’re sufficiently bored, press CTRL-D on an empty line to terminate cat and return to the shell prompt.

为了看到输入流的工作原理,请输入cat(不带文件名)并按下ENTER键。

这次,你不会立即得到shell提示符,因为cat仍在运行。现在在每行的末尾输入任何内容,然后按下ENTER键。

cat命令会重复打印你输入的每一行。

当你感到足够无聊时,在空行上按下CTRL-D来终止cat并返回到shell提示符。

The reason cat adopted an interactive behavior has to do with streams. Because you did not specify an input filename, cat read from the standard input stream provided by the Linux kernel rather than a stream connected to a file. In this case, the standard input was connected to the terminal in which you ran cat.

cat采用交互行为的原因与流有关。

因为你没有指定输入文件名,所以cat从Linux内核提供的标准输入流中读取,而不是与文件连接的流。

在这种情况下,标准输入与你运行cat的终端相连接。

NOTE Pressing CTRL-D on an empty line stops the current standard input entry from the terminal (and often terminates a program). Don’t confuse this with CTRL-C, which terminates a program regardless of its input or output.注意 在空行上按下CTRL-D会停止从终端读取当前的标准输入(通常也会终止一个程序)。不要将此与CTRL-C混淆,后者无论输入或输出如何都会终止一个程序。

Standard output is similar. The kernel gives each process a standard output stream where it can write its output. The cat command always writes its output to the standard output. When you ran cat in the terminal, the standard output was connected to that terminal, so that’s where you saw the output.

标准输出类似。

内核为每个进程提供一个标准输出流,进程可以在其中写入输出。

cat命令始终将其输出写入标准输出。

当你在终端上运行cat时,标准输出与该终端相连接,所以你在那里看到了输出。

Standard input and output are often abbreviated as stdin and stdout. Many commands operate as cat does; if you don’t specify an input file, the command reads from stdin. Output is a little different. Some commands (like cat) send output only to stdout, but others have the option to send output directly to files.

标准输入和输出通常缩写为stdin和stdout。许多命令的操作方式与cat相同;如果你没有指定输入文件,命令会从stdin读取。输出有些不同。有些命令(如cat)只将输出发送到stdout,但其他命令可以选择直接将输出发送到文件。

There is a third standard I/O stream called standard error. You’ll see it in 2.14.1 Standard Error.

还有第三个标准I/O流称为标准错误。你将在2.14.1标准错误中看到它。

One of the best features of standard streams is that you can easily manipulate them to read and write to places other than the terminal, as you’ll learn in 2.14 Shell Input and Output. In particular, you’ll learn how to connect streams to files and other processes.

标准流的一个最好的特性是你可以轻松地操作它们以读取和写入除终端以外的其他地方,你将在2.14 Shell输入和输出中学到这一点。特别是,你将学习如何将流连接到文件和其他进程。

2.3 Basic Commands(基本命令)

Now let’s look at some more Unix commands. Most of the following programs take multiple arguments, and some have so many options and formats that an unabridged listing would be pointless. This is a simplified list of the basic commands; you don’t need all of the details just yet.

现在让我们来看一些更多的Unix命令。以下大部分程序接受多个参数,并且有些命令有很多选项和格式,完整的列表是没有意义的。

这是一个简化的基本命令列表;你现在不需要所有的细节。

2.3.1 ls

The ls command lists the contents of a directory. The default is the current directory. Use ls -l for a detailed (long) listing and ls -F to display file type information. (For more on the file types and permissions displayed in the left column below, see 2.17 File Modes and Permissions.) Here is a sample long listing; it includes the owner of the file (column 3), the group (column 4), the file size (column 5), and the modification date/time (between column 5 and the filename):

ls命令列出一个目录的内容。默认是当前目录。

使用ls -l可以显示详细(长)列表,使用 ls -F 可以显示文件类型信息。

(有关左列中显示的文件类型和权限的更多信息,请参见2.17 文件模式和权限。)

以下是一个示例长列表;它包括文件的

  • 所有者(第3列)
  • 组(第4列)
  • 文件大小(第5列)
  • 修改日期/时间(在第5列和文件名之间)
代码语言:sh
复制
$ ls -l
总计 3616
-rw-r--r-- 1 juser users 3804 Apr 30 2011 abusive.c
-rw-r--r-- 1 juser users 4165 May 26 2010 battery.zip
-rw-r--r-- 1 juser users 131219 Oct 26 2012 beav_1.40-13.tar.gz
-rw-r--r-- 1 juser users 6255 May 30 2010 country.c
drwxr-xr-x 2 juser users 4096 Jul 17 20:00 cs335
-rwxr-xr-x 1 juser users 7108 Feb 2 2011 dhry
-rw-r--r-- 1 juser users 11309 Oct 20 2010 dhry.c
-rw-r--r-- 1 juser users 56 Oct 6 2012 doit
drwxr-xr-x 6 juser users 4096 Feb 20 13:51 dw
drwxr-xr-x 3 juser users 4096 May 2 2011 hough-stuff

You’ll learn more about the d in column 1 of this output in 2.17 File Modes and Permissions.

关于此输出中第1列的d,您将在2.17 文件模式和权限中了解更多信息。

2.3.2 cp

In its simplest form, cp copies files. For example, to copy file1 to file2, enter this:

cp命令用于复制文件。

例如,要将file1复制到file2,输入以下命令:

代码语言:sh
复制
cp file1 file2

To copy a number of files to a directory (folder) named dir, try this instead:

要将多个文件复制到名为dir的目录(文件夹),可以尝试以下命令:

代码语言:sh
复制
cp file1 ... fileN dir

2.3.3 mv

The mv (move) command is like cp. In its simplest form, it renames a file. For example, to rename file1 to file2, enter this:

mv(移动)命令与cp类似。在其最简单的形式中,它用于重命名文件。

例如,要将file1重命名为file2,输入以下命令:

代码语言:sh
复制
mv file1 file2

您还可以使用mv将多个文件移动到不同的目录:

代码语言:sh
复制
mv file1 ... fileN dir

2.3.4 touch

The touch command creates a file. If the file already exists, touch does not change it, but it does update the file’s modification time stamp printed with the ls -l command. For example, to create an empty file, enter this:

touch命令用于创建文件。

如果文件已经存在,touch不会更改它,但会更新使用ls -l命令打印的文件修改时间戳。

例如,要创建一个空文件,输入以下命令:

代码语言:sh
复制
touch file

Then run ls -l on that file. You should see output like the following, where the date and time ➊ indicate when you ran touch:

然后在该文件上运行ls -l。

您应该会看到类似以下的输出,其中日期和时间 ➊ 表示您运行touch的时间:

代码语言:sh
复制
ls -l file

-rw-r--r-- 1 juser users 0 May 21 18:32➊ file

2.3.5 rm

To delete (remove) a file, use rm. After you remove a file, it’s gone from your system and generally cannot be undeleted.

要删除文件,使用rm命令。删除文件后,它将从系统中消失,通常无法恢复。

代码语言:sh
复制
rm file

2.3.6 echo

The echo command prints its arguments to the standard output:

echo命令将其参数打印到标准输出:

代码语言:sh
复制
echo Hello again.
Hello again.

The echo command is very useful for finding expansions of shell globs (“wildcards” such as *) and variables (such as $HOME), which you will encounter later in this chapter.

echo命令非常有用,可以用于查找shell通配符(例如*)和变量(例如$HOME)的扩展,您将在本章后面遇到。

2.4 Navigating Directories(目录导航)

Unix has a directory hierarchy that starts at /, sometimes called the root directory. The directory separator is the slash (/), not the backslash (). There are several standard subdirectories in the root directory, such as /usr, as you’ll learn in 2.19 Linux Directory Hierarchy Essentials.

Unix有一个从/开始的目录层次结构,有时称为根目录。

目录分隔符是斜杠(/),而不是反斜杠(\)。

根目录中有几个标准子目录,例如/usr,您将在2.19 Linux目录层次结构基础知识中了解到。

When you refer to a file or directory, you specify a path or pathname. When a path starts with / (such as /usr/lib), it’s a full or absolute path.

当你引用一个文件或目录时,你需要指定一个路径或路径名。

当一个路径以/开头(比如/usr/lib),它就是一个完整的或绝对的路径。

A path component identified by two dots (..) specifies the parent of a directory. For example, if you’re working in /usr/lib, the path .. would refer to /usr. Similarly, ../bin would refer to /usr/bin.

由两个点(..)标识的路径组件指定了一个目录的父目录。

例如,如果你正在/usr/lib中工作,路径..将指向/usr。

同样,../bin将指向/usr/bin。

One dot (.) refers to the current directory; for example, if you’re in /usr/lib, the path . is still /usr/lib, and ./X11 is /usr/lib/X11. You won’t have to use . very often because most commands default to the current directory if a path doesn’t start with / (you could just use X11 instead of ./X11 in the preceding example).

一个点(.)表示当前目录;例如,如果你在/usr/lib中,路径.仍然是/usr/lib,而./X11是/usr/lib/X11。

你不需要经常使用.,因为大多数命令默认使用当前目录,如果路径不以/开头(在上面的例子中,你可以只使用X11而不是./X11)。

A path not beginning with / is called a relative path. Most of the time, you’ll work with relative pathnames, because you’ll already be in the directory you need to be in or somewhere close by.

不以/开头的路径称为相对路径。

大多数情况下,你会使用相对路径名,因为你已经在需要的目录中或者离目录很近了。

Now that you have a sense of the basic directory mechanics, here are some essential directory commands.

现在你对基本目录机制有了一定的了解,下面是一些必要的目录命令。

2.4.1 cd

The current working directory is the directory that a process (such as the shell) is currently in. The cd command changes the shell’s current working directory:

当前工作目录是进程(比如shell)当前所在的目录。cd命令用于改变shell的当前工作目录:

代码语言:sh
复制
	$ cd dir

If you omit dir, the shell returns to your home directory, the directory you started in when you first logged in.

如果你省略dir,shell会返回你的主目录,即你首次登录时所在的目录。

2.4.2 mkdir

The mkdir command creates a new directory dir:

mkdir命令用于创建一个新目录dir:

代码语言:sh
复制
$ mkdir dir

2.4.3 rmdir

The rmdir command removes the directory dir:

rmdir命令用于删除目录dir:

代码语言:sh
复制
$ rmdir dir

If dir isn’t empty, this command fails. However, if you’re impatient, you probably don’t want to laboriously delete all the files and subdirectories inside dir first. You can use rm -rf dir to delete a directory and its contents, but be careful! This is one of the few commands that can do serious damage, especially if you run it as the superuser. The -r option specifies recursive delete to repeatedly delete everything inside dir, and -f forces the delete operation. Don’t use the -rf flags with globs such as a star (*). And above all, always double-check your command before you run it.

如果dir不为空,该命令将失败。然而,如果你不耐烦,你可能不想费力地先删除dir内的所有文件和子目录。

你可以使用rm -rf dir命令来删除一个目录及其内容,但要小心!这是少数几个可能会造成严重损害的命令之一,特别是如果你以超级用户身份运行它。

-r选项指定递归删除以重复删除dir内的所有内容,-f强制执行删除操作。

不要在带有通配符(比如星号*)的情况下使用-rf标志。

最重要的是,在运行命令之前始终仔细检查命令。

2.4.4 Shell Globbing(通配符)

The shell can match simple patterns to file and directory names, a process known as globbing. This is similar to the concept of wildcards in other systems. The simplest of these is the glob character *, which tells the shell to match any number of arbitrary characters. For example, the following command prints a list of files in the current directory:

shell可以将简单的模式与文件和目录名匹配,这个过程称为globbing。

这类似于其他系统中通配符的概念。其中最简单的是通配符*,它告诉shell匹配任意数量的任意字符。

例如,以下命令打印出当前目录中的文件列表:

代码语言:sh
复制
$ echo *

The shell matches arguments containing globs to filenames, substitutes the filenames for those arguments, and then runs the revised command line. The substitution is called expansion because the shell substitutes all matching filenames. Here are some ways to use * to expand filenames:

shell将匹配包含通配符的参数与文件名,用文件名替换这些参数,然后运行修改后的命令行。

这个替换被称为扩展,因为shell会替换所有匹配的文件名。

下面是一些使用*扩展文件名的方法:

o at* expands to all filenames that start with at.

o *at expands to all filenames that end with at.

o at expands to all filenames that contain at.

  • at*扩展到以at开头的所有文件名。
  • *at扩展到以at结尾的所有文件名。*
  • at*扩展到包含at的所有文件名。

If no files match a glob, the shell performs no expansion, and the command runs with literal characters such as . For example, try a command such as echo dfkdsafh.

如果没有文件与通配符匹配,shell不进行扩展,命令将以字面字符运行,比如*。例如,尝试执行像echo *dfkdsafh这样的命令。

NOTE If you’re used to MS-DOS, you might instinctively type . to match all files. Break this habit now. In Linux and other versions of Unix, you must use to match all files. In the Unix shell, .* matches only files and directories that contain the dot (.) character in their names. Unix filenames do not need extensions and often do not carry them.注意如果你习惯于MS-DOS,你可能本能地输入*.*来匹配所有文件。现在要打破这个习惯。在Linux和其他版本的Unix中,你必须使用*来匹配所有文件。在Unix shell中,*.*只匹配文件名和目录名中包含点(.)字符的文件和目录。Unix文件名不需要扩展名,而且通常不带有扩展名。

Another shell glob character, the question mark (?), instructs the shell to match exactly one arbitrary character. For example, b?at matches boat and brat.

另一个shell通配符,问号(?),指示shell匹配一个任意字符。

例如,b?at匹配boat和brat。

If you don’t want the shell to expand a glob in a command, enclose the glob in single quotes (''). For example, the command echo '*' prints a star. You will find this handy for a few of the commands described in the next section, such as grep and find. (You’ll learn more much about quoting in 11.2 Quoting and Literals.)

如果你不希望shell在命令中扩展通配符,将通配符括在单引号('')中。

例如,命令echo '*'打印一个星号。

对于下一节中描述的一些命令,如grep和find,你会发现这很方便。

(你将在11.2引用和字面值中了解更多关于引用的内容。)

NOTE It is important to remember that the shell performs expansions before running commands, and only then. Therefore, if a makes it to a command without expanding, the shell will do nothing more with it; it’s up to the command to decide what it wants to do. 注意 必须记住,shell 在运行命令之前会执行扩展,而且仅在执行命令时才会这样做。因此,如果 进入命令后没有进行扩展,shell 就不会再对它做任何事情,而是由命令自己决定要做什么。

There is more to a modern shell’s pattern-matching capabilities, but * and ? are what you need to know now.

现代 shell 的模式匹配功能还有很多,但 * 和 ? 是你现在需要知道的。

2.5 Intermediate Commands(中级命令)

The following sections describe the most essential intermediate Unix commands.

以下部分介绍了最基本的中级Unix命令。

2.5.1 grep

The grep command prints the lines from a file or input stream that match an expression. For example, to print the lines in the /etc/passwd file that contain the text root, enter this:

grep命令会打印出与表达式匹配的文件或输入流中的行。

例如,要打印出包含文本"root"的/etc/passwd文件中的行,请输入以下命令:

代码语言:sh
复制
$ grep root /etc/passwd

The grep command is extraordinarily handy when operating on multiple files at once because it prints the filename in addition to the matching line. For example, if you want to check every file in /etc that contains the word root, you could use this command:

grep命令在同时操作多个文件时非常方便,因为它除了打印匹配的行外,还会打印出文件名。

例如,如果你想检查/etc目录中包含单词"root"的所有文件,可以使用以下命令:

代码语言:sh
复制
$ grep root /etc/*

Two of the most important grep options are -i (for case-insensitive matches) and -v (which inverts the search, that is, prints all lines that don’t match). There is also a more powerful variant called egrep (which is just a synonym for grep -E).

grep命令的两个最重要的选项是 -i(用于忽略大小写)和 -v(反转搜索,即打印出所有不匹配的行)。还有一个更强大的变体叫做egrep(它只是grep -E的同义词)。

grep understands patterns known as regular expressions that are grounded in computer science theory and are very common in Unix utilities. Regular expressions are more powerful than wildcard-style patterns, and they have a different syntax. There are two important things to remember about regular expressions:

grep理解称为正则表达式的模式,这些模式扎根于计算机科学理论,并且在Unix实用工具中非常常见。正则表达式比通配符样式更强大,语法也有所不同。关于正则表达式,有两个重要的事情需要记住:

o . matches any number of characters (like the in wildcards).

o . matches one arbitrary character.

o .匹配任意数量的字符(类似于通配符中的)。

o .匹配一个任意字符。

NOTE The grep(1) manual page contains a detailed description of regular expressions, but it can be a little difficult to read. To learn more, you can read Mastering Regular Expressions, 3rd edition (O’Reilly, 2006), or see the regular expressions chapter of Programming Perl, 4th edition (O’Reilly, 2012). If you like math and are interested in where regular expressions come from, look up Introduction to Automata Theory, Languages, and Computation, 3rd edition (Prentice Hall, 2006).注意grep(1)的手册页面包含了对正则表达式的详细描述,但阅读起来可能有些困难。如果想了解更多,可以阅读《精通正则表达式》第3版(O'Reilly,2006),或者查看《Perl编程》第4版(O'Reilly,2012)中的正则表达式章节。如果你喜欢数学,并且对正则表达式的来源感兴趣,请查阅《自动机理论、语言和计算导论》第3版(Prentice Hall,2006)。

2.5.2 less

The less command comes in handy when a file is really big or when a command’s output is long and scrolls off the top of the screen.

当文件非常大或者命令的输出内容很长并且超出屏幕时,less命令非常有用。

To page through a big file like /usr/share/dict/words, use the command less /usr/share/dict/words. When running less, you’ll see the contents of the file one screenful at a time. Press the spacebar to go forward in the file and the b key to skip back one screenful. To quit, type q

要浏览像/usr/share/dict/words这样的大文件,可以使用命令less /usr/share/dict/words。运行less时,你会一次看到文件的一页内容。按下空格键向前翻页,按下b键向后翻页。要退出,输入q。

NOTE The less command is an enhanced version of an older program named more. Most Linux desktops and servers have less, but it’s not standard on many embedded systems and other Unix systems. So if you ever run into a situation when you can’t use less, try more注意less命令是一个改进版本的旧程序more。大多数Linux桌面和服务器都有less,但在许多嵌入式系统和其他Unix系统中并不标准。所以,如果你遇到无法使用less的情况,请尝试使用more。

You can also search for text inside less. For example, to search forward for a word, type /word, and to search backward, use ?word. When you find a match, press n to continue searching.

你还可以在less中搜索文本。例如,要向前搜索一个单词,输入/word,要向后搜索,使用?word。当找到匹配项时,按下n键继续搜索。

As you’ll learn in 2.14 Shell Input and Output, you can send the standard output of nearly any program directly to another program’s standard input. This is exceptionally useful when you have a command with a lot of output to sift through and you’d like to use something like less to view the output. Here’s an example of sending the output of a grep command to less:

正如你将在2.14 Shell输入和输出中学到的那样,你可以将几乎任何程序的标准输出直接发送到另一个程序的标准输入。

当你有一个有大量输出需要筛选的命令,并且想要使用类似less的工具查看输出时,这非常有用。下面是一个将grep命令的输出发送到less的示例:

代码语言:sh
复制
$ grep ie /usr/share/dict/words | less

Try this command out for yourself. You’ll probably use less like this a lot.

请自己尝试一下这个命令。你可能经常会这样使用less。

2.5.3 pwd

The pwd (print working directory) program simply outputs the name of the current working directory. You may be wondering why you need this when most Linux distributions set up accounts with the current working directory in the prompt. There are two reasons.

pwd(打印当前工作目录)程序简单地输出当前工作目录的名称。你可能会想知道为什么需要这个,因为大多数Linux发行版在提示符中设置了当前工作目录。有两个原因。

First, not all prompts include the current working directory, and you may even want to get rid of it in your own prompt because it takes up a lot of space. If you do so, you need pwd.

首先,并不是所有的提示符都包含当前工作目录,你甚至可能想要在自己的提示符中去掉它,因为它占用了很多空间。如果这样做,你就需要pwd。

Second, the symbolic links that you’ll learn about in 2.17.2 Symbolic Links can sometimes obscure the true full path of the current working directory. You’ll use pwd -P to eliminate this confusion.

其次,在2.17.2符号链接中学到的符号链接有时可能会隐藏当前工作目录的真实完整路径。你将使用pwd -P来消除这种混淆。

2.5.4 diff

To see the differences between two text files, use diff:

要查看两个文本文件之间的差异,使用diff命令:

代码语言:sh
复制
$ diff file1 file2

Several options can control the format of the output, and the default output format is often the most comprehensible for human beings. However, most programmers prefer the output from diff -u when they need to send the output to someone else because automated tools can make better use of it.

有几个选项可以控制输出的格式,而默认输出格式通常对人类来说是最易理解的。

然而,大多数程序员在需要将输出发送给他人时更喜欢diff -u的输出,因为自动化工具可以更好地利用它。

2.5.5 file

If you see a file and are unsure of its format, try using the file command to see if the system can guess:

如果你看到一个文件并且不确定它的格式,可以尝试使用file命令查看系统是否能够猜测:

代码语言:sh
复制
$ file file

You may be surprised by how much this innocent-looking command can do.

你可能会对这个看似无害的命令能做多少事情感到惊讶。

2.5.6 查找和定位

It’s frustrating when you know that a certain file is in a directory tree somewhere but you just don’t know where. Run find to find file in dir:

当你知道某个文件在某个目录树中,但是却不知道具体位置时,这真是令人沮丧。使用find命令在目录中查找文件:

代码语言:sh
复制
$ find dir -name file -print

Like most programs in this section, find is capable of some fancy stuff. However, don’t try options such as -exec before you know the form shown here by heart and why you need the -name and -print options. The find command accepts special pattern-matching characters such as , but you must enclose them in single quotes ('')to protect the special characters from the shell’s own globbing feature. (Recall from 2.4.4 Shell Globbing (Wildcards) that the shell expands globs before running commands.)

和本节中的大多数程序一样,find命令也可以完成一些花哨的操作。

但是,在你完全掌握并理解这里所展示的形式以及为什么需要使用-name和-print选项之前,不要尝试像-exec这样的选项。

find命令接受特殊的模式匹配字符,比如*,但是你必须使用单引号('')将它们括起来,以保护这些特殊字符免受shell自身的通配符扩展的影响。

(回顾一下2.4.4节中介绍的Shell通配符,即shell在运行命令之前会扩展通配符。)

Most systems also have a locate command for finding files. Rather than searching for a file in real time, locate searches an index that the system builds periodically. Searching with locate is much faster than find, but if the file you’re looking for is newer than the index, locate won’t find it.

大多数系统还提供了一个locate命令用于查找文件。

与实时搜索文件不同,locate命令搜索的是系统定期构建的索引。

使用locate进行搜索比使用find快得多,但是如果你要查找的文件比索引更新,locate将无法找到它。

2.5.7 head 和 tail

To quickly view a portion of a file or stream of data, use the head and tail commands. For example, head /etc/passwd shows the first 10 lines of the password file, and tail /etc/passwd shows the last 10 lines.

要快速查看文件或数据流的一部分,可以使用head和tail命令。

例如,head /etc/passwd显示密码文件的前10行,而tail /etc/passwd显示最后10行。

要更改要显示的行数,使用-n选项,其中n是你想要查看的行数(例如,head -5 /etc/passwd)。要打印从第n行开始的行,使用tail +n。

2.5.8 sort

The sort command quickly puts the lines of a text file in alphanumeric order. If the file’s lines start with numbers and you want to sort in numerical order, use the -n option. The -r option reverses the order of the sort.

sort命令可以快速将文本文件的行按字母数字顺序排序。

如果文件的行以数字开头,并且你想按数字顺序排序,使用-n选项。-r选项可以反转排序的顺序。

2.6 Changing Your Password and Shell(修改密码和Shell)

Use the passwd command to change your password. You’ll be asked for your old password and then prompted for your new password twice. Choose a password that does not include real words in any language and don’t try to combine words.

使用passwd命令来修改密码。你将会被要求输入旧密码,然后再输入新密码两次。选择一个不包含任何语言中的真实单词,并且不尝试组合单词的密码。

One of the easiest ways to create a good password is to pick a sentence, produce an acronym from it, and then modify the acronym with a number or some punctuation. Then all you need to do is remember the sentence.

创建一个好的密码的最简单方法之一是选择一个句子,从中产生一个首字母缩写,然后用数字或标点符号修改这个缩写。然后你只需要记住这个句子。

You can change your shell with the chsh command (to an alternative such as ksh or tcsh), but keep in mind that this book assumes that you’re running bash

你可以使用chsh命令来更改你的Shell(例如ksh或tcsh),但请记住,本书假设你正在运行bash。

2.7 Dot Files(点文件)

Change to your home directory, take a look around with ls, and then run ls -a. Do you see the difference in the output? When you run ls without the -a, you won’t see the configuration files called dot files. These are files and directories whose names begin with a dot (.). Common dot files are .bashrc and .login, and there are dot directories, too, such as .ssh.

切换到你的主目录,用ls命令查看一下,然后运行ls -a。

你看到输出的差异了吗?当你运行ls而不带上-a选项时,你看不到被称为点文件的配置文件。

这些是以点(.)开头的文件和目录。

常见的点文件有.bashrc和.login,还有点目录,比如.ssh。

There is nothing special about dot files or directories. Some programs don’t show them by default so that you won’t see a complete mess when listing the contents of your home directory. For example, ls doesn’t list dot files unless you use the -a option. In addition, shell globs don’t match dot files unless you explicitly use a pattern such as .*.

点文件或目录并没有什么特别之处。

有些程序默认不显示它们,这样当列出主目录的内容时,你不会看到一团糟。

例如,ls命令不会列出点文件,除非你使用了-a选项。

此外,Shell通配符不会匹配点文件,除非你明确使用模式,比如.*。

NOTE You can run into problems with globs because . matches . and .. (the current and parent directories). You may wish to use a pattern such as .^. or .??* to get all dot files except the current and parent directories.注意通配符可能会导致问题,因为.*会匹配.和..(当前目录和父目录)。你可能希望使用类似.^.或.??的模式来获取除当前目录和父目录之外的所有点文件。

2.8 Environment and Shell Variables(环境和Shell变量)

The shell can store temporary variables, called shell variables, containing the values of text strings. Shell variables are very useful for keeping track of values in scripts, and some shell variables control the way the shell behaves. (For example, the bash shell reads the PS1 variable before displaying the prompt.)

Shell可以存储临时变量,称为Shell变量,其中包含文本字符串的值。

Shell变量在脚本中跟踪值非常有用,而且一些Shell变量可以控制Shell的行为方式(例如,bash Shell在显示提示符之前会读取PS1变量)。

To assign a value to a shell variable, use the equal sign (=). Here’s a simple example:

要给Shell变量赋值,使用等号(=)。下面是一个简单的示例:

代码语言:sh
复制
$ STUFF=blah

上面的示例将名为STUFF的变量的值设置为blah。要访问这个变量,使用$STUFF(例如,尝试运行echo $STUFF)。你将在第11章中了解到Shell变量的许多用途。

环境变量类似于Shell变量,但它不是特定于Shell。Unix系统上的所有进程都有环境变量存储空间。环境变量和Shell变量的主要区别在于,操作系统将你的Shell的所有环境变量传递给Shell运行的程序,而Shell变量无法在你运行的命令中访问。

使用Shell的export命令来分配一个环境变量。例如,如果你想将$STUFF Shell变量变成环境变量,使用以下命令:

$ STUFF=blah

$ export STUFF

环境变量非常有用,因为许多程序会读取它们来进行配置和选项。例如,你可以将你最喜欢的less命令行选项放在LESS环境变量中,当你运行less时,它将使用这些选项。(许多手册页包含一个标记为ENVIRONMENT的部分,描述了这些变量。)

2.9 The Command Path(命令路径)

PATH是一个特殊的环境变量,它包含了命令路径(或简称为路径)。命令路径是一个系统目录的列表,当shell尝试定位一个命令时,它会在这些目录中进行搜索。例如,当你运行ls命令时,shell会在PATH列出的目录中搜索ls程序。如果路径中的多个目录中都有同名的程序,shell会运行第一个匹配的程序。

如果你运行echo $PATH命令,你会看到路径的组成部分是由冒号(:)分隔的。例如:

$ echo $PATH

/usr/local/bin:/usr/bin:/bin

要告诉shell在更多地方寻找程序,可以修改PATH环境变量。例如,通过使用这个命令,你可以将目录dir添加到路径的开头,这样shell会在dir中查找程序,而不是在其他PATH目录中查找。

$ PATH=dir:$PATH

或者你可以将一个目录名追加到PATH变量的末尾,这会导致shell最后在dir中查找:

$ PATH=$PATH:dir

注意

修改路径时要小心,因为如果你错误地输入了$PATH,可能会意外清空整个路径。如果发生这种情况,不要惊慌!损坏并不是永久的;你可以重新启动一个新的shell。(要实现持久效果,你需要在编辑某个配置文件时错误地输入,即使如此,也很容易纠正。)返回正常状态的最简单方法之一是关闭当前的终端窗口并启动另一个窗口。

2.10 Special Characters(特殊字符)

When discussing Linux with others, you should know a few names for some of the special characters that you’ll encounter. If you’re amused by this sort of thing, see the “Jargon File” (http://www.catb.org/jargon/html/) or its printed companion, The New Hacker’s Dictionary (MIT Press, 1996). Table 2-1 describes a select set of the special characters, many of which you’ve already seen in this chapter. Some utilities, such as the Perl programming language, use almost all of these special characters! (Keep in mind that these are the American names for the characters.)

当与他人讨论 Linux 时,你应该了解一些特殊字符的名称,因为你会遇到它们。

如果你对这类事情感到有趣,可以参考《术语文件》(http://www.catb.org/jargon/html/)或其印刷版《新黑客词典》(MIT出版社,1996年)。

表2-1描述了一些特殊字符的选择集,其中许多你在本章中已经见过。

一些工具,如 Perl 编程语言,几乎使用了所有这些特殊字符!(请记住,这些是这些字符的美国名称。)

Table 2-1. Special Characters
Table 2-1. Special Characters

Table 2-1. Special Characters

Table 2-1. Special Characters
Table 2-1. Special Characters

Table 2-1. Special Characters

You will often see control characters marked with a caret; for example, ^C for ctrl-c.

您经常会看到用圆点标记的控制字符,例如,^C 表示 ctrl-c。

2.11 Command-Line Editing(命令行编辑)

As you play with the shell, notice that you can edit the command line using the left and right arrow keys, as well as page through previous commands using the up and down arrows. This is standard on most Linux systems.

当你使用shell时,你会注意到你可以使用左右箭头键来编辑命令行,还可以使用上下箭头键浏览之前的命令。

这在大多数Linux系统上都是标准的。

However, it’s a good idea to forget about the arrow keys and use control key sequences instead. If you learn the ones listed in Table 2-2, you’ll find that you’re better able to enter text in the many Unix programs that use these standard keystrokes.

然而,最好忘掉箭头键,而是使用控制键序列。

如果你学会了表2-2中列出的控制键序列,你会发现你能更好地在许多使用这些标准按键的Unix程序中输入文本。

Table 2-2. Command-Line Keystrokes

Table 2-2. Command-Line Keystrokes
Table 2-2. Command-Line Keystrokes

2.12 Text Editors(文本编辑器)

Speaking of editing, it’s time to learn an editor. To get serious with Unix, you must be able to edit text files without damaging them. Most parts of the system use plaintext configuration files (like the ones in /etc). It’s not difficult to edit files, but you will do it so often that you need a powerful tool for the job.

说到编辑,现在是时候学习一个编辑器了。

要认真对待Unix,你必须能够在不损坏文件的情况下编辑文本文件。

系统的大部分部分使用纯文本配置文件(比如在/etc目录下的文件)。

编辑文件并不困难,但你会经常这样做,所以需要一个强大的工具来完成这项工作。

You should try to learn one of the two de facto standard Unix text editors, vi and Emacs. Most Unix wizards are religious about their choice of editor, but don’t listen to them. Just choose for yourself. If you choose one that matches the way that you work, you’ll find it easier to learn. Basically, the choice comes down to this:

你应该尝试学习两个事实上的标准Unix文本编辑器之一,vi和Emacs。

大多数Unix专家对于他们选择的编辑器都有宗教般的热爱,但不要听他们的。

只需选择适合自己工作方式的编辑器即可,这样你会发现学习起来更容易。

基本上,选择编辑器有以下几点:

o If you want an editor that can do almost anything and has extensive online help, and you don’t mind doing ome extra typing to get these features, try Emacs.

o If speed is everything, give vi a shot; it “plays” a bit like a video game.

  • 如果你想要一个几乎可以做任何事情并且有广泛在线帮助的编辑器,而且不介意为了使用这些功能而多输入一些命令,可以尝试Emacs。
  • 如果速度至关重要,可以试试vi;它的使用方式有点像玩电子游戏。

Learning the vi and Vim Editors: Unix Text Processing, 7th edition (O’Reilly, 2008) can tell you everything you need to know about vi. For Emacs, use the online tutorial: Start Emacs, press CTRL-H, and then type T. Or read GNU Emacs Manual (Free Software Foundation, 2011).

《学习vi和Vim编辑器:Unix文本处理,第7版》(O'Reilly,2008)可以告诉你关于vi的一切。

对于Emacs,可以使用在线教程:启动Emacs,按下CTRL-H,然后输入T。

或者阅读《GNU Emacs手册》(自由软件基金会,2011)。

You might be tempted to experiment with a friendlier editor when you first start out, such as Pico or one of the myriad GUI editors out there, but if you tend to make a habit out of the first thing that you use, you don’t want to go down this route.

当你刚开始时,可能会尝试使用一个更友好的编辑器,比如Pico或者其他各种图形界面编辑器,但如果你倾向于养成第一次使用的习惯,就不要走这条路。

NOTE Editing text is where you’ll first start to see a difference between the terminal and the GUI. Editors such as vi run inside the terminal window, using the standard terminal I/O interface. GUI editors start their own window and present their own interface, independent of terminals. Emacs runs in a GUI by default but will run in a terminal window as well.注意编辑文本是你首次开始看到终端和图形界面之间差异的地方。例如vi这样的编辑器在终端窗口内运行,使用标准终端I/O接口。图形界面编辑器则启动自己的窗口,并呈现独立于终端的界面。Emacs默认在图形界面中运行,但也可以在终端窗口中运行。2.13 Getting Online Help(获取在线帮助)

Linux systems come with a wealth of documentation. For basic commands, the manual pages (or man pages) will tell you what you need to know. For example, to see the manual page for the ls command, run man as follows:

Linux系统提供了丰富的文档资源。对于基本命令,手册页(或man页)会告诉您所需的信息。

例如,要查看ls命令的手册页,请运行以下命令:

代码语言:sh
复制
$ man ls

Most manual pages concentrate primarily on reference information, perhaps with some examples and crossreferences, but that’s about it. Don’t expect a tutorial, and don’t expect an engaging literary style

大多数手册页主要集中在参考信息上,可能会包含一些示例和交叉引用,但仅此而已。

不要期望有教程,也不要期望有引人入胜的文学风格。

When programs have many options, the manual page often lists the options in some systematic way (for example, in alphabetical order), but it won’t tell you what the important ones are. If you’re patient, you can usually find what you need to know in the man page. If you’re impatient, ask a friend—or pay someone to be your friend so that you can ask him or her.

当程序有许多选项时,手册页通常会以某种系统的方式列出这些选项(例如按字母顺序),但它不会告诉您哪些是重要的选项。

如果您耐心一点,通常可以在手册页中找到所需的信息。

如果您不耐烦,可以向朋友询问,或者付钱请人充当您的朋友,这样您就可以向他们提问。

To search for a manual page by keyword, use the -k option:

要按关键字搜索手册页,请使用-k选项:

代码语言:sh
复制
$ man -k keyword

This is helpful if you don’t quite know the name of the command that you want. For example, if you’re looking for a command to sort something, run:

如果您不太知道所需命令的名称,这将很有帮助。例如,如果您正在寻找一个用于排序的命令,请运行:

代码语言:sh
复制
$ man -k sort
--snip--
comm (1) - compare two sorted files line by line
qsort (3) - sorts an array
sort (1) - sort lines of text files
sortm (1) - sort messages
tsort (1) - perform topological sort
--snip--

The output includes the manual page name, the manual section (see below), and a quick description of what the manual page contains.

输出结果包括手册页面名称、手册部分(见下文)以及手册页面内容的快速描述。

NOTE If you have any questions about the commands described in the previous sections, you may be able to find the answers by using the man command.注意 如果您对前面章节中描述的命令有任何疑问,可以使用 man 命令找到答案。

Manual pages are referenced by numbered sections. When someone refers to a manual page, the section number appears in parentheses next to the name, like ping(8), for example. Table 2-3 lists the sections and their numbers.

手册页面按章节编号引用。当有人引用手册页面时,章节编号会出现在名称旁边的括号中,例如 ping(8)。表 2-3 列出了章节及其编号。

Table 2-3. Online Manual Sections

表 2-3. 联机手册章节

Table 2-3. Online Manual Sections
Table 2-3. Online Manual Sections

Sections 1, 5, 7, and 8 should be good supplements to this book. Section 4 may be of marginal use, and Section 6 would be great if only it were a little larger. You probably won’t be able to use Section 3 if you aren’t a programmer, but you may be able to understand some of the material in Section 2 once you’ve read more about system calls in this book.

第1、5、7和8节应该是这本书的很好的补充。第4节可能有一些边缘用途,如果第6节能再大一点就更好了。

如果你不是程序员,可能无法使用第3节,但是在阅读本书关于系统调用的更多内容后,你可能能够理解第2节的一些材料。

You can select a manual page by section, which is sometimes important because man displays the first manual page that it finds when matching a particular search term. For example, to read the /etc/passwd file description (as opposed to the passwd command), you can insert the section number before the page name:

您可以按节选择手册页,这有时很重要,因为当匹配特定搜索词时,man会显示它找到的第一个手册页。

例如,要阅读/etc/passwd文件的描述(而不是passwd命令),您可以在页面名称之前插入节号:

代码语言:sh
复制
$ man 5 passwd

Manual pages cover the essentials, but there are many more ways to get online help. If you’re just looking for a certain option for a command, try entering a command name followed by --help or -h (the option varies from command to command). You may get a deluge (as in the case of ls --help), or you may find just what you’re looking for

手册页涵盖了基本内容,但还有许多其他获取在线帮助的方法。

如果您只是想要某个命令的特定选项,请尝试输入命令名称后跟--help或-h(选项因命令而异)。

您可能会得到一大堆信息(例如ls --help的情况),或者您可能只找到您需要的内容。

Some time ago, the GNU Project decided that it didn’t like manual pages very much and switched to another format called info (or texinfo). Often this documentation goes further than a typical manual page does, but it is sometimes more complex. To access an info manual, use info with the command name:

一段时间以前,GNU项目决定不太喜欢手册页,而是转向另一种称为info(或texinfo)的格式。

通常,这种文档比典型的手册页更详细,但有时更复杂。

要访问info手册,请使用info和命令名称:

代码语言:sh
复制
$ info command

Some packages dump their available documentation into /usr/share/doc with no regard for online manual systems such as man or info. See this directory on your system if you find yourself searching for documentation. And of course, search the Internet.

一些软件包会将其可用的文档转储到/usr/share/doc中,而不考虑man或info等在线手册系统。

如果您发现自己在搜索文档,请查看您系统上的此目录。

当然,还可以在互联网上搜索。

2.14 Shell Input and Output( Shell 输入和输出)

Now that you’re familiar with basic Unix commands, files, and directories, you’re ready to learn how to redirect standard input and output. Let’s start with standard output.

现在你已经熟悉了基本的Unix命令、文件和目录,你可以学习如何重定向标准输入和输出了。我们先从标准输出开始。

To send the output of command to a file instead of the terminal, use the > redirection character:

要将命令的输出发送到文件而不是终端,请使用重定向字符 >:

代码语言:sh
复制
$ command > file

The shell creates file if it does not already exist. If file exists, the shell erases (clobbers) the original file first. (Some shells have parameters that prevent clobbering. For example, enter set -C to avoid clobbering in bash.)

如果文件不存在,shell会创建该文件。

如果文件已经存在,shell会先擦除(覆盖)原文件。(一些shell有参数可以防止擦除。

例如,在bash中输入set -C可以避免擦除。)

你也可以使用 >> 重定向语法将输出追加到文件中而不是覆盖它:

代码语言:sh
复制
$ command >> file

This is a handy way to collect output in one place when executing sequences of related commands.

这是在执行相关命令序列时,将输出收集到一个地方的便捷方式。

To send the standard output of a command to the standard input of another command, use the pipe character (|). To see how this works, try these two commands:

要将一个命令的标准输出发送到另一个命令的标准输入,使用管道符号 |。

试试下面这两个命令,看看它们是如何工作的:

代码语言:sh
复制
$ head /proc/cpuinfo
$ head /proc/cpuinfo | tr a-z A-Z

You can send output through as many piped commands as you wish; just add another pipe before each additional command.

你可以通过任意多个管道命令发送输出,只需在每个附加命令之前添加另一个管道符号。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 第 2 章 Basic Commands and Directory Hierarchy(基本命令和目录层次结构)
    • 2.1 The Bourne Shell: /bin/sh(伯恩 shell /bin/sh)
      • 2.2.1 The Shell Window(shell 窗口)
      • 2.2.2 cat
      • 2.2.3 Standard Input and Standard Output(标准输入和标准输出)
    • 2.3 Basic Commands(基本命令)
      • 2.3.1 ls
      • 2.3.2 cp
      • 2.3.3 mv
      • 2.3.4 touch
      • 2.3.5 rm
      • 2.3.6 echo
    • 2.4 Navigating Directories(目录导航)
      • 2.4.1 cd
      • 2.4.2 mkdir
      • 2.4.3 rmdir
      • 2.4.4 Shell Globbing(通配符)
    • 2.5 Intermediate Commands(中级命令)
      • 2.5.1 grep
      • 2.5.2 less
      • 2.5.3 pwd
      • 2.5.4 diff
      • 2.5.5 file
      • 2.5.6 查找和定位
      • 2.5.7 head 和 tail
      • 2.5.8 sort
    • 2.6 Changing Your Password and Shell(修改密码和Shell)
      • 2.7 Dot Files(点文件)
        • 2.8 Environment and Shell Variables(环境和Shell变量)
          • 2.9 The Command Path(命令路径)
            • 2.10 Special Characters(特殊字符)
              • 2.11 Command-Line Editing(命令行编辑)
                • 2.12 Text Editors(文本编辑器)
                  • 2.14 Shell Input and Output( Shell 输入和输出)
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档