命令行指南

来自Ubuntu中文
Oneleaf留言 | 贡献2007年5月23日 (三) 10:05的版本
跳到导航跳到搜索

命令行指南

原文出处:

原文作者:Dag Rune Sneeggen

授权许可:

翻译人员:雕啸长空 purewind

校正人员:purewind

贡献人员:

适用版本:

文章状态:UbuntuHelp:等待翻译




Note that this is still a work in progress. If you're just looking for a basic introduction to the Linux command line, please see BasicCommands.

'注意:本文仍在修改之中,如果你只是想找一篇关于linux操作指令的文章,请参阅'BasicCommands.


Introduction(引言)

Even though Ubuntu is the newbie friendly and polished graphical distribution, there are still situations when a serious amount of time and mouse-clickage can be spared by typing a bit. I don't think this is a bad thing at all, no matter what you do Linux has one of its real strengths in the Command Line! :)

  • 可以说Ubuntu是当前图形界面最为友好和易操作的linux发行版,但还是有很多时候,只需执行几条简单的指令就可以完成繁琐的鼠标点击所完成的操作,从而节约大量的时间和精力,我向来提倡通过命令操作方式来使用linux,因为linux的命令操作模式功能实在太强大了!它可以实现你需要的所有操作!

What is it?(什么是linux的命令操作模式?)

A Command Line is in all simplicity a user interface which is based on lines of commands. You can say that it is a textual direct serial processor.

简单的说,命令行就是基于成行的命令的用户界面。您也可称其为一个文本化指令序列处理器。

  • Most commonly, the user interacts directly with the computer by typing one line (although it can be more than one), to which triggers actions from the computer based on the syntax of the current processor.
    Before everything gets too complicated, we can quite simply move on. /!\ The impatient can move right on to the Basic Usage section.
  • 绝大多数情况下, 用户通过输入一行命令(尽管可以不止一行)直接与计算机互动, 所触发的行为基于当前处理器的语法。
    在一切变得复杂之前,我们悄悄地简单扼要的继续。 /!\ 不耐烦地人可以直接看 基本用法 章节。
  • 命令操作模式是一个很简洁的操作界面,它通过输入一条一条的指命(有些情况下可能是成组的命令)来实现对计算机的操作,通常它也被称为“文本操作模式”

.

History(发展史)

In the early days of computers there was only the Command Line. The concept of a Graphical User Interface (GUI) to which most GUI are modelled after today were invented by engineers at Xerox's Palo Alto Research Center (PARC). A bit later, Apple paid a whole bunch of money to be allowed to "study" their GUI idea. And, after a while Apple had their own GUI.

早期的计算机操作系统都只有命令行操作模式,现在非常流行的“图形用户界面(GUI)”的概念最早是由施乐公司“帕尔托研究中心 (PARC)”的一名工程师所提出来的,在它提出来不久之后,苹果电脑公司出了一大笔钱才被许可“研究”他们的GUI概念,不久以后,苹果就有了他们自己的GUI。

  • Not until 1986 did UNIX get its first GUI, developed by the MIT Project. They named it X. Linux however, had to wait ten more years before XFree86 were released. XFree86 was, and is even today, a free adoptation of the original X server.
  • 在UNIX系统上直到1986年才出现第一个图形操作界面,是由MIT Project开发的,当时给这个图形界面命名为X,不过,在XFree86发布前,linux还要再多等上十年。XFree86今天依然是原始X 服务器的一个自由改编版。
  • As mentioned earlier, the CLI (Command Line Interface) were the only way to communicate with computers before the GUI was invented. In 1969, Bell Telephone Laboratories released V1 of the UNIX Timeshare System. UNIX had a shell called sh, which was the only means of communicating with the computer, and it would stay that way for quite some time.
  • 如上所述,在GUI发明以前,CLI(命令行用户界面)是用户操控计算机的唯一途径,1969年,“贝尔电话实验室”发布了UNIX分时系统的V1版。UNIX中有个外壳程序叫做sh ,是与计算机交流的唯一方法,这种情况持续了很久。

Later on there came dervatives of UNIX. HP-UX, 1BSD, Solaris, OpenVMS, IRIX, SCO XENIX, etc etc... As more time progressed, GNU/Linux emerged. However, the history of Linux itself is way off the scope for this HOWTO. As time progressed alternative CLI to sh emerged. zsh, ksh, bourne shell, etc etc...

  • 稍候涌现了很多的UNIX衍生系统(purewind:个人以为原文中的dervative应为derivative,可能原作者打错字了,或者其他原因,汗)。比如HP-UX,1BSD,Solaris,OpenVMS,IRIX,SCO,ENIX,等等...,随着时间的推移,GNU/Linux出现了。不过Linux的发展历史并不是本文所要阐述的。时间继续推移,用以替代sh的CLI也出现了,比如 zsh ,ksh ,bourne shell等等。
POSIX(UNIX可移植操作系统接口)

The wikipedia defines POSIX as the following;

维基百科是这样定义“UNIX可移植操作系统接口"的:

  • "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."

"UNIX可移植操作系统接口"是对所有参照IEEE组织制定的“Unix软件接口标准”而开发的软件的统称,依这个标准而开发的软件可以运行在所有基于Unix的操作系统上,IEEE组织给这一标准的命名为IEEE 1003,它的国际标准名称为ISO/IEC 9945。这个标准(UNIX可移植操作系统接口)来自于大约1985年的一个开发项目。后来应IEEE组织的要求,该项目成员Richard Stallman提议了一个好记的名字叫“POSIX",在那以前,它被称为IEEE-IX 。POSIX实际上就是可移植(Portable)操作(Operating)系统(System)接口(Interface)的首字母简写,最后加上X表明它的Unix血统。

  • This sounds fancy and all, but to stay rather concise, it will suffice to say that POSIX is the underlying standards and functionality of how your CLI responds and interacts. 这听起来确实古怪,为了保持简洁,可以说POSIX是您的CLI响应和交互的根本标准和功能。

Why you should care (为什么使用命令操作模式?)

So, this all sounds very dull and boring. Not to mention hard work and thinking?
Well, it isn't really. ;-) It's quite easy once you understand the basics.

这些听上去都非常枯燥乏味。更别提繁重的工作和思考了!事实上并非如此,只要你掌握了它的基本技巧,它就会变得相当简单。

You should care because the CLI can;

使用CLI可以给你带来下面这些好处(非全部的好处)

  • save you time.
  • 为您节省时间
  • help you out of a rough-spot.
  • 救您脱离苦海
  • enable you to use Linux in ways that using a GUI exclusively can not.
  • 您可以通过GUI所没有的方式来使用Linux
  • help you figure out why something doesn't work as expected, and you will most likely be able to fix the issue.
  • 帮助您找出为何目标没有实现,增加您解决问题的几率

For example you have been called by the systems administrator that you have used too much space. You want to quickly work out where the most space is used, using a graphical interface start you timer - go. Now go to a command line and type du | sort -n we will describe more later. It is faster to do some things on the command line and sometimes easier for graphical. Sometimes you need a hammer, sometimes a screwdriver.

举个例子:某天,公司的计算机管理员提醒您,你已经使用太多的磁盘空间了。您可能很急切地想知道到底是什么东西占了那么多的硬盘空间,对比一下通过图形界面查询和执行一条du | sort -n(我们以后再来解释),你会发现有些事情用命令行来做比较快,而用图形界面可能比较简单。有时候你需要锤子,有时候你需要起子。

Prerequisites(本文的假设前提)

This assumes that you are running any version of Ubuntu Linux, with default GNOME. You can however run Fluxbox, KDE, XFCE or basically anything else that you like, but this assumes GNOME for simplicity's sake.

本文假设您正在使用默认的图形界面为GNOME的任意版本Ubuntu Linux,当然您也可能正在使用Fluxbox,KDE,XFCE或是其他的图形操作界面,我们简单点就假设是GNOME 了。

How to invoke it(如何进入命令模式)

Click on your GNOME Menu. Then proceed to Accessories -> Terminal.
This will launch what is called gnome-terminal. Which is the terminal emulator that ships with GNOME.

点击GNOME菜单->附件->终端,就可以启动名为gnome-termina的程序,它就是GNOME随机的终端模拟器。

Basic structure and concepts(命令模式的基本结构和概念)

The first thing that you should notice is something like:

打开命令终端后你首先可能会注意到类似下面的界面:

dud@shadowplay:~ $
or
[dud@shadowplay ~]$

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 computers hostname, and ~ is the current directory. Each line of command is parsed by the computer after you press the Enter key. You can try now writing:

你所看到的这些被称为命令终端提示符,它表示计算机已就绪,正在等待着用户输入操作指令。以我的屏幕画面为例,dud是我所登录的用户,shadowplay是这台计算机的主机名,~表示当前目录。此时输入任何指令按回车之后该指令将会提交到计算机运行,比如你可以输入命令:ls 再按下回车

ls [ENTER]

{i} Do not type [ENTER], just hit the keyboard key
This will give you a list of the files and directories within your current location.

{i} [ENTER]是指输入完ls后按下回车键,而不是叫你输入这个单词,ls这个命令将会列出你当前所在目录里的所有文件和子目录列表。

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.
  • 终端模拟器是我们这里要用的,是你运行的GUI中封装的CLI序。当您关闭一个终端模拟器时,通过这个终端打开的所有程序将会关闭。
  • A shell is an interpeter for your terminal. You can drive a BMW or a Honda, but you'll still be driving in the same direction on the same road. It's slightly different ways to interact, simply speaking.
  • 外壳程序(shell)是您的终端的解释程序(purewind:原文的interpeter应为interpreter)。您可以开宝马也可以开本田,但是走得还是是一个方向一条路。简单的说就是交互的方式有点小差异。
  • A command is usually a small application which the shell will execute for you. Usually a command creates output, and often it takes arguments (input).
  • 命令一般是外壳程序为您执行的小程序。它通常会有一些结果提示信息输出。命令经常会接受一些参数(输入)来执行。
  • 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.
  • 输入是命令执行时接受的参数或者数据。输入会改变给出命令的行为。
  • Arguments are usually given either plainly, or by --switch="argument". Commonly they also have a short form eg, -s.
  • 参数通常会直接给出,或者通过 --switch="参数" 来给出。一般还会有简写形式,比如 -s 。
  • A process is a command/application that is currently running on your computer. It doesn't actually have to be active, it can sleep or be in a number of states.
  • 进程是当前在您的计算机中运行的命令或者程序。它并不总是活动的,它可以睡眠或者是处于其他几种状态。

Basic Usage(基本用法)

This section will try to give you a good rundown of the basic usage for the bash shell, which is the default shell in ubuntu.

本节将向您介绍bash shell程序的精要,它是ubuntu缺省的外壳程序。

Commands(常用指令)

I will now list a few of the basic and most useful commands that comes with Ubuntu Linux. /!\ Remember to type the commands exactly as they are written, and end with pressing the Enter key!

接下来我将举例阐述一下常用的基本指令: /!\ 记住要正确的按照下面写的输入,后面还得带上回车键!

Listing files:

查询文件列表

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

ls will default to listing your current directory, and sorting by alphabetical order. It will also be quite sparse about the information it returns. Common arguments for ls:

ls命令默认状态下将按首字母升序列出你当前文件夹下面的所有内容,但这样直接运行所得到的信息也是比较少的,通常它可以结合以下这些参数运行以查询更多的信息:

  • ls / will return the file listing for directory /. If ls is given one argument, it will take that as the directory it should work on. It will not use your current working directory in other words.
  • ls -l will give you a more detailed listing.
  • ls -a will list all files, even hidden(files starting with .) files.
  • ls -h will give file sizes in KB/MB/GB, instead of mere Bytes.

Listi ng current directory:

查询当前所在目录:

dud@shadowplay:~ $ pwd
/home/dud

Changing directory:

进入其他目录(用命令cd)

dud@shadowplay:~ $ pwd
/home/dud
dud@shadowplay:~ $ cd /root/
dud@shadowplay:~ $ pwd
/root

You can see how pwd first lists /home/dud as current directory, then cd changes it to /root/. Which pwd confirms.

上面例子中,当前目录原来是/home/dud,执行cd /root/之后再运行pwd可以发现,当前目录已经改为/root了

