个人工具

UbuntuHelp:UsingTheTerminal/zh

来自Ubuntu中文

BENZORG讨论 | 贡献2008年4月17日 (四) 22:01的版本 搜索man文档

跳转至: 导航, 搜索


为什么使用终端?

"Linux下面有多种GUIs(图形用户界面),可以在其下点击鼠标,拖拽,能够很好的工作却不需要一次读很多文档.而传统的Unix环境是 CLI(命令行界面),即在命令行下键入命令,执行想要的操作。相比之下,这种方式执行起来更快,功能也更强,不足之处是用户需要了解相关操作的命令。" --摘自 man intro(1) 在做某些任务,特别是系统配置等任务时使用终端是很有意义的,或许在帮助页或 论坛中你已经看到过类似这样的指令:

sudo gobbledegook blah_blah -w -t -f aWkward/ComBinationOf/mixedCase/underscores_strokes/and.dots

It's normally assumed that you know how to use the terminal - and anyone can manage typing and backspacing. But there are some crafty shortcuts which can make your life a lot easier:

  • How to move around in a terminal window and edit the text that you type there.
  • Some Linux commands for basic tasks.
  • Different ways to open a terminal, how to work with multiple terminals, etc.

使用本页

  • 本文将指导您熟练掌握基本的 GNU/Linux shell 命令。
  • 本文并不是命令行的完整教程,仅仅是对 Ubuntu图形界面工具的补充。
  • 所有命令都用黑体标明。
  • 需要键入的命令用“引用黑体”标明。
  • 本文的所有命令都要在终端下执行。
  • 注意:Linux是大小写敏感的。User,user,USER在Linux是不同的。

启动终端

在 Gnome (Ubuntu) 中

终端位于 应用程序 -> 附件 -> 终端

在 Xfce (Xubuntu) 中

终端位于 应用程序 -> 系统 -> 终端

在 KDE (Kubuntu) 中

终端位于 K菜单 -> 系统 -> 终端程序 (Konsole)

命令行

sudo:需要提升权限执行的命令

  • 如果您所在的目录或想操作的文件不在您的帐户所在的目录,下面的大多数命令都需要使用 sudo命令。这是一个特殊的命令,它给你临时的权限来修改系统设置。终端会询问你的密码。sudo 使用详情请参阅RootSudo

文件 & 目录类命令

  • pwdpwd 命令查看您当前所处的路径(pwd 代表 "打印当前工作目录")。例如:在桌面文件夹中执行 "pwd" 命令将输出 "~/Desktop"。注意,Gnome 的终端在窗口标题中也会显示这一信息-具体请看本页顶部的截屏图片。
  • lsls 命令列出当前目录下的文件(和目录)。使用特定的参数,您可以查看文件的大小,创建时间,权限等。例如:"ls ~"会列出您的home目录中的文件(和目录)。
  • cdcd命令用来改变当前工作目录。当您打开一个终端的时候,您就位于您的home目录中。如果想要切换到其它的目录,就要用 cd命令。例如:
  • 进入root目录,输入 "cd /"
  • 进入到您自己的目录,输入 "cd"
  • 进入当前目录的上一次目录,输入 "cd .."(译者注:cd 与 .. 之间有空格)
  • 进入前一个操作的目录,输入 "cd -"
  • 一次进入多层目录,输入 "cd /var/www",将会直接切换到/var的子目录/www中。另一个例子, "cd ~/Desktop"将会进入到您的桌面目录
  • cpcp命令用来复制文件。例如:"cp file foo"命令将会创建一个"file"的精确的副本,并命名为"foo",而 "file"不会有任何变化。If you are copying a directory, you must use "cp -r directory foo" (copy recursively).
  • mvmv命令将文件移动到另一个位置或者给文件更名。看下面的例子:"mv file foo"命令会将文件"file"更名为"foo"。"mv foo ~/Desktop"会将文件"foo"移动到桌面目录,但不会更名。如果想更名,你必须要指定一个新的名字。
  • 为了输入方便,您可以用 '~' 符号来代替您的home目录。
  • Note that if you are using mv with sudo you can use the ~ shortcut, because the terminal expands the ~ to your home directory. However, when you open a root shell with sudo -i or sudo -s, ~ will refer to the root account's home directory, not your own.
  • rm:这个命令用来移动或删除文件。对于非空的目录,用这个命令不能删除。
  • rmdir:命令rmdir用来删除“空”目录。要删除目录和其中的所有内容,使用rm -r代替。
  • mkdirmkdir 命令用来创建目录。例如:"mkdir music"将会创建一个 music 目录。
  • manman 命令用来显示其它命令的手册页。执行 "man man"可以查看到 man自己的信息。通过 "Man & Getting Help"能够分页显示更多的信息。

系统信息类命令

  • dfdf命令用来查看各个文件系统当前的空间使用状况。"df -h"可能是最有用的选项了-它以M和G为单位输出,而不是以块为单位。(-h 的含义是“便于阅读”)
  • du:The du command displays the disk usage for a directory. It can either display the space used for all subdirectories or the total for the directory you run it on. Example:
user@users-desktop:~$ du /media/floppy
1032 /media/floppy/files
1036 /media/floppy/
user@users-desktop:~$ du -sh /media/floppy
1.1M /media/floppy/
  • -s means "Summary" and -h means "Human Readable"
  • freefree命令用来查看系统中使用和剩馀的内存情况。"free -m" 将结果以M为单位输出,这对现在的计算机来说非常有用。
  • toptop 命令用来查看linux系统的信息,运行着的进程和系统资源,包括 CPU, RAM & swap使用情况和运行着的任务的总的数量。退出 top ,按"q"
  • uname -auname命令的 -a 参数用来查看系统的所有信息,包括 机器名,内核名称 & 版本 和一些其它的细节。它最大的用处是用来查看当前所用内核的信息。
  • lsb_release -alsb_release命令的-a参数查看当前运行的linux的版本信息,例如:
user@computer:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 6.06 LTS
Release: 6.06
Codename: dapper
  • ifconfig显示当前系统的网络接口信息。

添加新用户

  • "adduser newuser"命令用来创建一个用户名为"newuser"的新用户,为新用户 newuser 创建一个密码,使用如下命令"passwd newuser"

选项

命令的默认操作常常会被指定一个确定的 --参数所修改。例如ls命令有一个-s参数,因此 "ls -s"就会额外的显示出文件的大小。它也有一个 -h参数,将文件的大小以很好的可读性的格式输出。参数可以以簇聚合,比如 "ls -sh""ls -s -h"的效果相同。大多数的参数都很长,两个破折号前缀代表一个参数,所以"ls --size --human-readable"也和上面得命令相同。

'Man' 和 获得帮助

man command, info command and command --help是命令行下面最重要的工具。

在linux下面,几乎每一个命令和每一个应用程序都会有一个man(manual)文件,所以只要简单的键入"man "command""就能看到这个命令的手册页。例如,"man mv"会打开mv (Move) 的手册页。

利用键盘上的方向键移动手册页面,用"q"退出。

"man man"会查看man命令的手册页,这里是一个很好的开始!

"man intro"也非常有用 -它能够查看 "用户命令介绍",写的非常好!是一份很简介的linux命令的介绍。

还有一个就是info命令了,它通常比man还深入。输入"info info"命令可得到info页的介绍。

Some software developers prefer info to man (for instance, Debian and GNU developers), so if you find a very widely used command or app that doesn't have a man page, it's worth checking for an info page.

几乎所有的命令都能接受一个-h(或 --help)选项,能够输出命令的简要的描述和参数,然后自动退回命令提示符。可以输入"man -h""man --help"查看。

警告:It's possible (but rare) that a program doesn't understand the -h option to mean help. For this reason, check for a man or info page first, and try the long option --help before -h.

搜索man文档

如果您不确定用哪个命令或程序,您可以试试搜索man文件。

  • man -k foo会搜索关于foo的man文件。试试看"man -k nautilus"是怎样的。
  • 注意:这同apropos 命令是一样的。
  • man -f foo仅仅搜所系统man文件的标题。试试"man -f gnome"
  • 这个同 whatis'命令是相同的。'

=== Other Useful Things ===其他有用的东西

==== Prettier Manual Pages ==== 更加漂亮的使用手册页面

Users who have Konqueror installed will be pleased to find they can read and search man pages in a web browser context, prettified with their chosen desktop fonts and a little colour, by visiting man:/command in Konqueror's address bar. Some people might find this lightens the load if there's lots of documentation to read/search.

安装了Konquerer的用户会高兴的发现他们可以在Konqueror的地址栏里面输入“man:/command",达到类似在一个web浏览器中像浏览网页一样的阅读或搜索“man使用手册“的相关信息,通过选择字体与颜色(系统配置中的字体与颜色)来达到一定程度的美化,这会让部分需要阅读大量“man使用手册“内容的用户感到一些轻松。

Pasting in commands

Often, you will be referred to instructions that require commands to be pasted into the terminal. You might be wondering why the text you've copied from a web page using ctrl+C won't paste in with ctrl+V. Surely you don't have to type in all those nasty commands and filenames? Relax. Middle Button Click on your mouse (both buttons simultaneously on a two-button mouse) or Right Click and select Paste from the menu.

Save on typing

Up Arrow or ctrl+p Scrolls through the commands you've entered previously.
Down Arrow or ctrl+n Takes you back to a more recent command.
Enter When you have the command you want.
tab A very useful feature. It autocompletes any commands or filenames, if there's only one option, or else gives you a list of options.
ctrl+r Searches for commands you've already typed. When you have entered a very long, complex command and need to repeat it, using this key combination and then typing a portion of the command will search through your command history. When you find it, simply press Enter.

Change the text

The mouse won't work. Use the Left/Right arrow keys to move around the line. When the cursor is where you want it in the line, typing inserts text - ie it doesn't overtype what's already there.

ctrl+a or Home Moves the cursor to the start of a line.
ctrl+e or End Moves the cursor to the end of a line.
ctrl+b Moves to the beginning of the previous or current word.
ctrl+k Deletes from the current cursor position to the end of the line.
ctrl+u Deletes the whole of the current line.
ctrl+w Deletes the word before the cursor.

More ways to run a terminal

You can also get it with a function key You can run more than one - in tabs or separate windows

更多信息

关于Linux命令行的更详细指南,请参阅:


Head back to UserDocumentation