English • 中文
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，等等。
The Wikipedia defines POSIX as the following: "POSIX is the collective name of a family of related standards specified by the IEEE to define the application program interface (API) for software designed to run on variants of the Unix OS. They are formally designated as IEEE 1003 and the international standard name is ISO/IEC 9945. The standards emerged from a project, begun circa 1985. The term POSIX was suggested by Richard Stallman in response to an IEEE request for a memorable name; before that the standards effort was called IEEE-IX. POSIX is a near acronym for Portable Operating System Interface, with the X signifying the Unix heritage of the API." This sounds fancy and all, but to stay rather concise, POSIX is the underlying standard and functionality of how your CLI responds.
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`（我们等下再解释）。看到没？命令行下有些事情做起来就是快（虽然图形界面下更容易）。
Applications | Accessories | Terminal
KDE Menu | System | Konsole Terminal Program
Applications | System | Terminal
The first thing that you should notice is something like:
[email protected]:~ $ or [[email protected] ~]$
What you see here is called the prompt. It signifies that the computer is ready and awaiting user input. In my case, dud is the user that I'm logged in as. shadowplay is the computer's hostname, and ~ is the current directory (the user's home directory). Concepts:
- A terminal is a "physical"(direct) interface to your Linux Operating System.
- A terminal emulator is what we'll be using here. This is a CLI wrapped within your running GUI. Any applications running in a terminal emulator will be killed if you close the terminal emulator.
- A shell is an interpreter for your terminal.
- A command is usually a small utility that the shell will execute for you.
- Output is what a command returns; most often this is returned on the terminal.
- Input is the arguments or data that any given command will take. Input will change the way a given command acts.
- A process is a running application on your computer. It can be active, sleeping, or in a number of other states.
This section will try to give you a good rundown of the basic usage for the bash shell, which is the default user shell in Ubuntu.
The command syntax will vary with each command. Here are some of the basics. The simplest way to use some commands is to type just the command.
[email protected]:~ $ ls file1.txt file2.pdf file3.mp3 file1.pdf another_file.txt Yet-Another_file.txt file-with_other-NAME.TXT
The above example displays the contents of the current working directory, while other commands may require one or more arguments. command <argument> Example:
[email protected]:~ $ cat file1.txt Roses are red. Violets are blue, and you have the bird-flu!
The above example shows you the content of the file1.txt file by adding the filename as the argument for the cat command. Almost all commands, whether they have arguments or not, have options. command -option Example:
[email protected]:~ $ ls -r file-with_other-NAME.TXT Yet-Another_file.txt another_file.txt file1.pdf file3.mp3 file2.pdf file1.txt
Using the previous example of showing the current directory, we have added the -r option. As you can see, the listing of the current working directory has been displayed in the reverse order.
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 ; command2 Example:
[email protected]:~ $grep red file1.txt ; grep blue file1.txt Roses are red, Violets are blue,
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 && command2 Example:
[email protected]:~ $ grep red file1.txt && grep blue file1.txt Roses are red, Violets are blue, [email protected]:~ $ grep purple file1.txt && grep blue file1.txt [email protected]:~ $
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:
[email protected]:~ $ grep red file1.txt || grep blue file1.txt Roses are red, [email protected]:~ $ grep purple file1.txt || grep blue file1.txt Violets are blue, [email protected]:~ $
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:
[email protected]:~ $ ls f* file1.txt file2.pdf file3.mp3 file1.pdf file-with_other-NAME.TXT
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 ? Example:
[email protected]:~ $ ls file?.pdf file2.pdf
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:
[email protected]:~ $ ls file[0-9].* file1.txt file2.pdf file3.mp3 file1.pdf
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.
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.
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 > filename Example:
[email protected]:~ $ ls > file4.txt [email protected]:~ $ 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
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 >> filename Example:
[email protected]:~ $ ls >> file4.txt [email protected]:~ $ 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
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 < filename Example:
[email protected]:~ $ 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
As you can see from this example, we used the file4.txt as input into the sort command.
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:
[email protected]:~ $ ls | sort another_file.txt file1.pdf file1.txt file2.pdf file3.mp3 file-with_other-NAME.TXT Yet-Another_file.txt
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.