命令行星期二——第六部分
命令行星期二 命令行
不管怎样，让我们继续我们今天的日程吧：输入/输出重定向。


我们已经用过的大多数命令，都把他们的结果张贴到了终端显示中。然而，我们也可以让这些命令的显示放进一个文件、设备中，或者作为其它命令的输入，一切都在**重定向**。


### 标准输出


那么，命令行程序主要是怎么工作的呢？它们会将命令的结果丢给一个叫做标准输出的设备，然后，标准输出会将这些结果发送到你的显示设备上，这样你就可以畅快地阅读这些信息了。然而，你也可以让命令的结果显示到一个文本文件中，像这样，用“>”字符（我的例子）：



```
man zypper > man.txt

```

这儿我所干的是， 我想要获得zypper的一个帮助页。但是，我想要把它放到一个文本文件中去，因而我可以在我需要他们的时候（例如，在论坛、IRC寻求帮助时）很方便地读取这些结果。所以，这个命令做的是，保存man zypper输出结果到我的家目录中的man.txt，这样你可以看到，我可以用gedit来打开它：


![](/data/attachment/album/201408/03/100718ttvutwqwsuezotlq.png)


如果我重复了该命令，或者使用不同的命令重定向到了同名文件，那么它就会被一次又一次地覆盖。所以，在那种情况下，如果我想要把输出结果重定向并附加到现存文件中，我唯一能做的事情就是下一次使用“>>”来替换“>”。


### 标准输入


许多命令也可以接受来自称之为标准输入的设备的输入。默认情况下，输入来自键盘，然而也可以来自一个文件。要重定向它，使用跟前面相反的字符：‘**<**’。


请看下面的例子：



```
sort < man.txt

```

### 管道


要想把多个命令连接到一起，你可以使用管道。管道所要做的是，将一个命令的标准输出注入到另一个命令的标准输入。肖茨先生的最爱：



```
ls – l | less

```

通过使用‘| less’这个小把戏，你可以让任何命令滚动输出。 过滤器


### 过滤器


过滤器是管道中常用的程序。过滤器获取标准输入，对它进行操作，然后把结果发送到标准输出。它们可以以一种相当强大的方式处理信息。这儿提供了一个程序列表，大多数都是用作过滤器的。它们可以用于执行、查询，或者将帮助页重定向到它们，然后再重定向到一个文件！


过滤器：sort, uniq, grep （我们发现这一个东西遍布论坛，因此我建议大家深入研究一下）, fmt, pr, head, tail, tr, sed, awk。


### 结束


我们只剩下几个课程来阐述CLI的基本用法了，到成熟期的时候，我希望大家都会对CLI兴趣会增多一点了，也希望大家在萧条期也进行一些探索！而在你们动手干的时候，我希望你们…… **玩得不亦乐乎！**




---


via: <https://news.opensuse.org/2014/07/30/command-line-somedays-part-six/>


作者：[Nenad Latinović](https://news.opensuse.org/author/holden87/) 译者：[GOLinux](https://github.com/GOLinux) 校对：[wxy](https://github.com/wxy)


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


OK
# Command Line "Somedays" - Part Six

30. Jul 2014 | Nenad Latinović | No License

Hey guys. Yes, it’s Wednesday, and yes, we’re a day late. Reason? [This awesome announcement](https://news.opensuse.org/2014/07/29/factory-rolling-release/), that had to take the whole glory of Tuesday. Also, in the future, if there’s an important announcement/release announcement from the project, the CLT series will be postponed.

Anyway, let’s move on to our today’s agenda: The input/output redirection.

Most commands we have used, post their results in the terminal display. But, we can also make them display it in a file, device or input of other commands by **redirecting them**.

##### Standard output

So, how it basically works is that command line programs usually send the results of commands to a facility called standard output. The standard output then usually sends it to your display, so you can read off of it. But, you can also make it display the results in a text file, like this, using the** ‘>’** character (my case):

man zypper > man.txt

What I did here, is I wanted a man page for zypper. But I wanted it in a text file, so I can easily access the results in case I need them (asking for help on the forums, IRC, f.ex.). So what this command did is that it saved the output of man zypper to man.txt in my home folder, which you can see here, opened in gedit (click to enlarge):

If I should repeat the command, or use different commands with the same file name output, it would rewrite the file over and over again. So, in that case, if I’d like to have the output redirection append the next results to the existing file, the only thing I have to do is use

**’»‘**instead of** ‘>’** the next time.

##### Standard input

Many commands can also accept input from a facility called standard input. By default, it comes from a keyboard, but it can also come from a file. To redirect it, use the inverted character from the one before: **’<’**

The example given is the following:

sort < man.txt

##### Pipeline

To connect multiple commands together, you use *pipelines*. What pipelines do, is feed the standard output of one command into the standard input of the other command. Mr Shotts’ favorite:

ls - l | less

By using this’ | less’ trick, you can make any command have scrolling output. |


##### Filters

Filters is a commonly used program in pipelines. Filters take a standard input, perform an operation on it, and send the results to the standard output. They can process information in a very powerful way. Here’s a list of the programs, most commonly used as filters. For exercise, look up, or redirect man pages to them to a file!

Filters: sort, uniq, grep (we’ve all seen this one appear all over forums, so I suggest we look into it more), fmt, pr, head, tail, tr, sed, awk.

##### Finito

Once again, halting to a stop for this week. We have only a couple of lessons left, to cover the basics of the CLI, and by the time autumn comes, I hope you’ll be all interested in CLI a little more and continue with some winter explorations! And while doing it, I hope you’ll…

##### …have a lot of fun!

**Categories:**
[
Distribution
](/category/distribution)

**Tags:**