164-166.html

来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 140 行

HTML
140
字号
<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=164-166//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="161-164.html">Previous</A></TD>

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

<TD><A HREF="167-170.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<P>When you use <TT><B>cp</B></TT>, you are actually making a second physical copy of your file and placing it on your disk. This can be slower (although for small files, you won&#146;t notice any difference), and it causes a bit more wear and tear on your computer. Don&#146;t make copies of files when all you really want to do is move them!</P>

<H4 ALIGN="LEFT"><A NAME="Heading19"></A><FONT COLOR="#000077">Moving and Copying with Wildcards</FONT></H4>

<P>If you have 20 files in a directory, and you want to copy them to another directory, it would be very tedious to use the <TT><B>cp</B></TT> command on each one. Fortunately, you can use the wildcards <B>*</B> and <B>?</B> to copy more than one file at a time.</P>

<P>If you want to move or copy <I>all</I> files in a directory, use the wildcard <B>*</B>:</P>

<!-- CODE SNIP //-->

<PRE>

darkstar:~&#36; <B>cp * /tmp</B>

</PRE>

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

<P>This command copies every file in your current directory to the directory <TT><B>/tmp</B></TT>.</P>

<P>You can use <B>*</B> along with other characters to match only certain files. For instance, suppose you have a directory that contains the files <TT><B>book1</B></TT>, <TT><B>book_idea</B></TT>, <B>book-chapter-1</B>, and <TT><B>poem.book</B></TT>. To copy just the first three files, you can type <TT><B>cp book* /tmp</B></TT>. When you type <TT><B>book*</B></TT>, you are asking Linux to match all files whose names start with <TT><B>book</B></TT>. In this case, <TT><B>poem.book</B></TT> does not start with <TT><B>book</B></TT>, so there is no way <TT><B>book*</B></TT> can match it. (Note that if your filename were <TT><B>book.poem, book*</B></TT> would match it.)</P>

<P>As you saw at the outset, <TT><B>mv</B></TT> and <TT><B>cp</B></TT> are very simple commands. It&#146;s specifying the files that is the complicated part! If things still seem confusing, don&#146;t worry. Even experts sometimes mess up &#147;simple&#148; moves and copies. Follow the examples and try any different ways you can think of. There is a definite logic as to how the files to be moved and copied should be specified. It takes a while to become familiar with this logic, and you will probably have to practice a while before these things become intuitive.</P>

<H4 ALIGN="LEFT"><A NAME="Heading20"></A><FONT COLOR="#000077">Moving Directories</FONT></H4>

<P>To move a directory, use the <TT><B>mv</B></TT> command. The syntax is <TT><B>mv</B> &lt;<I>directory</I>&gt; &lt;<I>destination</I>&gt;</TT>. In the following example, let&#146;s move the <TT><B>newdir</B></TT> subdirectory found in your current directory to the <TT><B>/tmp</B></TT> directory:</P>

<!-- CODE SNIP //-->

<PRE>

darkstar:~&#36; <B>mvdir newdir /tmp</B>

darkstar:~&#36; <B>cd /tmp</B>

darkstar:/tmp&#36; <B>ls</B>

/newdir

</PRE>

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

<P>The directory <TT><B>newdir</B></TT> is now a subdirectory of <TT><B>/tmp</B></TT>.</P>

<P>When you move a directory, all its files and subdirectories go with it.</P>

<P>You can use the <TT><B>mv</B></TT> command to rename a directory without moving it. For example, if you want to rename the directory <TT><B>newdir</B></TT> to <TT><B>olddir</B></TT> without copying it, the command</P>

<!-- CODE SNIP //-->

<PRE>

mv newdir olddir

</PRE>

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

<P>does the task. All the files in <TT><B>newdir</B></TT> now are under <TT><B>olddir</B></TT>.</P>

<H3><A NAME="Heading21"></A><FONT COLOR="#000077">Removing Files and Directories</FONT></H3>

<P>Now that you know how to create files and directories, it&#146;s time to learn how to undo your handiwork.

</P>

<P>To remove (or delete) a file, use the <TT><B>rm</B></TT> command (<TT><B>rm</B></TT> is a very terse spelling of <I>remove</I>). The syntax is <TT><B>rm</B> &lt;<I>filename</I>&gt;</TT>. For example, the command:</P>

<!-- CODE SNIP //-->

<PRE>

darkstar:~&#36; <B>rm dead_duck</B>

</PRE>

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

<P>removes the file <TT><B>dead_duck</B></TT> from your current directory.</P>

<!-- CODE SNIP //-->

<PRE>

darkstar:~&#36; <B>rm /tmp/dead_duck</B>

</PRE>

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

<P>removes the file <TT><B>dead_duck</B></TT> from the <TT><B>/tmp</B></TT> directory.</P>

<P>You can use wildcards with the <TT><B>rm</B></TT> command, just as with all other Linux commands. This can cause you a lot of problems when you type the command at the wrong location. For example, the command</P>

<!-- CODE SNIP //-->

<PRE>

darkstar:~&#36; <B>rm *</B>

</PRE>

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

<P>removes all files from your current directory. There&#146;s no way to undelete the files, so if you issued this command by accident, you&#146;re out of luck. The moral of the story is to be very careful when using wildcards with the <TT><B>rm</B></TT> command!</P>

<P>You can combine wildcards and directory paths. For example, the command</P>

<!-- CODE SNIP //-->

<PRE>

darkstar:~&#36; <B>rm /tmp/*duck</B>

</PRE>

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

<P>removes all files ending in <TT><B>duck</B></TT> from the <TT><B>/tmp</B></TT> directory.</P>

<P>As soon as a file is removed, it is <I>gone!</I> Always think about what you&#146;re doing before you remove a file. You can use one of the following options to keep out of trouble when using wildcards:</P>

<DL>

<DD><B>&#149;</B>&nbsp;&nbsp;Run <TT><B>ls</B></TT> using the same file specification you use with the <TT><B>rm</B></TT> command. For instance:

<!-- CODE SNIP //-->

<PRE>

darkstar:~&#36; <B>ls *duck

</B>dead_duck  guiduck  lame-duck

:~&#36; rm *duck

</PRE>

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

<BR>In this case, you <I>thought</I> you wanted to remove all files that matched <TT><B>*duck</B></TT>. To verify that this indeed was the case, all the <TT><B>*duck</B></TT> files were listed (wildcards work the same way with all commands). The listing looked okay, so you went ahead and removed the files.

<DD><B>&#149;</B>&nbsp;&nbsp;Use the <TT><B>i</B></TT> (interactive) option with <TT><B>rm</B></TT>:

<!-- CODE SNIP //-->

<PRE>

darkstar:~&#36; rm -i *duck

rm: remove &#145;dead_duck&#146;? y

rm: remove &#145;guiduck&#146;? n

rm: remove &#145;lame-duck&#146;? y

darkstar:~&#36;

</PRE>

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

<BR>When you use <TT><B>rm -i</B></TT>, the command goes through the list of files to be deleted one by one, prompting you for the okay to remove the file. If you type <TT><B>y</B></TT> or <TT><B>Y</B></TT>, <TT><B>rm</B></TT> removes the file. If you type any other character, <TT><B>rm</B></TT> does not remove it. The only disadvantage of using this interactive mode is that it can be very tedious when the list of files to be removed is long.

</DL>

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="161-164.html">Previous</A></TD>

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

<TD><A HREF="167-170.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





</td>
</tr>
</table>

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?