个人工具

“UbuntuHelp:CommandlineHowto/zh”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
 
(未显示同一用户的6个中间版本)
第1行: 第1行:
 
{{From|https://help.ubuntu.com/community/CommandlineHowto}} {{Languages|UbuntuHelp:CommandlineHowto}}
 
{{From|https://help.ubuntu.com/community/CommandlineHowto}} {{Languages|UbuntuHelp:CommandlineHowto}}
  
{{Translator|佚名,杏林小草}}
+
{{Translator|机枪兵,杏林小草}}
  
 
== 介绍 ==
 
== 介绍 ==
第15行: 第15行:
 
=== 命令行的历史 ===
 
=== 命令行的历史 ===
  
在计算机出现的早期,命令行是唯一的人机交互方式。后来,'''Xerox's Palo Alto 研究中心(PARC)''' 提出了图形用户界面(GUI)的概念,以后的大多数 GUI 都是基于它的概念构建的。后来,苹果花了一大笔钱,才被允许“研究”他们自己的 GUI。苹果拥有了自己的 GUI 后,直到 1986 年,Unix 上终于出现了第一个 GUI,它被命名为 X。不过 Linux 不得不再等了 10 年,才发布 XFree86。它至今仍是当初 X server 的免费实现版本。
+
在计算机出现的早期,命令行是唯一的人机交互方式。後来,'''Xerox's Palo Alto 研究中心(PARC)''' 提出了图形用户界面(GUI)的概念,以後的大多数 GUI 都是基于它的概念构建的。後来,苹果花了一大笔钱,才被允许“研究”他们自己的 GUI。苹果拥有了自己的 GUI 後,直到 1986 年,Unix 上终于出现了第一个 GUI,它被命名为 X。不过 Linux 不得不再等了 10 年,才发布 XFree86。它至今仍是当初 X server 的免费实现版本。
  
之前我介绍过,CLI(命令行界面)是图形界面出来之前唯一的人机交互途径。1969 年,'''贝尔电话实验室''' 发布了 UNIX Timeshare System 的 V1 版本。UNIX 有了一个 shell,称为 sh。很长一段时间内它都是同计算机进行交流的唯一渠道。到了后来,UNIX 出现了衍生版本:HP-UX,1BSD,Solaris,OpenVMS,IRIX,SCO XENIX,等等。再接着,Linux 出现了。Linux 的发展历程同本文关系不大,总之 sh 之外的 CLI 开始多了起来:zsh,ksh,bourne shell,等等。
+
之前我介绍过,CLI(命令行界面)是图形界面出来之前唯一的人机交互途径。1969 年,'''贝尔电话实验室''' 发布了 UNIX Timeshare System 的 V1 版本。UNIX 有了一个 shell,称为 sh。很长一段时间内它都是同计算机进行交流的唯一渠道。到了後来,UNIX 出现了衍生版本:HP-UX,1BSD,Solaris,OpenVMS,IRIX,SCO XENIX,等等。再接着,Linux 出现了。Linux 的发展历程同本文关系不大,总之 sh 之外的 CLI 开始多了起来:zsh,ksh,bourne shell,等等。
  
 
==== 可移植操作系统接口(POSIX) ====
 
==== 可移植操作系统接口(POSIX) ====
  
维基百科是这样定义可移植操作系统接口(POSIX)的:“可移植操作系统接口是电气电子工程师学会(IEEE)定义的一组标准,旨在为Unix及其变体上运行的应用程序提供统一的接口(API)。正式的称呼是IEEE 1003,国际标准化名称是ISO/IEC 9945。本标准的构建大约始于1985年。最初的名字是IEEE-IX,後来,应电气电子工程师学会的要求,Richard Stallman将本标准命名为POSIX。POSIX的前四个字母代表可移植(Portal)操作系统(Operation System)界面(Interface),X代表Unix血统的API。”如今,POSIX已经是底层标准。<br>
+
维基百科是这样定义可移植操作系统接口(POSIX)的:“可移植操作系统接口是电气电子工程师学会(IEEE)定义的一组标准,旨在为Unix及其变体上运行的应用程序提供统一的接口(API)。正式的称呼是IEEE 1003,国际标准化名称是ISO/IEC 9945。本标准的构建大约始于1985年。最初的名字是IEEE-IX,后来,应电气电子工程师学会的要求,Richard Stallman将本标准命名为POSIX。POSIX的前四个字母代表可移植(Portal)操作系统(Operation System)界面(Interface),X代表Unix血统的API。”如今,POSIX已经是底层标准。<br>
  
 
=== 使用命令行的优势 ===
 
=== 使用命令行的优势 ===
第82行: 第82行:
 
and you have the bird-flu!
 
and you have the bird-flu!
 
</pre>  
 
</pre>  
上面的例子告诉我们,通过在''cat''命令后面加上文件名作为参数,就可以显示文件file1.txt的内容。几乎所有的命令,不管有没有参数,都有选项,格式是'''命令 -选项'''(译注:在DOS下,选项也被称为开关)。例如:
+
上面的例子告诉我们,通过在''cat''命令後面加上文件名作为参数,就可以显示文件file1.txt的内容。几乎所有的命令,不管有没有参数,都有选项,格式是'''命令 -选项'''(译注:在DOS下,选项也被称为开关)。例如:
 
<pre>dud@shadowplay:~ $ ls -r
 
<pre>dud@shadowplay:~ $ ls -r
 
file-with_other-NAME.TXT
 
file-with_other-NAME.TXT
第92行: 第92行:
 
file1.txt
 
file1.txt
 
</pre>  
 
</pre>  
我们看到,加上''-r''选项之后,ls命令就以逆序列出当前目录的内容。
+
我们看到,加上''-r''选项之後,ls命令就以逆序列出当前目录的内容。
  
 
=== 多个命令 ===
 
=== 多个命令 ===
  
Sometimes the desired task may require the use of more than one command to be completed. Here is the syntax for the use of multiple commands. If you want to execute two commands consecutively, then you would use the following syntax: '''command1&nbsp;; command2''' Example:
+
有时,完成一项任务需要使用多个命令。现在我们就来讲解使用多个命令的语法。如果你想按顺序执行两个命令,可以使用分号语法:'''命令1;命令2'''。例如:
 
<pre>dud@shadowplay:~ $grep red file1.txt&nbsp;; grep blue file1.txt
 
<pre>dud@shadowplay:~ $grep red file1.txt&nbsp;; grep blue file1.txt
 
Roses are red,
 
Roses are red,
 
Violets are blue,
 
Violets are blue,
 
</pre>  
 
</pre>  
In the example above, command1 and command2 are executed. However, if you need command1 to complete successfully before executing command2, then you would use the following syntax: '''command1 &amp;&amp; command2''' Example:
+
上例说明命令1和命令2都被执行。如果你要求命令1成功执行後,才能再执行命令2,那么就应该使用“&amp;&amp;”语法:'''命令1 &amp;&amp; 命令2'''。例如:
 
<pre>dud@shadowplay:~ $ grep red file1.txt &amp;&amp; grep blue file1.txt
 
<pre>dud@shadowplay:~ $ grep red file1.txt &amp;&amp; grep blue file1.txt
 
Roses are red,
 
Roses are red,
第108行: 第108行:
 
dud@shadowplay:~ $
 
dud@shadowplay:~ $
 
</pre>  
 
</pre>  
In the example above, you will notice nothing happened when the first command did not complete successfully. If you want command2 to execute only if command1 fails, then you would use the following syntax: '''command1 || command2''' Example:
+
通过上面的例子,我们注意到如果第一个命令未能成功执行,则不执行第二个命令。如果你希望只有在命令1执行失败的时候才执行命令2,那么可以使用“||”语法:'''命令1||命令2'''。例如:
 
<pre>dud@shadowplay:~ $ grep red file1.txt || grep blue file1.txt
 
<pre>dud@shadowplay:~ $ grep red file1.txt || grep blue file1.txt
 
Roses are red,
 
Roses are red,
第115行: 第115行:
 
dud@shadowplay:~ $
 
dud@shadowplay:~ $
 
</pre>  
 
</pre>  
In the example above, you will notice command2 was only executed when command1 failed.
+
上面的例子告诉我们,只有在第一个命令失败的时候,第二个命令才执行。
  
 
=== 通配符 ===
 
=== 通配符 ===
  
There is a powerful feature when using arguments called wildcards, which are single or multiple characters that can represent anything or a specific range of characters. The syntax for the wildcard is as follows: '''command *''' Example:
+
通配符是一种强大的语法,它用在命令的参数中,可以代表单个或任意个特定或全部字符。第一个介绍的通配符是“*”,它代表任意个任意字符(杏林小草注:任意个包括了零个),语法为 '''命令 *''' 。例如:
 
<pre>dud@shadowplay:~ $ ls f*
 
<pre>dud@shadowplay:~ $ ls f*
 
file1.txt
 
file1.txt
第127行: 第127行:
 
file-with_other-NAME.TXT
 
file-with_other-NAME.TXT
 
</pre>  
 
</pre>  
The example above gives a list of all files that start with the letter f. The next wildcard we will use allows you to match any single character. The syntax for this wildcard is as follows: '''command&nbsp;?''' Example:
+
在上面的例子中,我们列出了当前目录下所有以字母“f”开头的文件。下一个介绍的通配符是“?”,它代表单个任意字符,语法为 '''命令&nbsp;?'''。例如:
 
<pre>dud@shadowplay:~ $ ls file?.pdf
 
<pre>dud@shadowplay:~ $ ls file?.pdf
 
file2.pdf
 
file2.pdf
 
</pre>  
 
</pre>  
The example above gives you output of only one file that fits the pattern. Finally, the following wildcard allows you to use a range of values, and its syntax is: '''command [abc...]''' Example:
+
上例只有一个文件符合用户要求的样式。最後,语法 '''命令 [abc...]''' 允许你使用括号中指定的字符。例如:
 
<pre>dud@shadowplay:~ $ ls file[0-9].*
 
<pre>dud@shadowplay:~ $ ls file[0-9].*
 
file1.txt
 
file1.txt
第138行: 第138行:
 
file1.pdf
 
file1.pdf
 
</pre>  
 
</pre>  
In the above example, there were two wildcards used: the range wildcard followed by matching any strings wildcard. You can see with the above examples just how powerful using wildcards can be.
+
上例使用了两种通配符,范围通配符([])和任意通配符(*)。从这个例子我们可以了解到通配符的威力多么的大!
 +
 
 +
<br>
 +
 
 +
杏林小草注:事实上,所有的shell都支持通配符;而且,展开通配符的不是命令,而是shell,这是linux命令行与DOS命令行的区别。其它常用的通配符还有:
 +
 
 +
[a-c]:从“a”开始,到“c”结束的所有字符。符号“-”除外。<br>
 +
 
 +
[!abc]:所有的字符,除了“abc”之外。<br>[!a-c]:你可能猜到了,它的意思是除了从“a”开始,到“c”结束的字符外的所有字符。<br>
 +
 
 +
{词1,词2,词3,...}:这个就复杂点了,举例来说吧,file_{one,two,three}代表file_one、file_two、file_three。明白了吧?
  
 
=== 控制流 ===
 
=== 控制流 ===
  
Commands read input from the keyboard (standard input, or stdin) and write to output (standard out, or stdout). There is also a special output category for error messages called standard error (or stderr). These three locations are created automatically for each program. We can redirect input and output to and from a command.
+
命令的输入来自键盘(称标准输入或stdin),然后输出到屏幕(称标准输出或stdout)。还有一个特殊的标准错误输出,专门用于显示错误信息。系统自动为每一个程序创建这三个输入输出设备。用户可以重新定向这三种信息流。
  
 
==== 重定向 ====
 
==== 重定向 ====
  
If you wanted the output of a command to go to a file instead of the terminal, then you would use the following syntax: '''command &gt; filename''' Example:
+
如果你希望命令执行的结果输出到文件而非终端,可以使用这个语法: '''命令 &gt; 文件名'''。例如:
 
<pre>dud@shadowplay:~ $ ls &gt; file4.txt
 
<pre>dud@shadowplay:~ $ ls &gt; file4.txt
 
dud@shadowplay:~ $ cat file4.txt
 
dud@shadowplay:~ $ cat file4.txt
第158行: 第168行:
 
file4.txt
 
file4.txt
 
</pre>  
 
</pre>  
The above example will create file4.txt if it is not found. '''NOTE:''' If file4.txt exists already, the above command will overwrite its contents. If you want to add to the end of a existing file, then you would use the following syntax: '''command &gt;&gt; filename''' Example:
+
在上面的例子中,如果file4.txt不存在,那么系统会自动创建这个文件。'''注意:'''如果file4.txt已经存在,那么上述操作会覆盖原来的文件。如果你希望命令执行的结果追加到原有文件的后面,可以使用这个语法: '''命令 &gt;&gt; 文件名'''。例如:
 
<pre>dud@shadowplay:~ $ ls &gt;&gt; file4.txt
 
<pre>dud@shadowplay:~ $ ls &gt;&gt; file4.txt
 
dud@shadowplay:~ $ cat file4.txt
 
dud@shadowplay:~ $ cat file4.txt
第178行: 第188行:
 
file4.txt
 
file4.txt
 
</pre>  
 
</pre>  
In the example, you will notice the file was appended with the new information. Now we are going to do a different redirection: We are going to take the input from a file for the command to be executed. Here is the syntax for this redirection: '''command &lt; filename''' Example:
+
在上例中,新的信息追加到了旧文件的尾部。现在,我们来看看另一个方向的重定向----我们用文件作为命令的输入。语法是这样的: '''命令 &lt; 文件名'''。例如:
 
<pre>dud@shadowplay:~ $ sort &lt; file4.txt
 
<pre>dud@shadowplay:~ $ sort &lt; file4.txt
 
another_file.txt
 
another_file.txt
第195行: 第205行:
 
Yet-Another_file.txt
 
Yet-Another_file.txt
 
</pre>  
 
</pre>  
As you can see from this example, we used the file4.txt as input into the sort command.
+
正如你看到的,file4.txt作为sort命令的输入。
  
 
==== 管道 ====
 
==== 管道 ====
  
When you need the output from command 1 for the input into command 2, then you would use pipe character '|'. Here is the syntax for the pipe character: '''command | command''' Example:
+
如果你需要将一个命令的输出作为另一个命令的输入,那么你可能会用到管道符“|”。语法是:'''命令1 | 命令2'''。例如:
 
<pre>dud@shadowplay:~ $ ls | sort
 
<pre>dud@shadowplay:~ $ ls | sort
 
another_file.txt
 
another_file.txt
第209行: 第219行:
 
Yet-Another_file.txt
 
Yet-Another_file.txt
 
</pre>  
 
</pre>  
The above example is using the output from ls as input to the sort command. You will notice the list has been sorted. As you can see, the command line is an easy and powerful way of completing many tasks. If you want more information on using the command line, then look at the further reading section of this document.
+
在上面的例子中,ls的输出成为sort的输入。你可能注意到了,ls的输出背sort排序。用管道符将命令串成一长串是一种简单但强大的方法,可以借此完成许多任务。
  
 
== 进一步的介绍 ==
 
== 进一步的介绍 ==
  
http://rute.2038bug.com/index.html.gz <br> [[UbuntuHelp:BasicCommands|BasicCommands]] has a list of basic commands.<br> [[UbuntuHelp:AdvancedCommandlineHowto|AdvancedCommandlineHowto]] has some advanced command line features such as scripting.<br>
+
http://rute.2038bug.com/index.html.gz <br> [[UbuntuHelp:BasicCommands|BasicCommands]] 上面有基本命令的列表。<br> [[UbuntuHelp:AdvancedCommandlineHowto|AdvancedCommandlineHowto]] 上面介绍命令行的高级特性,如脚本。<br>
  
 
[[Category:UbuntuHelp]]
 
[[Category:UbuntuHelp]]

2008年6月9日 (一) 18:40的最新版本


介绍

Ubuntu 是一个对新手友好的,带有很棒的图形界面的发行版。尽管如此,有时候还是免不了需要大量重复的鼠标操作。其实只需要在键盘上敲几个键就能免除这些繁复。我认为这有百利而无一害:不论你做什么,Linux 都能够在命令行中发挥它真正的力量!:)

什么是命令行?

简单的说,命令行就是以一行一行的命令为基础的用户界面。你可以管它叫 字符型直接串行命令处理器。多数情况下,用户同计算机进行交互的方式是键入一行(或多行)命令。这些命令根据命令处理器的语法,触发了计算机执行各种动作。

为了保持简单易懂,我们一步一步慢慢来。/!\ 你要是等不及,不妨直接跳去看 命令语法 一节。

命令行的历史

在计算机出现的早期,命令行是唯一的人机交互方式。後来,Xerox's Palo Alto 研究中心(PARC) 提出了图形用户界面(GUI)的概念,以後的大多数 GUI 都是基于它的概念构建的。後来,苹果花了一大笔钱,才被允许“研究”他们自己的 GUI。苹果拥有了自己的 GUI 後,直到 1986 年,Unix 上终于出现了第一个 GUI,它被命名为 X。不过 Linux 不得不再等了 10 年,才发布 XFree86。它至今仍是当初 X server 的免费实现版本。

之前我介绍过,CLI(命令行界面)是图形界面出来之前唯一的人机交互途径。1969 年,贝尔电话实验室 发布了 UNIX Timeshare System 的 V1 版本。UNIX 有了一个 shell,称为 sh。很长一段时间内它都是同计算机进行交流的唯一渠道。到了後来,UNIX 出现了衍生版本:HP-UX,1BSD,Solaris,OpenVMS,IRIX,SCO XENIX,等等。再接着,Linux 出现了。Linux 的发展历程同本文关系不大,总之 sh 之外的 CLI 开始多了起来:zsh,ksh,bourne shell,等等。

可移植操作系统接口(POSIX)

维基百科是这样定义可移植操作系统接口(POSIX)的:“可移植操作系统接口是电气电子工程师学会(IEEE)定义的一组标准,旨在为Unix及其变体上运行的应用程序提供统一的接口(API)。正式的称呼是IEEE 1003,国际标准化名称是ISO/IEC 9945。本标准的构建大约始于1985年。最初的名字是IEEE-IX,后来,应电气电子工程师学会的要求,Richard Stallman将本标准命名为POSIX。POSIX的前四个字母代表可移植(Portal)操作系统(Operation System)界面(Interface),X代表Unix血统的API。”如今,POSIX已经是底层标准。

使用命令行的优势

So this all sounds very dull and boring, not to mention hard.
Well, it isn't, really. ;) It's quite easy once you understand the basics. 使用命令行的优势在于:

  • 节省时间;
  • 当系统崩溃或者由于配置问题,而没法使用图形界面时,命令行的作用就体现出来了;
  • 某些任务无法在图形界面下完成。

举个例子。有一天系统管理员告诉你,你占用了太多的空间。那么你就要找出自己哪些地方占用的空间最多。在图形界面下,你可以拿出你的秒表,预备——开始!

现在,切换到命令行,执行下面的语句:`du | sort -n`(我们等下再解释)。看到没?命令行下有些事情做起来就是快(虽然图形界面下更容易)。

学习命令行的先决条件

本文假设你安装了任意版本的 Ubuntu,并且有兴趣了解其内在机制。

如何执行命令

依据本文的目标,我们将在桌面上打开命令行界面。
Ubuntu
Applications | Accessories | Terminal
Kubuntu
KDE Menu | System | Konsole Terminal Program
Xubuntu 6.10
Applications | System | Terminal

基本格式和概念

你首先注意到的应该是下面的文字:

dud@shadowplay:~ $
或者
[dud@shadowplay ~]$

你所看到的这叫做提示符。它表明计算机已经准备好了,并且在等待用户的输入。在这个例子当中,dud 指的是我当前登录的用户名,shadowplay 是计算机的名字,~ 则表示当前的路径(用户的主目录)。基本概念如下:

  • 终端指的是 Linux 操作系统的“物理(直接)”界面。
  • 我们现在用的叫做终端仿真器,也就是在 GUI 界面中模拟一个 CLI 界面。当它关闭时,通过这个仿真器中运行起来的所有程序也都会终止。
  • shell 是终端下的一个命令翻译器。
  • 命令是 shell 将会为你执行的东西。
  • 命令执行返回的结果称为输出,执行结果通常输出到终端上。
  • 命令的输入指的是这个命令要用到的参数或数据。命令会根据输入来调整自己的执行方式。
  • 一个正在运行的程序成为进程。它可以被激活、休眠,或者变成其他状态。

命令语法

本节将解释 Ubuntu 缺省的 shell,bash shell 的基本用法。

单个命令

每个命令的语法都不尽相同。这里给出一些基本例子。执行命令的最简单的方式,就是将这个命令打出来。
例如:

dud@shadowplay:~ $ ls
file1.txt
file2.pdf
file3.mp3
file1.pdf
another_file.txt
Yet-Another_file.txt
file-with_other-NAME.TXT

上例用来显示当前目录中的内容。有的命令可能会需要一个或多个参数,格式是 命令 <参数> 。例如:

dud@shadowplay:~ $ cat file1.txt
Roses are red.
Violets are blue,
and you have the bird-flu!

上面的例子告诉我们,通过在cat命令後面加上文件名作为参数,就可以显示文件file1.txt的内容。几乎所有的命令,不管有没有参数,都有选项,格式是命令 -选项(译注:在DOS下,选项也被称为开关)。例如:

dud@shadowplay:~ $ ls -r
file-with_other-NAME.TXT
Yet-Another_file.txt
another_file.txt
file1.pdf
file3.mp3
file2.pdf
file1.txt

我们看到,加上-r选项之後,ls命令就以逆序列出当前目录的内容。

多个命令

有时,完成一项任务需要使用多个命令。现在我们就来讲解使用多个命令的语法。如果你想按顺序执行两个命令,可以使用分号语法:命令1;命令2。例如:

dud@shadowplay:~ $grep red file1.txt ; grep blue file1.txt
Roses are red,
Violets are blue,

上例说明命令1和命令2都被执行。如果你要求命令1成功执行後,才能再执行命令2,那么就应该使用“&&”语法:命令1 && 命令2。例如:

dud@shadowplay:~ $ grep red file1.txt && grep blue file1.txt
Roses are red,
Violets are blue,
dud@shadowplay:~ $ grep purple file1.txt && grep blue file1.txt
dud@shadowplay:~ $

通过上面的例子,我们注意到如果第一个命令未能成功执行,则不执行第二个命令。如果你希望只有在命令1执行失败的时候才执行命令2,那么可以使用“||”语法:命令1||命令2。例如:

dud@shadowplay:~ $ grep red file1.txt || grep blue file1.txt
Roses are red,
dud@shadowplay:~ $ grep purple file1.txt || grep blue file1.txt
Violets are blue,
dud@shadowplay:~ $

上面的例子告诉我们,只有在第一个命令失败的时候,第二个命令才执行。

通配符

通配符是一种强大的语法,它用在命令的参数中,可以代表单个或任意个特定或全部字符。第一个介绍的通配符是“*”,它代表任意个任意字符(杏林小草注:任意个包括了零个),语法为 命令 * 。例如:

dud@shadowplay:~ $ ls f*
file1.txt
file2.pdf
file3.mp3
file1.pdf
file-with_other-NAME.TXT

在上面的例子中,我们列出了当前目录下所有以字母“f”开头的文件。下一个介绍的通配符是“?”,它代表单个任意字符,语法为 命令 ?。例如:

dud@shadowplay:~ $ ls file?.pdf
file2.pdf

上例只有一个文件符合用户要求的样式。最後,语法 命令 [abc...] 允许你使用括号中指定的字符。例如:

dud@shadowplay:~ $ ls file[0-9].*
file1.txt
file2.pdf
file3.mp3
file1.pdf

上例使用了两种通配符,范围通配符([])和任意通配符(*)。从这个例子我们可以了解到通配符的威力多么的大!


杏林小草注:事实上,所有的shell都支持通配符;而且,展开通配符的不是命令,而是shell,这是linux命令行与DOS命令行的区别。其它常用的通配符还有:

[a-c]:从“a”开始,到“c”结束的所有字符。符号“-”除外。

[!abc]:所有的字符,除了“abc”之外。
[!a-c]:你可能猜到了,它的意思是除了从“a”开始,到“c”结束的字符外的所有字符。

{词1,词2,词3,...}:这个就复杂点了,举例来说吧,file_{one,two,three}代表file_one、file_two、file_three。明白了吧?

控制流

命令的输入来自键盘(称标准输入或stdin),然后输出到屏幕(称标准输出或stdout)。还有一个特殊的标准错误输出,专门用于显示错误信息。系统自动为每一个程序创建这三个输入输出设备。用户可以重新定向这三种信息流。

重定向

如果你希望命令执行的结果输出到文件而非终端,可以使用这个语法: 命令 > 文件名。例如:

dud@shadowplay:~ $ ls > file4.txt
dud@shadowplay:~ $ cat file4.txt
file1.txt
file2.pdf
file3.mp3
file1.pdf
another_file.txt
Yet-Another_file.txt
file-with_other-NAME.TXT
file4.txt

在上面的例子中,如果file4.txt不存在,那么系统会自动创建这个文件。注意:如果file4.txt已经存在,那么上述操作会覆盖原来的文件。如果你希望命令执行的结果追加到原有文件的后面,可以使用这个语法: 命令 >> 文件名。例如:

dud@shadowplay:~ $ ls >> file4.txt
dud@shadowplay:~ $ cat file4.txt
file1.txt
file2.pdf
file3.mp3
file1.pdf
another_file.txt
Yet-Another_file.txt
file-with_other-NAME.TXT
file4.txt
file1.txt
file2.pdf
file3.mp3
file1.pdf
another_file.txt
Yet-Another_file.txt
file-with_other-NAME.TXT
file4.txt

在上例中,新的信息追加到了旧文件的尾部。现在,我们来看看另一个方向的重定向----我们用文件作为命令的输入。语法是这样的: 命令 < 文件名。例如:

dud@shadowplay:~ $ sort < file4.txt
another_file.txt
another_file.txt
file1.txt
file1.txt
file2.pdf
file2.pdf
file3.mp3
file3.mp3
file4.txt
file4.txt
file-with_other-NAME.TXT
file-with_other-NAME.TXT
Yet-Another_file.txt
Yet-Another_file.txt

正如你看到的,file4.txt作为sort命令的输入。

管道

如果你需要将一个命令的输出作为另一个命令的输入,那么你可能会用到管道符“|”。语法是:命令1 | 命令2。例如:

dud@shadowplay:~ $ ls | sort
another_file.txt
file1.pdf
file1.txt
file2.pdf
file3.mp3
file-with_other-NAME.TXT
Yet-Another_file.txt

在上面的例子中,ls的输出成为sort的输入。你可能注意到了,ls的输出背sort排序。用管道符将命令串成一长串是一种简单但强大的方法,可以借此完成许多任务。

进一步的介绍

http://rute.2038bug.com/index.html.gz
BasicCommands 上面有基本命令的列表。
AdvancedCommandlineHowto 上面介绍命令行的高级特性,如脚本。