个人工具

“UbuntuHelp:UsingTheTerminal”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
第1行: 第1行:
{{From|https://help.ubuntu.com/community/UsingTheTerminal}}
+
{{From|https://help.ubuntu.com/community/UsingTheTerminal}} {{Languages|UbuntuHelp:UsingTheTerminal}}
{{Languages|UbuntuHelp:UsingTheTerminal}}
+
 
#title Using The Terminal
+
=== 为什么? ===
=== Why? ===
+
 
''"Under Linux there are GUIs (graphical user interfaces), where you can point and click and drag, and hopefully get  work  done  without  first reading  lots  of  documentation. The traditional Unix environment is a CLI (command line interface), where you type commands to tell the  computer  what to do. That is faster and more powerful, but requires finding out what the commands are." -- from '''man intro(1)''' ''  
+
''"Linux下面有多种GUIs(图形用户界面),可以在其下点击鼠标,拖拽,能够很好的工作却不需要一次读很多文档.而传统的Unix环境是 CLI(命令行界面),即在命令行下键入命令,执行想要的操作。相比之下,这种方式执行起来更快,功能也更强,不足之处是用户需要了解相关操作的命令。" --摘自 '''man intro(1)'''''
For some tasks, especially things like system configuration it makes sense to use the terminal, and you'll probably have seen instructions on help pages or [http://www.ubuntuforums.org forums] similar to:
+
在做某些任务,特别是系统配置等任务时使用终端是很有意义的,或许在帮助页或 [http://www.ubuntuforums.org 论坛]中你已经看到过类似这样的指令:
<pre><nowiki>
+
<pre>sudo gobbledegook blah_blah -w -t -f aWkward/ComBinationOf/mixedCase/underscores_strokes/and.dots
sudo gobbledegook blah_blah -w -t -f aWkward/ComBinationOf/mixedCase/underscores_strokes/and.dots
+
</pre>  
</nowiki></pre>
+
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:
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.  
+
* How to move around in a terminal window and edit the text that you type there.
 
* Some Linux commands for basic tasks.
 
* Some Linux commands for basic tasks.
 
* Different ways to open a terminal, how to work with multiple terminals, etc.
 
* Different ways to open a terminal, how to work with multiple terminals, etc.
 +
 
=== Using this page ===
 
=== Using this page ===
 +
 
* This page will make you familiar with basic GNU/Linux shell commands.
 
* This page will make you familiar with basic GNU/Linux shell commands.
 
* It is not intended to be a complete guide to the command line, just an introduction to complement Ubuntu's graphical tools.
 
* It is not intended to be a complete guide to the command line, just an introduction to complement Ubuntu's graphical tools.
第19行: 第21行:
 
* All of the commands on this page are to be issued from a command prompt in a terminal.
 
* All of the commands on this page are to be issued from a command prompt in a terminal.
 
* '''Note that the terminal is case sensitive.''' User, user, and USER are all different to Linux.
 
* '''Note that the terminal is case sensitive.''' User, user, and USER are all different to Linux.
 +
 
=== Starting a Terminal ===
 
=== Starting a Terminal ===
 +
 
==== In Gnome (Ubuntu) ====
 
==== In Gnome (Ubuntu) ====
The terminal can be found at '''Applications menu''' -> '''Accessories''' -> '''Terminal'''.
+
 
 +
The terminal can be found at '''Applications menu''' -&gt; '''Accessories''' -&gt; '''Terminal'''.
 +
 
 
==== In Xfce (Xubuntu) ====
 
==== In Xfce (Xubuntu) ====
The terminal can be found at '''Applications menu''' -> '''System''' -> '''Terminal'''.
+
 
 +
The terminal can be found at '''Applications menu''' -&gt; '''System''' -&gt; '''Terminal'''.
 +
 
 
==== In KDE (Kubuntu) ====
 
==== In KDE (Kubuntu) ====
The terminal can be found at '''KMenu''' -> '''System''' -> '''Terminal Program (Konsole)'''.
+
 
 +
The terminal can be found at '''KMenu''' -&gt; '''System''' -&gt; '''Terminal Program (Konsole)'''.
 +
 
 
=== Commands ===
 
=== Commands ===
 +
 
==== sudo: Executing Commands with Elevated Privileges ====
 
==== sudo: Executing Commands with Elevated Privileges ====
* Most of the following commands will need to be prefaced with the '''sudo''' command if you will be working with directories or files not owned by your account. This is a special command which temporarily gives you access to change computer settings. The terminal will ask you for your password. Please see [[UbuntuHelp:RootSudo|RootSudo]] for information on using sudo.
+
 
==== File & Directory Commands ====
+
* Most of the following commands will need to be prefaced with the '''sudo''' command if you will be working with directories or files not owned by your account. This is a special command which temporarily gives you access to change computer settings. The terminal will ask you for your password. Please see [[UbuntuHelp:RootSudo|RootSudo]] for information on using sudo.
* '''pwd''': The '''pwd''' command will allow you to know in which directory you're located ('''pwd''' stands for "print working directory"). Example: '''"pwd"''' in the Desktop directory will show "~/Desktop". Note that the Gnome Terminal also displays this information in the title bar of its window - see the example screenshot at the top of this page.
+
 
* '''ls''': The '''ls''' command will show you the files in your current directory. Used with certain options, you can see sizes of files, when files where made, and permissions of files. Example: '''"ls ~"''' will show you the files that are in your home directory.
+
==== File &amp; Directory Commands ====
* '''cd''': The '''cd''' command will allow you to change directories. When you open a terminal you will be in your home directory. To move around the file system you will use '''cd'''. Examples:
+
 
 +
* '''pwd''': The '''pwd''' command will allow you to know in which directory you're located ('''pwd''' stands for "print working directory"). Example: '''"pwd"''' in the Desktop directory will show "~/Desktop". Note that the Gnome Terminal also displays this information in the title bar of its window - see the example screenshot at the top of this page.
 +
* '''ls''': The '''ls''' command will show you the files in your current directory. Used with certain options, you can see sizes of files, when files where made, and permissions of files. Example: '''"ls ~"''' will show you the files that are in your home directory.
 +
* '''cd''': The '''cd''' command will allow you to change directories. When you open a terminal you will be in your home directory. To move around the file system you will use '''cd'''. Examples:
 
* To navigate into the root directory, use '''"cd /"'''
 
* To navigate into the root directory, use '''"cd /"'''
 
* To navigate to your home directory, use '''"cd"''' or '''"cd ~"'''
 
* To navigate to your home directory, use '''"cd"''' or '''"cd ~"'''
第38行: 第52行:
 
* To navigate to the previous directory (or back), use '''"cd -"'''
 
* To navigate to the previous directory (or back), use '''"cd -"'''
 
* To navigate through multiple levels of directory at once, specify the full directory path that you want to go to. For example, use, '''"cd /var/www"''' to go directly to the /www subdirectory of /var/. As another example, '''"cd ~/Desktop"''' will move you to the Desktop subdirectory inside your home directory.
 
* To navigate through multiple levels of directory at once, specify the full directory path that you want to go to. For example, use, '''"cd /var/www"''' to go directly to the /www subdirectory of /var/. As another example, '''"cd ~/Desktop"''' will move you to the Desktop subdirectory inside your home directory.
* '''cp''': The '''cp''' command will make a copy of a file for you. Example: '''"cp file foo"''' will make a exact copy of "file" and name it "foo", but the file "file" will still be there. If you are copying a directory, you must use '''"cp -r directory foo"''' (copy recursively).
+
* '''cp''': The '''cp''' command will make a copy of a file for you. Example: '''"cp file foo"''' will make a exact copy of "file" and name it "foo", but the file "file" will still be there. If you are copying a directory, you must use '''"cp -r directory foo"''' (copy recursively).
* '''mv''': The '''mv''' command will move a file to a different location or will rename a file.   Examples are as follows: '''"mv file foo"''' will rename the file "file" to "foo". '''"mv foo ~/Desktop"''' will move the file "foo" to your Desktop directory but will not rename it. You must specify a new file name to rename a file.
+
* '''mv''': The '''mv''' command will move a file to a different location or will rename a file. Examples are as follows: '''"mv file foo"''' will rename the file "file" to "foo". '''"mv foo ~/Desktop"''' will move the file "foo" to your Desktop directory but will not rename it. You must specify a new file name to rename a file.
 
* To save on typing, you can substitute '~' in place of the home directory.
 
* To save on typing, you can substitute '~' in place of the home directory.
 
* 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.
 
* 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''': Use this command to remove or delete a file in your directory.
 
* '''rm''': Use this command to remove or delete a file in your directory.
* '''rmdir''': The '''rmdir''' command will delete an ''empty'' directory. To delete a directory and all of its contents recursively, use '''rm -r''' instead.
+
* '''rmdir''': The '''rmdir''' command will delete an ''empty'' directory. To delete a directory and all of its contents recursively, use '''rm -r''' instead.
* '''mkdir''': The '''mkdir''' command will allow you to create directories. Example: '''"mkdir music"''' will create a directory called "music".
+
* '''mkdir''': The '''mkdir''' command will allow you to create directories. Example: '''"mkdir music"''' will create a directory called "music".
* '''man''': The '''man''' command is used to show you the manual of other commands. Try '''"man man"''' to get the man page for '''man''' itself. See the "'''Man''' & Getting Help" section down the page for more information.
+
* '''man''': The '''man''' command is used to show you the manual of other commands. Try '''"man man"''' to get the man page for '''man''' itself. See the "'''Man''' &amp; Getting Help" section down the page for more information.
 +
 
 
==== System Information Commands ====
 
==== System Information Commands ====
 +
 
* '''df''': The '''df''' command displays filesystem disk space usage for all mounted partitions. "'''df -h'''" is probably the most useful - it uses megabytes (M) and gigabytes (G) instead of blocks to report. ('''-h''' means "human-readable")
 
* '''df''': The '''df''' command displays filesystem disk space usage for all mounted partitions. "'''df -h'''" is probably the most useful - it uses megabytes (M) and gigabytes (G) instead of blocks to report. ('''-h''' means "human-readable")
 
* '''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:
 
* '''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:
<pre><nowiki>
+
<pre>user@users-desktop:~$ du /media/floppy
user@users-desktop:~$ du /media/floppy
+
1032 /media/floppy/files
1032   /media/floppy/files
+
1036 /media/floppy/
1036   /media/floppy/
+
 
user@users-desktop:~$ du -sh /media/floppy
 
user@users-desktop:~$ du -sh /media/floppy
1.1M   /media/floppy/
+
1.1M /media/floppy/
</nowiki></pre>
+
</pre>  
 
* '''-s''' means "Summary" and '''-h''' means "Human Readable"
 
* '''-s''' means "Summary" and '''-h''' means "Human Readable"
 
* '''free''': The '''free''' command displays the amount of free and used memory in the system. '''"free -m"''' will give the information using megabytes, which is probably most useful for current computers.
 
* '''free''': The '''free''' command displays the amount of free and used memory in the system. '''"free -m"''' will give the information using megabytes, which is probably most useful for current computers.
* '''top''': The '''top''' command displays information on your Linux system, running processes and system resources, including CPU, RAM & swap usage and total number of tasks being run. To exit '''top''', press '''"q"'''.
+
* '''top''': The '''top''' command displays information on your Linux system, running processes and system resources, including CPU, RAM &amp; swap usage and total number of tasks being run. To exit '''top''', press '''"q"'''.
* '''uname -a''': The '''uname''' command with the '''-a''' option prints all system information, including machine name, kernel name & version, and a few other details. Most useful for checking which kernel you're using.
+
* '''uname -a''': The '''uname''' command with the '''-a''' option prints all system information, including machine name, kernel name &amp; version, and a few other details. Most useful for checking which kernel you're using.
 
* '''lsb_release -a''': The '''lsb_release''' command with the '''-a''' option prints version information for the Linux release you're running, for example:
 
* '''lsb_release -a''': The '''lsb_release''' command with the '''-a''' option prints version information for the Linux release you're running, for example:
<pre><nowiki>
+
<pre>user@computer:~$ lsb_release -a
user@computer:~$ lsb_release -a
+
 
No LSB modules are available.
 
No LSB modules are available.
 
Distributor ID: Ubuntu
 
Distributor ID: Ubuntu
Description:   Ubuntu 6.06 LTS
+
Description: Ubuntu 6.06 LTS
Release:       6.06
+
Release: 6.06
Codename:       dapper
+
Codename: dapper
</nowiki></pre>
+
</pre>  
 
* '''ifconfig''' reports on your system's network interfaces.
 
* '''ifconfig''' reports on your system's network interfaces.
 +
 
==== Adding A New User ====
 
==== Adding A New User ====
* '''"adduser newuser"''' command will create a new general user called "newuser" on your system, and to assign a password for the newuser account use '''"passwd newuser"'''.
+
 
 +
* '''"adduser newuser"''' command will create a new general user called "newuser" on your system, and to assign a password for the newuser account use '''"passwd newuser"'''.
 +
 
 
=== Options ===
 
=== Options ===
The default behaviour for a command may usually be modified by adding a '''--''option''''' to the command. The '''ls''' command for example has an '''-s''' option so that '''"ls -s"''' will include file sizes in the listing. There is also a '''-h''' option to get those sizes in a "human readable" format.
+
 
Options can be grouped in clusters so '''"ls -sh"''' is exactly the same command as '''"ls -s -h"'''. Most options have a long version, prefixed with two dashes instead of one, so even '''"ls --size --human-readable"''' is the same command.
+
The default behaviour for a command may usually be modified by adding a '''--''option''''' to the command. The '''ls''' command for example has an '''-s''' option so that '''"ls -s"''' will include file sizes in the listing. There is also a '''-h''' option to get those sizes in a "human readable" format. Options can be grouped in clusters so '''"ls -sh"''' is exactly the same command as '''"ls -s -h"'''. Most options have a long version, prefixed with two dashes instead of one, so even '''"ls --size --human-readable"''' is the same command.
 +
 
 
=== "Man" and getting help ===
 
=== "Man" and getting help ===
/!\ '''man ''command''''', '''info ''command''''' and '''''command'' --help''' are the most important tools at the command line.
+
 
Nearly every command and application in Linux will have a man (manual) file, so finding them is as simple as typing '''"man "command""''' to bring up a longer manual entry for the specified command. For example, '''"man mv"''' will bring up the '''mv''' (Move) manual.
+
/!\ '''man ''command''''', '''info ''command''''' and '''''command'' --help''' are the most important tools at the command line. Nearly every command and application in Linux will have a man (manual) file, so finding them is as simple as typing '''"man "command""''' to bring up a longer manual entry for the specified command. For example, '''"man mv"''' will bring up the '''mv''' (Move) manual. Move up and down the man file with the arrow keys, and quit back to the command prompt with '''"q"'''. '''"man man"''' will bring up the manual entry for the '''man''' command, which is a good place to start! '''"man intro"''' is especially useful - it displays the "Introduction to user commands" which is a well-written, fairly brief introduction to the Linux command line. There are also '''info''' pages, which are generally more in-depth than '''man''' pages. Try '''"info info"''' for the introduction to info pages. 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. Virtually all commands understand the '''-h''' (or '''--help''') option which will produce a short usage description of the command and it's options, then exit back to the command prompt. Try '''"man -h"''' or '''"man --help"''' to see this in action. ''Caveat: 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.''
Move up and down the man file with the arrow keys, and quit back to the command prompt with '''"q"'''.
+
 
'''"man man"''' will bring up the manual entry for the '''man''' command, which is a good place to start!
+
'''"man intro"''' is especially useful - it displays the "Introduction to user commands" which is a well-written, fairly brief introduction to the Linux command line.
+
There are also '''info''' pages, which are generally more in-depth than '''man''' pages. Try '''"info info"''' for the introduction to info pages.  
+
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.
+
Virtually all commands understand the '''-h''' (or '''--help''') option which will produce a short usage description of the command and it's options, then exit back to the command prompt. Try '''"man -h"''' or '''"man --help"''' to see this in action.
+
''Caveat: 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.''
+
 
==== Searching for man files ====
 
==== Searching for man files ====
 +
 
If you aren't sure which command or application you need to use, you can try searching the man files.
 
If you aren't sure which command or application you need to use, you can try searching the man files.
 +
 
* '''man -k ''foo''''' will search the man files for ''foo''. Try '''"man -k nautilus"''' to see how this works.
 
* '''man -k ''foo''''' will search the man files for ''foo''. Try '''"man -k nautilus"''' to see how this works.
 
* Note that this is the same as doing '''apropos ''command'''''.
 
* Note that this is the same as doing '''apropos ''command'''''.
 
* '''man -f ''foo''''' searches only the titles of your system's man files. Try '''"man -f gnome"''', for example.
 
* '''man -f ''foo''''' searches only the titles of your system's man files. Try '''"man -f gnome"''', for example.
 
* Note that this is the same as doing '''whatis ''command'''''.
 
* Note that this is the same as doing '''whatis ''command'''''.
 +
 
=== Other Useful Things ===
 
=== Other Useful Things ===
 +
 
==== Prettier Manual Pages ====
 
==== 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.
 
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.
 +
 
==== Pasting in commands ====
 
==== 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.
+
 
 +
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 ====
 
==== Save on typing ====
{|border="1" cellspacing="0"
+
 
|'''Up Arrow''' or '''ctrl+p''' |||| Scrolls through the commands you've entered previously.
+
{| cellspacing="0" border="1"
 
|-
 
|-
| '''Down Arrow''' or '''ctrl+n''' |||| Takes you back to a more recent command.
+
| '''Up Arrow''' or '''ctrl+p'''
 +
|  
 +
| Scrolls through the commands you've entered previously.
 
|-
 
|-
| '''Enter''' |||| When you have the command you want.
+
| '''Down Arrow''' or '''ctrl+n'''
 +
|  
 +
| Takes you back to a more recent command.
 
|-
 
|-
| '''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.
+
| '''Enter'''
 +
|  
 +
| When you have the command you want.
 
|-
 
|-
| '''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'''.
+
| '''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 ====
 
==== 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.
+
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.
{|border="1" cellspacing="0"
+
 
| '''ctrl+a''' or '''Home''' |||| Moves the cursor to the ''start'' of a line.
+
{| cellspacing="0" border="1"
 +
|-
 +
| '''ctrl+a''' or '''Home'''
 +
|  
 +
| Moves the cursor to the ''start'' of a line.
 
|-
 
|-
| '''ctrl+e''' or '''End''' |||| Moves the cursor to the '''''e'''nd'' of a line.
+
| '''ctrl+e''' or '''End'''
 +
|  
 +
| Moves the cursor to the '''''e'''nd'' of a line.
 
|-
 
|-
| '''ctrl+b''' |||| Moves to the '''b'''eginning of the previous or current word.
+
| '''ctrl+b'''
 +
|  
 +
| Moves to the '''b'''eginning of the previous or current word.
 
|-
 
|-
| '''ctrl+k''' |||| Deletes from the current cursor position to the end of the line.
+
| '''ctrl+k'''
 +
|  
 +
| Deletes from the current cursor position to the end of the line.
 
|-
 
|-
| '''ctrl+u''' |||| Deletes the whole of the current line.
+
| '''ctrl+u'''
 +
|  
 +
| Deletes the whole of the current line.
 
|-
 
|-
| '''ctrl+w''' |||| Deletes the word before the cursor.
+
| '''ctrl+w'''
 +
|  
 +
| Deletes the word before the cursor.
 
|}
 
|}
 +
 
=== More ways to run a terminal ===
 
=== 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
+
You can also get it with a function key You can run more than one - in tabs or separate windows
 +
 
 
=== More Information ===
 
=== More Information ===
 +
 
* [[UbuntuHelp:AptGetHowto|AptGetHowto]] - using apt-get to install packages from the command line.
 
* [[UbuntuHelp:AptGetHowto|AptGetHowto]] - using apt-get to install packages from the command line.
* [[UbuntuHelp:Repositories/CommandLine| Commandline Repository Editing]] - adding the Universe/Multiverse repositories through the command line.
+
* [[UbuntuHelp:Repositories/CommandLine|Commandline Repository Editing]] - adding the Universe/Multiverse repositories through the command line.
* [[UbuntuHelp:grep| grep Howto]] - grep is a powerful command line search tool.
+
* [[UbuntuHelp:Grep|grep Howto]] - grep is a powerful command line search tool.
* [[UbuntuHelp:find| find Howto]] - locate files on the command line.
+
* [[UbuntuHelp:Find|find Howto]] - locate files on the command line.
 
* [[UbuntuHelp:CommandlineHowto|CommandlineHowto]] - longer and more complete than this basic guide, but still unfinished.
 
* [[UbuntuHelp:CommandlineHowto|CommandlineHowto]] - longer and more complete than this basic guide, but still unfinished.
 
* [[UbuntuHelp:HowToReadline|HowToReadline]] - information on some more advanced customization for the command line.
 
* [[UbuntuHelp:HowToReadline|HowToReadline]] - information on some more advanced customization for the command line.
 +
 
For more detailed tutorials on the Linux command line, please see:
 
For more detailed tutorials on the Linux command line, please see:
* http://linuxcommand.org/ - basic BASH tutorials, including BASH scripting
+
 
* http://linuxsurvival.com/index.php - Java-based tutorials
+
* http://linuxcommand.org/ - basic BASH tutorials, including BASH scripting
 +
* http://linuxsurvival.com/index.php - Java-based tutorials
 
* http://rute.2038bug.com/index.html.gz - a massive online book about system administration, almost all from the command line.
 
* http://rute.2038bug.com/index.html.gz - a massive online book about system administration, almost all from the command line.
 +
 
----
 
----
 +
 
Head back to [[UbuntuHelp:UserDocumentation|UserDocumentation]]
 
Head back to [[UbuntuHelp:UserDocumentation|UserDocumentation]]
  
[[category:UbuntuHelp]]
+
[[Category:UbuntuHelp]]

2008年4月14日 (一) 13:17的版本


为什么?

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

Using this page

  • This page will make you familiar with basic GNU/Linux shell commands.
  • It is not intended to be a complete guide to the command line, just an introduction to complement Ubuntu's graphical tools.
  • All command names will be in bold.
  • Commands needing to be typed will be in "bold with quotes".
  • All of the commands on this page are to be issued from a command prompt in a terminal.
  • Note that the terminal is case sensitive. User, user, and USER are all different to Linux.

Starting a Terminal

In Gnome (Ubuntu)

The terminal can be found at Applications menu -> Accessories -> Terminal.

In Xfce (Xubuntu)

The terminal can be found at Applications menu -> System -> Terminal.

In KDE (Kubuntu)

The terminal can be found at KMenu -> System -> Terminal Program (Konsole).

Commands

sudo: Executing Commands with Elevated Privileges

  • Most of the following commands will need to be prefaced with the sudo command if you will be working with directories or files not owned by your account. This is a special command which temporarily gives you access to change computer settings. The terminal will ask you for your password. Please see RootSudo for information on using sudo.

File & Directory Commands

  • pwd: The pwd command will allow you to know in which directory you're located (pwd stands for "print working directory"). Example: "pwd" in the Desktop directory will show "~/Desktop". Note that the Gnome Terminal also displays this information in the title bar of its window - see the example screenshot at the top of this page.
  • ls: The ls command will show you the files in your current directory. Used with certain options, you can see sizes of files, when files where made, and permissions of files. Example: "ls ~" will show you the files that are in your home directory.
  • cd: The cd command will allow you to change directories. When you open a terminal you will be in your home directory. To move around the file system you will use cd. Examples:
  • To navigate into the root directory, use "cd /"
  • To navigate to your home directory, use "cd" or "cd ~"
  • To navigate up one directory level, use "cd .."
  • To navigate to the previous directory (or back), use "cd -"
  • To navigate through multiple levels of directory at once, specify the full directory path that you want to go to. For example, use, "cd /var/www" to go directly to the /www subdirectory of /var/. As another example, "cd ~/Desktop" will move you to the Desktop subdirectory inside your home directory.
  • cp: The cp command will make a copy of a file for you. Example: "cp file foo" will make a exact copy of "file" and name it "foo", but the file "file" will still be there. If you are copying a directory, you must use "cp -r directory foo" (copy recursively).
  • mv: The mv command will move a file to a different location or will rename a file. Examples are as follows: "mv file foo" will rename the file "file" to "foo". "mv foo ~/Desktop" will move the file "foo" to your Desktop directory but will not rename it. You must specify a new file name to rename a file.
  • To save on typing, you can substitute '~' in place of the home directory.
  • 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: Use this command to remove or delete a file in your directory.
  • rmdir: The rmdir command will delete an empty directory. To delete a directory and all of its contents recursively, use rm -r instead.
  • mkdir: The mkdir command will allow you to create directories. Example: "mkdir music" will create a directory called "music".
  • man: The man command is used to show you the manual of other commands. Try "man man" to get the man page for man itself. See the "Man & Getting Help" section down the page for more information.

System Information Commands

  • df: The df command displays filesystem disk space usage for all mounted partitions. "df -h" is probably the most useful - it uses megabytes (M) and gigabytes (G) instead of blocks to report. (-h means "human-readable")
  • 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"
  • free: The free command displays the amount of free and used memory in the system. "free -m" will give the information using megabytes, which is probably most useful for current computers.
  • top: The top command displays information on your Linux system, running processes and system resources, including CPU, RAM & swap usage and total number of tasks being run. To exit top, press "q".
  • uname -a: The uname command with the -a option prints all system information, including machine name, kernel name & version, and a few other details. Most useful for checking which kernel you're using.
  • lsb_release -a: The lsb_release command with the -a option prints version information for the Linux release you're running, for example:
user@computer:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 6.06 LTS
Release: 6.06
Codename: dapper
  • ifconfig reports on your system's network interfaces.

Adding A New User

  • "adduser newuser" command will create a new general user called "newuser" on your system, and to assign a password for the newuser account use "passwd newuser".

Options

The default behaviour for a command may usually be modified by adding a --option to the command. The ls command for example has an -s option so that "ls -s" will include file sizes in the listing. There is also a -h option to get those sizes in a "human readable" format. Options can be grouped in clusters so "ls -sh" is exactly the same command as "ls -s -h". Most options have a long version, prefixed with two dashes instead of one, so even "ls --size --human-readable" is the same command.

"Man" and getting help

/!\ man command, info command and command --help are the most important tools at the command line. Nearly every command and application in Linux will have a man (manual) file, so finding them is as simple as typing "man "command"" to bring up a longer manual entry for the specified command. For example, "man mv" will bring up the mv (Move) manual. Move up and down the man file with the arrow keys, and quit back to the command prompt with "q". "man man" will bring up the manual entry for the man command, which is a good place to start! "man intro" is especially useful - it displays the "Introduction to user commands" which is a well-written, fairly brief introduction to the Linux command line. There are also info pages, which are generally more in-depth than man pages. Try "info info" for the introduction to info pages. 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. Virtually all commands understand the -h (or --help) option which will produce a short usage description of the command and it's options, then exit back to the command prompt. Try "man -h" or "man --help" to see this in action. Caveat: 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.

Searching for man files

If you aren't sure which command or application you need to use, you can try searching the man files.

  • man -k foo will search the man files for foo. Try "man -k nautilus" to see how this works.
  • Note that this is the same as doing apropos command.
  • man -f foo searches only the titles of your system's man files. Try "man -f gnome", for example.
  • Note that this is the same as doing whatis command.

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.

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

More Information

  • AptGetHowto - using apt-get to install packages from the command line.
  • Commandline Repository Editing - adding the Universe/Multiverse repositories through the command line.
  • grep Howto - grep is a powerful command line search tool.
  • find Howto - locate files on the command line.
  • CommandlineHowto - longer and more complete than this basic guide, but still unfinished.
  • HowToReadline - information on some more advanced customization for the command line.

For more detailed tutorials on the Linux command line, please see:


Head back to UserDocumentation