📄 158-161.html
字号:
<HTML>
<HEAD>
<TITLE>Linux Unleashed, Third Edition:Using the File System</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=8//-->
<!--PAGES=158-161//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="156-158.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="161-164.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H3><A NAME="Heading15"></A><FONT COLOR="#000077">Creating and Deleting Files</FONT></H3>
<P>Linux has many ways to create and delete files. In fact, some of the ways are so easy to perform that you have to be careful not to accidentally overwrite or erase files!
</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Warning: </B><BR>Go through the following sections very carefully. You should be logged in as your “ordinary” username, <I>not</I> as <TT>root</TT>! Only when you’re absolutely sure you understand these sections thoroughly should you use these commands while logged in as <TT>root</TT>.
<P>There is no “unerase” command in Linux! Be <I>sure</I> you know what you’re doing!<HR></FONT>
</BLOCKQUOTE>
</P>
<P>Return to your home directory by typing <TT><B>cd</B></TT>. Make sure you’re in your <TT>/<B>home</B>/<<I>user</I>></TT> directory by running <TT><B>pwd</B></TT>.</P>
<P>In the last chapter, you created a file by typing <TT><B>ls -l /bin > output</B></TT>. Remember, the <TT><B>></B></TT> symbol means “redirect all output to the following filename.” Note that the file <TT><B>output</B></TT> didn’t exist before you entered this command. When you redirect to a file, Linux automatically creates the file if it doesn’t already exist.</P>
<P>What if you want to type text into a file rather than some command’s output? The quick and dirty way is to use the command <TT><B>cat</B></TT>.</P>
<H4 ALIGN="LEFT"><A NAME="Heading16"></A><FONT COLOR="#000077">cat: That Useful Feline</FONT></H4>
<P>The <TT><B>cat</B></TT> command is one of the simplest, yet most useful, commands in Linux. (It certainly does more than any living feline!) The <TT><B>cat</B></TT> command basically takes all input and outputs it to a file or other source such as the screen. By default, <TT><B>cat</B></TT> takes its input from the keyboard and outputs it to the screen. Type <TT><B>cat</B></TT> at the command line:</P>
<!-- CODE SNIP //-->
<PRE>
darkstar:~$ <B>cat</B>
</PRE>
<!-- END CODE SNIP //-->
<P>The cursor moves down to the next line, but nothing else seems to happen. Now <TT><B>cat</B></TT> is waiting for some input so type a few short lines:</P>
<!-- CODE SNIP //-->
<PRE>
hello
hello
what
what
asdf
asdf
</PRE>
<!-- END CODE SNIP //-->
<P>Everything you type is repeated onscreen as soon as you press Enter!
</P>
<P>How do you get out of this? At the start of a line, type <TT><B>^D</B></TT> (Ctrl+D). (In other words, hold the Ctrl key and press D.) If you’re not at the beginning of a line, you have to type <TT><B>^D</B></TT> twice. <TT><B>^D</B></TT> is the Linux “end of file” character. When a program such as <TT><B>cat</B></TT> encounters a <TT><B>^D</B></TT>, it assumes that it has finished with the current file, and it goes on to the next one. In this case, if you type <TT><B>^D</B></TT> by itself on an empty line, there is no next file to go on to, and <TT><B>cat</B></TT> exits.</P>
<P>In this simple exercise, <TT><B>cat</B></TT> accepted input from the keyboard and displayed it back to you on the screen. Not a very useful command so far, is it? Fortunately, there’s a lot more flexibility to <TT><B>cat</B></TT>.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Note: </B><BR>When you say that a program <I>exits,</I> you mean that it has finished running and that you are back at the Linux command prompt. It may seem odd to talk about the <I>program</I> exiting when, from your point of view as a user, you have exited the program. This turn of phrase goes back to the early days of UNIX, when it was coined by the people who were programming the system. They looked at things from the program’s point of view, not the user’s!<HR></FONT>
</BLOCKQUOTE>
<P>So how do you use <TT><B>cat</B></TT> to create a file? Simple! You redirect the output of <TT><B>cat</B></TT> from the screen to a file:</P>
<!-- CODE SNIP //-->
<PRE>
darkstar:~$ <B>cat > newfile</B>
<B>Hello world</B>
<B>Here’s some text</B>
</PRE>
<!-- END CODE SNIP //-->
<P>You can type as much as you want. When you are finished, press <TT><B>^D</B></TT> by itself on a line; you are back at the Linux prompt. Instead of showing you each line as you typed it on the screen, <TT><B>cat</B></TT> has redirected it to a file called <TT><B>newfile</B></TT>.</P>
<P>Now you want to look at the contents of <TT><B>newfile</B></TT>. You can use the <TT><B>more</B></TT> or <TT><B>less</B></TT> commands, but instead, let’s use <TT><B>cat</B></TT>. Yes, you can use <TT><B>cat</B></TT> to look at files simply by providing it with a filename:</P>
<!-- CODE SNIP //-->
<PRE>
darkstar:~$ <B>cat newfile</B>
Hello world
Here’s some text
darkstar:~$
</PRE>
<!-- END CODE SNIP //-->
<P>Cool! You can also add to the end of the file by using <TT><B>>></B></TT>. Whenever you use <TT><B>>></B></TT>, whether with <TT><B>cat</B></TT> or any other command, the output is always <I>appended</I> to the specified file. (Note that the <TT><B>^D</B></TT> character does not appear onscreen. It’s shown in the examples for clarity.)</P>
<!-- CODE SNIP //-->
<PRE>
darkstar:~$ <B>cat >> newfile</B>
<B>Some more lines</B>
<B>^D</B>
darkstar:~$ <B>cat newfile</B>
Hello world
Here’s some text
Some more lines
darkstar:~$
</PRE>
<!-- END CODE SNIP //-->
<P>To discover what <TT><B>cat</B></TT> actually stands for, let’s create another file.</P>
<!-- CODE SNIP //-->
<PRE>
darkstar:~$ <B>cat > anotherfile</B>
<B>Different text</B></B>
<B>^D</B>
darkstar:~$
</PRE>
<!-- END CODE SNIP //-->
<P>Now, try this:
</P>
<!-- CODE SNIP //-->
<PRE>
darkstar:~$ <B>cat newfile anotherfile> thirdfile</B>
darkstar:~$ <B>cat thirdfile</B>
Hello world
Here’s some text
Some more lines
Different text
darkstar:~$
</PRE>
<!-- END CODE SNIP //-->
<P><TT><B>cat</B></TT> stands for <I>concatenate</I>; <TT><B>cat</B></TT> takes all the specified inputs and regurgitates them in a single lump. We’ve concatenated newfile and anotherfile together into thirdfile. This by itself would not be very interesting, but combine it with the various forms of input and output redirection available in Linux and you have a powerful and useful tool.</P>
<P>Sometimes you want to change just one line of a file, or perhaps you are creating a large and complicated file and don’t want to rely on <TT><B>cat</B></TT> (which doesn’t allow you to correct errors). For this, you should use one of the editing programs available in Linux. They are discussed in Chapter 16, “Text Editors: <TT>vi</TT> and <TT>emacs</TT>.”</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="156-158.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="161-164.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 + -