⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 221-225.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTML
字号:
<HTML>

<HEAD>

<TITLE>Linux Configuration and Installation:Basic Linux Tools</TITLE>

<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!--ISBN=1558285660//-->

<!--TITLE=Linux Configuration and Installation//-->

<!--AUTHOR=Patrick Volkerding//-->

<!--AUTHOR=Kevin Reichard//-->

<!--AUTHOR=Eric Foster//-->

<!--PUBLISHER=IDG Books Worldwide, Inc.//-->

<!--IMPRINT=M & T Books//-->

<!--CHAPTER=4//-->

<!--PAGES=221-225//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="218-221.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="225-228.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H4 ALIGN="LEFT"><A NAME="Heading18"></A><FONT COLOR="#000077">Using Cat</FONT></H4>

<P>The <B>cat</B> command does so many things under the UNIX and Linux operating systems, it&#146;s a wonder you don&#146;t use it for everything. On a very basic level, <B>cat</B> can be used to view the contents of a file:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~$ cat <I>filename</I>

</PRE>

<!-- END CODE SNIP //-->

<P>where <I>filename</I> is the name of the file you want to view. For example, to view the contents of a file named <I>test</I>, you&#146;d use the following command line:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~$ cat test

     This is our Linux test file. Big whoop.

</PRE>

<!-- END CODE SNIP //-->