Echoing:

在屏幕上输出字符:

dud@shadowplay:~ $ echo "Hello World"
Hello World

A very useful command, which will quite simply echo back what you give it as arguments. /!\ It's important to note that although this works without the quotes, it's a very good practise to start using quotes as you'll be needing that often later on!

这是一个很有用的命令,它可以在屏幕上输入你指定的参数(""号中的内容),当然这里举的这个例子中它没有多大的实际意义,但随着你对LINUX指令的不断深入,就会发现它的价值所在。

List content of a file:

显示文件内容:

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

The command cat will print out content of text files to your terminal. The file file1.txt had a poem as we saw. ;-)

cat这个命令可以用来在终端上显示txt文本文件的内容。如上例输出的这首“诗”;

Copying a file:

复制文件:

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

Moving a file:

移动文件:

dud@shadowplay:~ $ ls
file1.txt
file2.txt
dud@shadowplay:~ $ mv file1.txt new_file.txt
dud@shadowplay:~ $ ls
file2.txt
new_file.txt

You might have noticed by now that the CLI is rather mute in terms of giving feedback. Most common commands can be given a -v argument to be a bit more verbose.

需要注意的是,在命令操作时系统基本上不会给你什么提示,当然,绝大多数的命令可以通过加上一个参数 -v来要求系统给出执行命令的反馈信息;

dud@shadowplay:~ $ mv -v file1.txt new_file.txt
`file1.txt' -> `new_file.txt'

This will tell you that it moved file1.txt to new_file.txt.

加上-v参数后,系统就会输出操作提示

`file1.txt' -> `new_file.txt'

Creating an empty file:

建立一个空文本文件:

dud@shadowplay:~ $ ls
file1.txt
dud@shadowplay:~ $ touch tempfile.txt
dud@shadowplay:~ $ ls
file1.txt
tempfile.txt

Creating a directory:

建立一个目录

dud@shadowplay:~ $ ls
file1.txt
tempfile.txt
dud@shadowplay:~ $ mkdir test_dir
dud@shadowplay:~ $ ls
file1.txt
tempfile.txt
test_dir

Removing a file/directory

删除文件/目录

dud@shadowplay:~ $ ls -p
file1.txt
tempfile.txt
test_dir/
dud@shadowplay:~ $ rm -i tempfile.txt
rm: remove regular empty file `test.txt'? y
dud@shadowplay:~ $ ls -p
file1.txt
test_dir/
dud@shadowplay:~ $ rm test_dir
rm: cannot remove `test_dir': Is a directory
dud@shadowplay:~ $ rm -R test_dir
dud@shadowplay:~ $ ls -p
file1.txt

So what happened here? First we listed the files, which has 2 files and a directory.

我们来分析一下上面的操作:首先我们通过ls命令查询可知当前目下有两个文件和一个文件夹;

  • You can use ls -p, which will indicate if an item is a file/directory/symlink/etc.
  • 你可以用参数-p来让系统显示某一项的类型,比如是文件/文件夹/快捷链接等等;
  • Next we used rm -i to delete the file. the -i switch will ask for confirmation before doing things; interactivity.
  • 接下来我们用rm -i尝试删除文件,-i参数是让系统在执行删除操作前输出一条确认提示;i也就是交互性的意思;
  • We then tried to delete the directory with rm test_dir, which failed because you have to use rm -R to delete directories.
  • 当我们尝试用上面的命令去删除一个文件夹时会得到错误的提示,因为删除文件夹必须使用-R参数

/!\ EXTREMELY IMPORTANT: The CLI will automatically assume that you have full and utter control of what you are doing! If you tell it to rm -Rf / it will delete your entire harddrive without even telling you that it is doing so! By always using rm -i, you assure that you at least will have to confirm before doing stupid things. If you want it to always ask before running any deletion, you can run a simple command:

特别提示:在使用命令操作时,系统假设你很明确自己在做什么,它不会给你太多的提示,比如你执行rm -Rf /,它将会删除你硬盘上所有的东西,并且不会给你任何提示,所以,尽量在使用命令时加上-i的参数,以让系统在执行前进行一次确认,防止你干一些蠢事。如果你觉得每次都要输入-i太麻烦,你可以执行以下的命令,让-i成为默认参数:

alias rm='rm -i'

Listing processes:

查询当前进程:

dud@shadowplay:~ $ ps
PID TTY          TIME CMD
11278 pts/1    00:00:00 bash
24448 pts/1    00:00:00 ps

This shows the processes running, that is "owned" by you.

这条命令会例出你所启动的所有进程;

  • ps -a will show all processes that is running, for all owners.
  • ps -a可以例出系统当前运行的所有进程,包括由其他用户启动的进程;
  • ps auxww is incredibly handy! It will show all processes except for some specific system processes, but it will also list it in a more readable format; with extra details.
  • ps auxww是一条相当人性化的命令,它会例出除一些很特殊进程以外的所有进程,并会以一个高可读的形式显示结果,每一个进程都会有较为详细的解释;

Ok, that should do as an introduction! :D You can have a look at for instance:

基本命令的介绍就到此为止,你可以参阅以下地址得到更加详细的命令指南:

Control flow(控制流程)

Input/Output(输入/输出)

Commands read input from the keyboard (standard input or stdin) and write to output (standard out or stdout). There is also a special category for error messages called standard error (or stderr). These are automatically created by your shell when it runs you command.

input用来读取你通过键盘(或其他标准输入设备)输入的信息,output用于在屏幕(或其他标准输出设备)上输出你指定的输出内容.另外还有一些标准的出错提示也是通过这个命令来实现的。通常在遇到操作错误时,系统会自动调用这个命令来输出标准错误提示;

We can redirect input and output to and from a command

我们能重定向命令中产生的输入和输出流的位置

Redirection

重定向If you wanted the output of a command to go to a file instead of the terminal, then you would use the following syntax:

如果你想把命令产生的输出流指向一个文件而不是(默认的)终端,你可以使用如下的语句:

Example:

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

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:

以上例子将创建文件file4.txt如果file4.txt不存在的话。注意:如果file4.txt已经存在,那么上面的命令将覆盖文件的内容。如果你想将内容添加到已存在的文件内容的最后,那你可以用下面这个语句:

  • command >> filename

Example:

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

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:

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

As you can see from this example, we used the file4.txt as input into the sort command.

如你所见,我们将file4.txt(的内容)作为命令的参数输入。

管道(Pipes)

The true power of Linux comes from the ability to combine simple programs into more complex functions by simply joining them together. This is done by the pipe character '|' represented by a broken bar on the keyboard. For example let's sort our grep command above:

grep -i command < myfile | sort > result.text

Search for command in myfile, push this output into sort and then write the sorted file to result.text.

搜索 myfile 中的命令,将输出分类并写入分类文件到 result.text 。

后台进程(Background Processes)

The CLI is not a serial interface to the system. You can give the system commands while it is executing others. To start a process in the background, append a '&' to the command:

CLI 不是系统的串行接口。您可以在执行其他命令时给出系统命令。要启动一个进程到后台,追加一个“&”到命令后面。

sleep 60 &
ls

The sleep command runs in the background, and you can still interact with the computer. The best way to think of '&' is like ';' except that it starts commands asynchronously.

睡眠命令在后台运行,您依然可以与计算机交互。除了不同步启动命令以外,最好把 '&' 理解成 ';'。

If you have a command that is taking a long time, and you want to put it into the background, it is easy. Simply type ctrl-z while the command is running, and it will stop. Then type bg to put it into the background. The command fg will put it back into the foreground.

如果您有一个命令将占用很多时间,您想把它放入后台运行,也很简单。只要在命令运行时按下ctrl-z,它就会停止。然后键入 bg 使其转入后台。fg 命令可使其转回前台。

sleep 60
<ctrl-z>
bg
fg

Finally, you can kill a foreground process with ctrl-c

最后,您可以使用 ctrl-c 来杀死一个前台进程。

Environment(环境)

Special variables. PATH, PS1, ...

特殊变量。PATH, PS1, ...

Manipulating Variables(操作变量)

Simple scripting(简单脚本)

Further reading(深入读本)

http://rute.2038bug.com/index.html.gz