📄 ch07.htm
字号:
<A NAME="Heading7<FONT COLOR="#000077"><B>NOTE:</B> </FONT>As with basic Linux
commands, case is important! For instance, <TT>ls</TT> has an <TT>R</TT> option (recursive:
show files in subdirectories, too) that gives much different results from the <TT>r</TT>
option.
<HR>
</DL>
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading8<FONT COLOR="#000077"><B>TIP: </B></FONT>You can think of
<TT>a</TT> as the "all files" option, <TT>l</TT> as the "long list"
option, <TT>t</TT> as the "sort by time" option, <TT>r</TT> as the "reverse
sort" option, and so on. In fact, most options in Linux are mnemonic--the option
letter stands for a word or phrase. Some option letters mean the same thing in many
different Linux commands. For instance, <TT>v</TT> often means verbose--in other
words, "Give me lots of detail." However, do not assume that, on any unfamiliar
command, certain options will work in the "usual" way! For instance, <TT>r</TT>
is the recursive option for many Linux commands; however, in the case of <TT>ls</TT>,
reverse sort is more commonly used, and therefore it gets the easier-to-type lowercase
r, while recursive is left with the capital R. It might seem like not much extra
effort to press the Shift key to get the capital letter, but try typing a string
of four or five options, one of which is capitalized!
<HR>
</DL>
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading9<FONT COLOR="#000077"><B>NOTE:</B> </FONT>You can easily find
out which options are available for any Linux command by using the <TT>man</TT> command.
See the section "The Linux Man Pages" later in this chapter.
<HR>
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading10<FONT COLOR="#000077">Other Parameters</FONT></H4>
<P>Linux commands often use parameters that are not actual command options. These
parameters, such as filenames or directories, are not preceded by a dash.</P>
<P>For instance, by default <TT>ls</TT> lists the files in your current directory.
You can, however, tell <TT>ls</TT> to list the files in any other directory simply
by adding the directory to the command line. For instance, <TT>ls /bin</TT> will
list everything in the <TT>/bin</TT> directory. This can be combined with command
options, so that <TT>ls -l /bin</TT> gives you detailed listings of the files in
<TT>/bin</TT>. Try this. You will be impressed by the number of files in the <TT>/bin</TT>
directory!</P>
<P>You can also specify <TT>ls</TT> to list information about any particular file
by entering its filename. For instance, <TT>ls -la .lessrc</TT> gives detailed information
only about the <TT>.lessrc</TT> file.
<H4 ALIGN="CENTER"><A NAME="Heading11<FONT COLOR="#000077">Input and Output
Redirection</FONT></H4>
<P>Many Linux commands let you specify which file or directory they are to act upon,
as we saw with the example <TT>ls -l /bin </TT>earlier.</P>
<P>You can also "pipe" the output from a command so that it becomes another
command's input. This is done by typing two or more commands separated by the <TT>|</TT>
character. (This character normally is found on the same key as the <TT>\</TT> character.
You must hold down the Shift key or you will get <TT>\</TT> instead of <TT>|</TT>.)
The <TT>|</TT> character means "Use the output from the previous command as
the input for the next command." Therefore, typing <TT>command_1|command_2</TT>
does both commands, one after the other, before giving you the results.</P>
<P>Using our assembly-line metaphor, we are processing items through two black boxes
instead of just one. When we use piping, it's like hooking up the first command's
output conveyor belt to become the input conveyor belt for the second command.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading12<FONT COLOR="#000077"><B>TIP: </B></FONT>Although Linux doesn't
care whether <TT>|</TT> is set off by spaces, if <TT>command_1 | command_2</TT> is
easier for you to read and understand than <TT>command_1|command_2</TT>, by all means
use spaces around <TT>|</TT>.
<HR>
</DL>
<P>You will have noticed that the output of <TT>ls -l /bin</TT> is many lines long,
so that much of the information scrolls off the screen before you can read it. You
can pipe this output to a formatting program called <TT>more</TT>, which displays
information in screen-sized chunks. When you enter <TT>ls -l /bin | more</TT>, you
will see the following:<FONT COLOR="#0066FF"></FONT>
<PRE><FONT COLOR="#0066FF">darkstar:~$ ls -l /bin | more
total 1611
-rwxr-xr-x 1 root bin 1248 Sep 17 04:25 arch*
-rwxr-xr-x 1 root bin 295940 Sep 5 01:45 bash*
-rwxr-xr-x 1 root bin 4840 Nov 24 1993 cat*
-rwxr-xr-x 1 root bin 9220 Jul 20 12:06 chgrp*
-rwxr-xr-x 1 root bin 13316 Jul 20 12:06 chmod*
-rwxr-xr-x 1 root bin 13316 Jul 20 12:06 chown*
lrwxrwxrwx 1 root root 17 Dec 7 13:37 compress -> /usr/bin/comp
ress*
-rwxr-xr-x 1 root bin 21508 Jul 20 12:06 cp*
-rwxr-xr-x 1 root bin 41988 May 1 1994 cpio*
lrwxrwxrwx 1 root root 4 Dec 7 13:40 csh -> tcsh*
-rwxr-xr-x 1 root bin 5192 Nov 24 1993 cut*
-rwxr-xr-x 1 root bin 19872 Mar 23 1994 date*
-rwxr-xr-x 1 root bin 17412 Jul 20 12:06 dd*
-rwxr-xr-x 1 root bin 13316 Jul 20 12:06 df*
-rwxr-xr-x 1 root bin 66564 Jun 9 1994 dialog*
-rwxr-xr-x 1 root bin 1752 Sep 17 04:25 dmesg*
lrwxrwxrwx 1 root root 8 Dec 7 13:37 dnsdomainname -> hostname*
-rwxr-xr-x 1 root bin 13316 Jul 20 12:06 du*
-rwxr-xr-x 1 root bin 3312 Mar 23 1994 echo*
-rwxr-xr-x 1 root bin 36684 May 4 1994 ed*
-rwxr-xr-x 1 root bin 326 Mar 23 1994 false*
--More--
</FONT></PRE>
<P>The <TT>--More--</TT> at the bottom of the screen tells you that there's more
text to come. To go to the next screen of text, press the spacebar. Every time you
press the spacebar, <TT>more</TT> displays another screenful of text. When the last
screenful of text has been displayed, <TT>more</TT> returns you to the Linux prompt.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading13<FONT COLOR="#000077"><B>TIP:</B> </FONT>The <TT>more</TT>
command can do many other things. For instance, to move back one screen at a time,
type <TT>b</TT> for "back." Another useful command is <TT>q</TT> for "quit."
This lets you leave immediately, without having to go through all the remaining screens
of text. While in <TT>more</TT>, type <TT>h</TT> for "help." This will
list the commands available within <TT>more</TT>.
<HR>
</DL>
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading14<FONT COLOR="#000077"><B>NOTE: </B></FONT>The Linux system
sometimes uses the command <TT>less</TT> instead of <TT>more</TT>. One difference
you will notice is that, unlike <TT>more</TT>, <TT>less</TT> requires you to type
<TT>q</TT> to return to the command line, even if you're at the end of the text to
be displayed. This might seem cumbersome, but it prevents you from accidentally exiting
the program by pressing the spacebar once too often. The name <TT>less</TT> is a
play on <TT>more</TT>. Originally, <TT>less</TT> was designed to have many features
that <TT>more</TT> lacked. The version of <TT>more</TT> included in your Linux system
has most of these features, however. The Linux <TT>man</TT> program, discussed later,
uses <TT>less</TT> to display text. Most other UNIX systems use <TT>more</TT> by
default. Don't get confused. Remember to type <TT>q</TT> to exit from <TT>less</TT>!
<HR>
</DL>
<P>Another thing you can do in Linux is to send output to a file instead of the screen.
There are many different reasons why you might want to do this. You might want to
save a "snapshot" of a command's output as it was at a certain time, or
you might want to save a command's output for further examination. You might also
want to save the output from a command that takes a very long time to run, and so
on.</P>
<P>To send output to a file, use the <TT>></TT> symbol (found above the period
on your keyboard). For instance, you can place the output of the <TT>ls -l /bin</TT>
command into a file called <TT>test</TT> by typing <TT>ls -l /bin > test</TT>.
Again, spaces around <TT>></TT> are optional and not strictly necessary, but they
do make the command much more readable.</P>
<P>If you now do an <TT>ls</TT> or <TT>ls -l</TT>, you will see that you've created
a new file called <TT>test</TT> in your own directory.</P>
<P>To see the contents of a file, you can again use the <TT>more</TT> command. Just
specify the name of the file you want to look at. In this case, you would type <TT>more
test</TT>.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading15<FONT COLOR="#000077"><B>WARNING:</B> </FONT>Be careful!
When you use <TT>></TT>, you completely overwrite the previous contents of the
file you specify to take the output (if that file existed). For example, if we already
had a file called <TT>test</TT> in our directory, its old contents would be completely
replaced by the output from <TT>ls -l /bin</TT>. Linux will not warn you that you
are about to do this! Be particularly careful if you're not in your usual directory,
or if you're logged in as <TT>root</TT>. You could, for instance, accidentally clobber
the Linux program <TT>test</TT>, which exists as a file named <TT>test</TT>--fortunately,
not in the directory where we created our <TT>test</TT> file! It's a good idea to
check if the output file already exists before using <TT>></TT>. In our example,
we could have typed <TT>ls -l test</TT> beforehand. If no information is displayed,
the file does not exist.
<HR>
</DL>
<P>You can specify that you want to add your output to the end of the file, rather
than replace the file's contents, by using <TT>>></TT>. Type <TT>who >>
test</TT> to add the output of the <TT>who</TT> command to the end of the text already
in the file <TT>test</TT>.</P>
<P>You can examine the results by using either <TT>more</TT> or <TT>less</TT> and
paging through to the end of the file, or by using the Linux command <TT>tail</TT>,
which displays the last few lines of the specified file. In this case, you would
type <TT>tail test</TT> to see the last few lines of the file <TT>test</TT>. Try
using <TT>tail</TT>!
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading16<FONT COLOR="#000077"><B>NOTE:</B> </FONT>For a more detailed
discussion of redirection and piping, see Chapter 10, "<TT>bash</TT>."
<HR>
</DL>
<H3 ALIGN="CENTER"><A NAME="Heading17<FONT COLOR="#000077">Notational Conventions
Used to Describe Linux Commands</FONT></H3>
<H3 ALIGN="CENTER"><FONT COLOR="#000077"></FONT></H3>
<P>There is a set of accepted notational conventions used to describe, in a concise
and consistent way, the correct syntax for any given Linux command. This specifies
what options or parameters you must use, what options or parameters you can use or
not use, and so on. Sometimes this set of conventions is used to give a complete
and exhaustive listing of a command's syntax, showing every possible command and
parameter. Sometimes it is used to make a particular example more general and the
command's basic usage clearer.</P>
<P>If you remember the following five basic rules, you will be able, in principle,
to understand the syntax of any Linux or UNIX command.
<H4 ALIGN="CENTER"><A NAME="Heading19<FONT COLOR="#000077">Six Basic Rules
of Linux Notation</FONT></H4>
<DL>
<DD><B>1.</B> Any text standing by itself, and not within <TT>[]</TT>, <TT><></TT>,
or <TT>{}</TT>, must be typed exactly as shown.<BR>
<B><BR>
2.</B> Any text within square brackets (<TT>[]</TT>) is optional. You can type it
or not type it. For instance, the syntax <TT>ls [-l]</TT> means you must type <TT>ls</TT>
(per the first rule), while adding <TT>-l</TT> is optional, but not necessary. Do
not type the square brackets themselves! In our example, type <TT>ls</TT> or <TT>ls
-l</TT>. Don't type <TT>ls [-l]</TT>.<BR>
<B><BR>
3. </B>Angle brackets (<TT><></TT>) and the text within them must be replaced
by appropriate text (usually a name or value). The text within the brackets usually
indicates the nature of the replacement. For instance, the syntax <TT>more <</TT>filename<TT>></TT>
means that you should replace <filename> with the name of the file you wish
to examine using <TT>more</TT>. If you want to look at the file <TT>test</TT>, you
would type <TT>more test</TT>. Remember, do not use the angle brackets when you actually
type the command!<BR>
<B><BR>
4.</B> Curly braces (<TT>{}</TT>) indicate that you must choose one of the values
given within the braces. The values are separated by <TT>|</TT> (which in this case
means or, not pipe!). For example, the syntax <TT>command -{a|b}</TT> means you must
enter either <TT>command -a</TT> or <TT>command -b</TT>.<BR>
<B><BR>
5.</B> An ellipsis (<TT>...</TT>) means "and so on." They are normally
used with parameters such as filenames, as described later.<BR>
<B><BR>
6. </B>The sixth basic rule states that the brackets can be combined as necessary.
For instance, you don't have to type a filename with the <TT>more</TT> command. This
would be indicated as <TT>more [<</TT>filename<TT>>]</TT>. The outer set of
square brackets makes the entire parameter optional. If you do decide to use the
parameter, replace the inner set of angle brackets with the appropriate value. Because
the <TT>more</TT> command enables one or more filenames to be specified, the syntax
becomes <TT>more [<</TT>filename<TT>>...]</TT>. The ellipsis means you can
have as many <TT><</TT>filenames<TT>></TT> as you want.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -