📄 130-132.html
字号:
<HTML>
<HEAD>
<TITLE>Linux Unleashed, Third Edition:Basic Linux Commands</TITLE>
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0672313723//-->
<!--TITLE=Linux Unleashed, Third Edition//-->
<!--AUTHOR=Tim Parker//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=7//-->
<!--PAGES=130-132//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="128-130.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="132-134.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading5"></A><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 earlier with the example <TT><B>ls -l /bin</B></TT>.</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><B>|</B></TT> character. (This character normally is found on the same key as the <TT><B>\</B></TT> character. You must hold the Shift key or you get <TT><B>\</B></TT> instead of <TT><B>|</B></TT>). The <TT><B>|</B></TT> character means “Use the output from the previous command as the input for the next command.” Therefore, typing <TT><B>command_1|command_2</B></TT> does both commands, one after the other, before giving you the results.</P>
<P>Using our assembly-line metaphor, items are being processed 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.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Tip: </B><BR>Although Linux doesn’t care whether <TT><B>|</B></TT> is set off by spaces, if <TT><B>command_1 | command_2</B></TT> is easier for you to read and understand than <TT><B>command_1|command_2</B></TT>, by all means use spaces around <TT><B>|</B></TT>.<HR></FONT>
</BLOCKQUOTE>
<P>You may have noticed that the output of <TT><B>ls -l /bin</B></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><B>more</B></TT>, which displays information in screen-sized chunks. When you enter <TT><B>ls -l /bin | more</B></TT>, you see the following:</P>
<!-- CODE //-->
<PRE>
darkstar:~$ <TT><B>ls -l /bin | more</B></TT>
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*
<TT><B>--More--</B></TT>
</PRE>
<!-- END CODE //-->
<P>The <TT><B>--More--</B></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><B>more</B></TT> displays another screen full of text. When the last screen with text has been displayed, <TT><B>more</B></TT> returns you to the Linux prompt.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Tip: </B><BR>The <TT><B>more</B></TT> command can do many other things. For instance, to move back one screen at a time, press <TT><B>b</B></TT> for “back.” (Not all versions of <TT><B>more</B></TT> support the <TT><B>b</B></TT> option.) Another useful command is <TT><B>q</B></TT> for “quit.” This lets you leave immediately, without having to go through all the remaining screens of text. Ctrl+C does the same thing.
<P>While in <TT><B>more</B></TT>, press <TT><B>h</B></TT> for “help.” This lists the commands available within <TT><B>more</B></TT>.<HR></FONT>
</BLOCKQUOTE>
</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Note: </B><BR>Linux sometimes includes the command <TT><B>less</B></TT> instead of <TT><B>more</B></TT>. One difference you will notice is that, unlike <TT><B>more</B></TT>, <TT><B>less</B></TT> requires you to type <TT><B>q</B></TT> to return to the command line, even if you’re at the end of the text to be displayed. This may seem cumbersome, but it prevents you from accidentally exiting the program by pressing the Spacebar once too often.
<P>The name <TT><B>less</B></TT> is a play on <TT><B>more</B></TT>. Originally, <TT><B>less</B></TT> was designed to have many features that <TT><B>more</B></TT> lacked. The version of <TT><B>more</B></TT> included in most Linux system has most of these features, however.</P>
<P>The Linux <TT><B>man</B></TT> program, discussed later, uses <TT><B>less</B></TT> to display text. Most other UNIX systems use <TT><B>more</B></TT> by default. Don’t get confused. Just remember to press <TT><B>q</B></TT> to exit from <TT><B>less</B></TT>!<HR></FONT>
</BLOCKQUOTE>
</P>
<P>Another thing you can do in Linux is send output to a file instead of the screen. There are many different reasons why you might want to do this. Perhaps you want to save a “snapshot” of a command’s output as it was at a certain time or maybe you 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><B>></B></TT> symbol (found above the period on your keyboard). For instance, you can place the output of the <TT><B>ls -l /bin</B></TT> command into a file called <TT><B>output</B></TT> by typing <TT><B>ls -l /bin > output</B></TT>. Again, spaces around <TT><B>></B></TT> are optional and not strictly necessary, but they do make the command much more readable.</P>
<P>If you now enter an <TT><B>ls</B></TT> or <TT><B>ls -l command</B></TT>, you will see that you’ve created a new file called <TT><B>output</B></TT> in your own directory.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="128-130.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="132-134.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -