📄 0056-0058.html
字号:
<HTML>
<HEAD>
<TITLE>Sams Teach Yourself Linux in 24 Hours:Reading and Navigation Commands:EarthWeb Inc.-</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=0672311623 //-->
<!-- TITLE=Sams Teach Yourself Linux in 24 Hours//-->
<!-- AUTHOR=Bill Ball//-->
<!-- PUBLISHER=Macmillan Computer Publishing//-->
<!-- IMPRINT=Sams//-->
<!-- CHAPTER=04 //-->
<!-- PAGES=0041-0062 //-->
<!-- UNASSIGNED1 //-->
<!-- UNASSIGNED2 //-->
<P><CENTER>
<a href="0053-0055.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0059-0062.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-56"><P>Page 56</P></A>
<!-- CODE //-->
<PRE>
/usr/local/netscape
|-- dynfonts
|-- java
| `-- classes
|-- movemail-src
|-- nethelp
| `-- netscape
| |-- collabra
| |-- composer
| |-- confernc
| |-- home
| |-- messengr
| |-- navigatr
| |-- netcastr
| |-- nscal
| `-- shared
|-- plugins
`-- spell
17 directories
</PRE>
<!-- END CODE //-->
<P>If you're interested in this utility (or other file utilities), you'll find a compressed
archive containing its source, manual page, and ready-to-run binary at
</P>
<!-- CODE SNIP //-->
<PRE>
<a href="http://sunsite.unc.edu/pub/linux/utils/file">
http://sunsite.unc.edu/pub/Linux/utils/file</A>
</PRE>
<!-- END CODE SNIP //-->
<H4><A NAME="ch04_ 16">
Listing and Combining Files with the cat Command
</A></H4>
<P>The cat (concatenate file) command is used to send the contents of files to your screen.
This command may also be used to send files' contents into other files. Hour 6 covers terms
such as standard input, standard output, and redirection, and this section shows you some
basic uses for this command.
</P>
<P>Although cat may be useful for reading short files, it is usually used to either combine,
</P>
<P>create, overwrite, or append files. To use cat to look at a short file, you can enter
</P>
<!-- CODE //-->
<PRE>
# cat test.txt
This text file was created by the cat command.
Cat could be the world's simplest text editor.
If you read this book, you'll learn how to use cat.
This is the last line of text in this file.
</PRE>
<!-- END CODE //-->
<P>The cat command also has a number of options. If you'd like to see your file with
line numbers, perhaps to note a specific phrase, you can use the
-n option:
</P>
<!-- CODE //-->
<PRE>
# cat -n test.txt
<OL>
<LI> This text file was created by the cat command.
<LI> Cat could be the world's simplest text editor.
<LI> If you read this book, you'll learn how to use cat.
<LI> This is the last line of text in this file.
</OL>
</PRE>
<!-- END CODE //-->
<P>You can also use cat to look at several files at once, because
cat accepts wildcards, for example:
</P>
<!-- CODE SNIP //-->
<PRE>
# cat -n test*
</PRE>
<!-- END CODE SNIP //-->
<A NAME="PAGENUM-57"><P>Page 57</P></A>
<OL>
<LI> This text file was created by the cat command.
<LI> Cat could be the world's simplest text editor.
<LI> If you read this book, you'll learn how to use cat.
<LI> This is the last line of text in this file.
<LI> This is the first line of test2.txt.
<LI> This file was also created by cat.
<LI> This is the last line of test2.txt.
</OL>
<P>As you can see, cat has also included a second file in its output, and has numbered each
line of the output, not each file. Note that you could also see both files with
</P>
<!-- CODE SNIP //-->
<PRE>
# cat test.txt test2.txt
</PRE>
<!-- END CODE SNIP //-->
<P>The output will be exactly the same as if you'd used a wildcard. But looking at several
files is only one way to use cat. You can also use the
cat command with the redirection operator > to combine files. For example, if you would like to combine
test.txt and test2.txt into a third file called
test3.txt, you can use
</P>
<!-- CODE SNIP //-->
<PRE>
# cat test* > test3.txt
</PRE>
<!-- END CODE SNIP //-->
<P>You can check the result with
</P>
<!-- CODE //-->
<PRE>
# ls -l test*
-rw-rw-r-- 1 bball bball 190 Nov 12 17:39 test.txt
-rw-rw-r-- 1 bball bball 108 Nov 12 17:45 test2.txt
-rw-rw-r-- 1 bball bball 298 Nov 12 18:00 test3.txt
</PRE>
<!-- END CODE //-->
<P>But what if you want to combine test.txt and
test2.txt without creating a larger, third file?
In this case, you'd first decide whether you want the contents of
test.txt to go into test2.txt, or the contents of
test2.txt to go into test.txt. Then, using cat with the
>> redirection operator, you might type
</P>
<!-- CODE SNIP //-->
<PRE>
# cat test.txt >> test2.txt
</PRE>
<!-- END CODE SNIP //-->
<P>This appends the contents of test.txt to the end of the
test2.txt. To check the results, use cat again:
</P>
<!-- CODE //-->
<PRE>
# cat test2.txt
This is the first line of test2.txt.
This file was also created by cat.
This is the last line of test2.txt.
This text file was created by the cat command.
Cat could be the world's simplest text editor.
If you read this book, you'll learn how to use cat.
This is the last line of text in this file.
</PRE>
<!-- END CODE //-->
<P>Note that if you had entered the command
</P>
<!-- CODE SNIP //-->
<PRE>
# cat -n test.txt >> test2.txt
</PRE>
<!-- END CODE SNIP //-->
<P>The test2.txt file would look like
</P>
<!-- CODE SNIP //-->
<PRE>
# cat test2.txt
</PRE>
<!-- END CODE SNIP //-->
<A NAME="PAGENUM-58"><P>Page 58</P></A>
<!-- CODE //-->
<PRE>
This is the first line of test2.txt.
This file was also created by cat.
This is the last line of test2.txt.
<OL>
<LI> This text file was created by the cat command.
<LI> Cat could be the world's simplest text editor.
<LI> If you read this book, you'll learn how to use cat.
<LI> This is the last line of text in this file.
</OL>
</PRE>
<!-- END CODE //-->
<P>Finally, here's a trick you can use if you want to create a short text file without
running a word processor or text editor. Because the
cat command can read the standard input (more about this in Hour 6), you can make the
cat command create a file and fill it with your keystrokes. Here's how:
</P>
<!-- CODE SNIP //-->
<PRE>
# cat > myfile.txt
</PRE>
<!-- END CODE SNIP //-->
<P>Now, enter some text:
</P>
<!-- CODE SNIP //-->
<PRE>
# cat > myfile.txt
This is the cat word processor.
This is the end of the file.
</PRE>
<!-- END CODE SNIP //-->
<P>Then, when you're done typing, press Ctrl+D to close the file. To see if this works, try
</P>
<!-- CODE //-->
<PRE>
# ls -l myfile.txt
-rw-rw-r-- 1 bball bball 61 Nov 12 18:26 myfile.txt
# cat myfile.txt
This is the cat word processor.
This is the end of the file.
</PRE>
<!-- END CODE //-->
<P>You should also know that the cat command will print out the contents of any file, and
not just text files. Although cat may be useful to look at one or several short files, what do
you do when you want to read longer text files? Read on, and you'll learn about pager
commands, which will make life easier when reading longer files.
</P>
<H4><A NAME="ch04_ 17">
Reading Files with the more Command
</A></H4>
<P>The more command is one of a family of Linux commands called
pagers. Pager commands let you browse through files, reading a screen or line at a time. This can be
extremely helpful, especially if you read a lot of manual pages, because the
man command will use a pager to display each page.
</P>
<P>The more command is a traditional pager in the sense that it provides the basic features
of early pagers. You can use more on the command line with
</P>
<!-- CODE SNIP //-->
<PRE>
# more longfile.txt
</PRE>
<!-- END CODE SNIP //-->
<P>If you need help, you can tap the H key to see a help screen. You can also run
other commands from inside more by using the exclamation character
(!). Reading through a text file is easy because you can advance one screenful (the current screen size) by tapping
</P>
<P><CENTER>
<a href="0053-0055.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0059-0062.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -