📄 325-327.html
字号:
<HTML>
<HEAD>
<TITLE>Special Edition Using Linux, Fourth Edition:Managing Files and Directories</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=0789717468//-->
<!--TITLE=Special Edition Using Linux, Fourth Edition//-->
<!--AUTHOR=Jack Tackett//-->
<!--AUTHOR=Jr.//-->
<!--AUTHOR=Steve Burnett//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Que//-->
<!--CHAPTER=17//-->
<!--PAGES=325-327//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="323-325.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="327-329.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>If you try to delete a directory with the <TT>rm</TT> command, you’re told that it’s a directory and can’t be deleted. If you want to delete empty directories, use the <TT>rmdir</TT> command, as with MS-DOS.</P>
<P>Linux offers another way to delete directories and their contents, but it’s far more dangerous. The <TT>rm -r</TT> command recursively deletes any directories and files it encounters. If you have a directory named ./foo that contains files and subdirectories, the command <TT>rm -r foo</TT> deletes the ./foo directory and its contents, including all subdirectories.</P>
<P>If you give the command <TT>rm -i -r</TT>, each directory that the <TT>rm</TT> command encounters triggers a confirmation prompt. You must answer yes before the directory and its contents are deleted. If you left any files in the directory you were attempting to delete, <TT>rm</TT> balks, just as it does if you attempt to remove the nonempty directory with the <TT>rm</TT> command with no options.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>You don’t have to issue each flag individually for a Linux command. If the flag doesn’t take an argument, you can combine the flags. Thus, rm -i -r can be issued as <TT>rm -ir</TT>.<HR></FONT>
</BLOCKQUOTE>
<H3><A NAME="Heading7"></A><FONT COLOR="#000077">Viewing the Contents of a File</FONT></H3>
<P>Almost every Linux command prints to the standard output device, typically your screen. If the command takes its input from a file after manipulating the file in some way, the command prints the file to your screen. The trick in choosing a Linux command depends on how you want the file displayed. You can use three standard commands: <TT>cat</TT>, <TT>more</TT>, and <TT>less</TT>.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>Linux, as all UNIX systems do, opens four system files at startup: standard input, standard output, standard error, and AUX. These files are actually physical devices:<HR></FONT>
</BLOCKQUOTE>
<CENTER>
<TABLE WIDTH="90%">
<TR>
<TH WIDTH="35%" ALIGN="LEFT">Name
<TH WIDTH="35%" ALIGN="LEFT">Alias
<TH WIDTH="30%" ALIGN="LEFT">Device
<TR>
<TD>Standard input
<TD>standard in (stdin)
<TD>The keyboard
<TR>
<TD>Standard output
<TD>standard out (stdout)
<TD>The screen
<TR>
<TD>Standard error
<TD>standard err (stderr)
<TD>The screen
<TR>
<TD>AUX
<TD>auxiliary
<TD>An auxiliary device
</TABLE>
</CENTER>
<H4 ALIGN="LEFT"><A NAME="Heading8"></A><FONT COLOR="#000077">Using <I>cat</I> to View a File
</FONT></H4>
<P>For displaying short ASCII files, the simplest command is <TT>cat</TT>, which stands for <I>concatenate</I>. The <TT>cat</TT> command takes a list of files (or a single file) and prints the contents unaltered on standard output, one file after another. Its primary purpose is to concatenate files (as in <TT>cat <I>file1 file2>file3</I></TT>), but it works just as well to send the contents of a short file to your screen.</P>
<P>If you try to display large files by using <TT>cat</TT>, the file scrolls past your screen as fast as the screen can handle the character stream. One way to stop the flow of data is to alternatively press <Ctrl-s> and <Ctrl-q> to send start and stop messages to your screen, or you can use one of the page-at-a-time commands, <TT>more</TT> or <TT>less</TT>.</P>
<H4 ALIGN="LEFT"><A NAME="Heading9"></A><FONT COLOR="#000077">Using <I>more</I> to View a File
</FONT></H4>
<P>Both <TT>more</TT> and <TT>less</TT> display a screen of data at a time. Although they both do roughly the same thing, they do it differently. <TT>more</TT> and <TT>less</TT> determine how many lines your terminal can display from the terminal database and from your <TT>TERM</TT> environment variable.</P>
<P>The <TT>more</TT> command is older than <TT>less</TT>, and it’s derived from the Berkeley version of UNIX. It proved so useful that, like the <TT>vi</TT> editor, it has become a standard. This section covers just the basics of the command.</P>
<P>The simplest form of the <TT>more</TT> command is <TT>more <I>filename</I></TT>. You see a screen of data from the file. If you want to go on to the next screen, press the space bar. If you press <Return>, only the next line is displayed. If you’re looking through a series of files (with the command <TT>more <I>file1 file2</I> …</TT>) and want to stop to edit one, you can do so with the <TT>e</TT> or <TT>v</TT> command. Pressing <e> within <TT>more</TT> invokes whatever editor you’ve defined in your <TT>EDIT</TT> shell environment variable on the current file. Pressing <v> uses whatever editor has been defined in the <TT>VISUAL</TT> variable. If you haven’t defined these variables in your environment, <TT>more</TT> defaults to the <TT>ed</TT> editor for the <TT>e</TT> command and to the <TT>vi</TT> editor for the <TT>v</TT> command.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR>• <B>See</B> “Setting the Shell Environment,” <B>p. 344</B><HR></FONT>
</BLOCKQUOTE>
<P>The <TT>more</TT> command has only one real drawback—you can’t go backward in a file and redisplay a previous screen. However, you can go backward in a file with <TT>less</TT>.</P>
<H4 ALIGN="LEFT"><A NAME="Heading10"></A><FONT COLOR="#000077">Using <I>less</I> to View a File
</FONT></H4>
<P>One disadvantage to the <TT>less</TT> command is that you can’t use an editor on a file being displayed. However, <TT>less</TT> makes up for this deficiency by allowing you to move forward and backward through a file.</P>
<P>The <TT>less</TT> command works almost the same way that <TT>more</TT> does. To page through a file, type the command <TT><B>less <I>filename</I></B></TT>. One screen of data is displayed. To advance to the next screen, press the Spacebar as you did with the <TT>more</TT> command.</P>
<P>To move backward in a file, press the <b> key. To go to a certain position expressed as a percentage of the file, press <p> and specify the percentage at the <TT>:</TT> prompt.</P>
<H4 ALIGN="LEFT"><A NAME="Heading11"></A><FONT COLOR="#000077">Searching Through a File and Escaping to the Shell</FONT></H4>
<P>The <TT>less</TT> and <TT>more</TT> commands allow you to search for strings in the file being displayed. The <TT>less</TT> command, however, allows you to search backward through the file as well. Use the search syntax <TT>less <I>/string</I></TT> to search backward through the file. With the <TT>less</TT> and <TT>more</TT> commands, if a string is found, a new page is displayed with the line containing the matching string at the top of the screen. With <TT>less</TT>, pressing the <n> key repeats the previous search.</P>
<P>The <TT>more</TT> and <TT>less</TT> commands also allow you to escape to the shell with the <TT>!</TT> command. When you escape to the shell with the <TT>!</TT> command, you’re actually in a subshell; you must exit the subshell just as you do when you log out from a session. Depending on which shell you’re using, you can press <Ctrl-d> or type <TT>exit</TT> to return to the same screen in <TT>more</TT> or <TT>less</TT> that you escaped from. If you press <Ctrl-d> and get a message to use <TT>logout</TT> instead of <Ctrl-d>, use the <TT>logout</TT> command.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="323-325.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="327-329.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 + -