<P><B>Cat</B>, by default, displays its output to the screen. However, <B>cat</B> can be told to send its output elsewhere, which brings us to another of its many uses: It can also be used to store a file under a different filename, much in the manner of the <B>cp</B> command (which will be covered later in this chapter). For example, to create another copy of the <B>test</B> file (which we&#146;ll call <B>memo.kr</B>), you&#146;d use the following command line:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~$ cat test &gt; memo.kr

     gilbert:~$ ls

     memo.kr     test

</PRE>

<!-- END CODE SNIP //-->

<P>In this example, <B>cat</B> uses the output from the <B>test</B> file as the input for the <B>memo.kr</B> file.</P>

<P><B>Cat</B> can also be used to create simple ASCII files; we say <I>simple</I> because <B>cat</B> sends your keyboard input directly to a file, rather than giving you the chance to edit the file. (The full-screen editors <B>elvis</B> and <B>emacs</B> can be used to edit files.) To create a simple file named <B>memo</B>, you&#146;d use the following command line:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~$ cat &gt; memo

</PRE>

<!-- END CODE SNIP //-->

<P>Anything you type would go directly into the <B>memo</B> file one line at a time. When creating a file like this, there are a few things to remember:</P>

<DL>

<DD><B>&#149;</B>&nbsp;&nbsp;Hit the <B>Enter</B> key at the end of every line. Otherwise, part of your typing will end up in the ether.

<DD><B>&#149;</B>&nbsp;&nbsp;You can move within the line using the <B>Backspace</B> key (well, partially, anyway; <B>Backspace</B> will merely delete the preceding character). You can&#146;t move to a previous line, however.

<DD><B>&#149;</B>&nbsp;&nbsp;Type <B>Ctrl-D</B> when you&#146;re finished typing.

</DL>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>NOTE:&nbsp;&nbsp;</B>The <B>Ctrl-D</B> sequence can be used whenever you run a Linux command that requires keyboard input.<HR></FONT>

</BLOCKQUOTE>

<P>Finally, <B>cat</B> can be used to combine files. For example, you can add to the aforementioned <B>memo</B> file with the following command line:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~$ cat &gt;&gt; memo

</PRE>

<!-- END CODE SNIP //-->

<P>Whatever you type will be added to the <B>memo</B> file. The previous rules apply. In addition, you can redirect two existing files as input to a new third file:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~$ cat memo1 memo2 &gt; memo3

</PRE>

<!-- END CODE SNIP //-->

<P>The order of the files on the command line determines the order of the data in the new file.

</P>

<P>There are a host of options to the <B>cat</B> command; they are listed in Table 4.7.</P>

<CENTER>

<TABLE WIDTH="95%"><CAPTION><B>Table 4.7</B> Options to the Cat Command

<TR>

<TH WIDTH="25%" ALIGN="LEFT">Option

<TH WIDTH="75%" ALIGN="LEFT">Result

<TR>

<TH COLSPAN="2"><HR>

<TR>

<TD>-b

<TD>Numbers all lines, except for those not containing characters.

<TR>

<TD>-n

<TD>Numbers all lines.

<TR>

<TD>-s

<TD>Replaces a series of blank lines with a single blank line.

<TR>

<TD>-v

<TD>Prints nonprinting (i.e., control) characters.

<TR>

<TD COLSPAN="2"><HR>

</TABLE>

</CENTER>

<H4 ALIGN="LEFT"><A NAME="Heading19"></A><FONT COLOR="#000077">Other Ways to View a File</FONT></H4>

<P>Linux contains two handy tools for viewing a file: <B>more</B> and <B>less</B>. The <B>more</B> command is pretty simple; the following command line launches <B>more</B> with the file <B>test</B>:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~$ more test

</PRE>

<!-- END CODE SNIP //-->

<P>The <B>more</B> command presents one page of text at a time, with the percentage of text displayed at the bottom of the screen. Use the <B>Enter</B> key to move forward one line in the document, or press the <B>Spacebar</B> to move ahead an entire page. Unfortunately, you can&#146;t move back to the beginning of a file once it&#146;s scrolled by.</P>

<P>In addition, <B>more</B> gives you the ability to search for a specific text string, by typing:</P>

<!-- CODE SNIP //-->

<PRE>

     /<I>string</I>

</PRE>

<!-- END CODE SNIP //-->

<P>where <I>string</I> is the text string you want to search for.</P>

<P><FONT SIZE="+1"><B>Where Less is More Than More</B></FONT></P>

<P>The <B>less</B> command isn&#146;t part of the standard UNIX distribution, but it&#146;s a very useful addition to the Linux command set. The <B>less</B> command provides more options when viewing a file&#151;namely, the ability to move both forward and backward through a file. Again, to use <B>less</B> to view the file named <B>test</B>, you&#146;d use the following command line:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~$ less test

</PRE>

<!-- END CODE SNIP //-->

<P>As with <B>more</B>, you can use the /<I>string</I> option to search for text.</P>

<P>A big advantage to <B>less</B> is its ability to search backward through a file by pressing <B>b</B>.</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>NOTE:&nbsp;&nbsp;</B>An X Window version of this program, <B>xless</B>, is also included as part of Slackware.<HR></FONT>

</BLOCKQUOTE>

<P><FONT SIZE="+1"><B>Using Head and Tail to View Portions of a File</B></FONT></P>

<P>If a file is especially large, you may not want to load all of it and try to scroll through it, particularly if you&#146;re just interested in a quick glance at its contents. In this case, you can use the <B>head</B> command to view the beginning of the fil, or tail to view the end of the file. For both commands, the default is to display 10 lines. Therefore, to display the first 10 lines of the file <B>report</B>, you&#146;d use the following command line:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~$ head report

</PRE>

<!-- END CODE SNIP //-->

<P>To view the last 10 lines of the file <B>report</B>, you&#146;d use the following command line:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~$ tail report

</PRE>

<!-- END CODE SNIP //-->

<P>To change the default of 10 lines, you&#146;d specify a new number as an argument to the command; the following, for example, displays the first 20 lines of the file <B>report</B>:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~$ head -20 report

</PRE>

<!-- END CODE SNIP //-->

<P><FONT SIZE="+1"><B>Viewing an Octal Dump with Od</B></FONT></P>

<P>Finally, there&#146;s the <B>od</B> command, which allows you to view an octal dump of a file:</P>

<!-- CODE SNIP //-->

<PRE>

     $ od filename

</PRE>

<!-- END CODE SNIP //-->

<P>where <I>filename</I> is the name of the file to be viewed.</P><P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="218-221.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="225-228.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 + -