📄 228-230.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=228-230//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="225-228.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="230-234.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading22"></A><FONT COLOR="#000077">Moving and Renaming Files with Mv</FONT></H4>
<P>The <B>mv</B> command is used to move files from one directory to another. This command doesn’t leave a copy of the original file in the original location (for that, use the <B>cp</B> command); it deletes the original copy and inserts the new copy in the new location.</P>
<P>The following command line would move the <B>textfile</B> file to the new home (<B>~</B>) location:</P>
<!-- CODE SNIP //-->
<PRE>
gilbert:/usr$ mv textfile ~
</PRE>
<!-- END CODE SNIP //-->
<P>If you were to run the <B>ls</B>, you’d find that <B>textfile</B> didn’t appear in <B>/usr</B>, but was now located in your home directory.</P>
<P>In this example, <B>textfile</B> retains its current filename, no matter where you move it. You can also use the <B>mv</B> command to rename a file. (In fact, it’s one of the few ways to actually rename a file, because there’s no command for doing so within Linux.) The following command changes the <B>textfile</B> filename to <B>aardvark</B>:</P>
<!-- CODE SNIP //-->
<PRE>
gilbert:~$ mv textfile aardvark
</PRE>
<!-- END CODE SNIP //-->
<P>The following command line would move <B>textfile</B> to a new directory <I>and</I> give it a new filename of <B>aardvark</B>:</P>
<!-- CODE SNIP //-->
<PRE>
gilbert:/usr$ mv textfile ~/aardvark
</PRE>
<!-- END CODE SNIP //-->
<P>Linux can be fairly harsh when you’re moving and renaming files. For example, the <B>mv</B> command will overwrite an existing file with a renamed file and not warn you. If you ran the following command line and a file named <B>aardvark</B> already existed in your home directory, you’d be in trouble:</P>
<!-- CODE SNIP //-->
<PRE>
gilbert:/usr$ mv textfile ~/aardvark
</PRE>
<!-- END CODE SNIP //-->
<P>as <B>mv</B> would overwrite the original <B>aardvark</B> file with the new <B>aardvark</B> file. To avoid this problem, use the <I>-i</I> option with the <B>mv</B> command:</P>
<!-- CODE SNIP //-->
<PRE>
gilbert:/usr$ mv -i textfile ~/aardvark
mv: overwrite 'aardvark'?
</PRE>
<!-- END CODE SNIP //-->
<P>Type <B>y</B> if you want to overwrite <B>aardvark</B>, <B>n</B> (or any other key) if you do not.</P>
<P>A summary of the options to <B>mv</B> are listed in Table 4.9.</P>
<CENTER>
<TABLE WIDTH="95%"><CAPTION><B>Table 4.9</B> A Summary of the Mv Command Options
<TR>
<TH WIDTH="25%" ALIGN="LEFT">Option
<TH WIDTH="75%" ALIGN="LEFT">Result
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TD>-f
<TD>Overwrites existing file.
<TR>
<TD>-I
<TD>Checks before overwriting existing files.
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
</CENTER>
<H4 ALIGN="LEFT"><A NAME="Heading23"></A><FONT COLOR="#000077">Removing Files with Rm</FONT></H4>
<P>The <B>rm</B> (short for <I>remove</I>) command removes files. Simple enough, right? To remove a file, simply list it on the command line:</P>
<!-- CODE SNIP //-->
<PRE>
gilbert:~$ rm aardvark
</PRE>
<!-- END CODE SNIP //-->
<P><B>Aardvark</B> will then be swiftly and painlessly removed—so swiftly that you won’t have a chance to confirm your choice. However, like the other commands listed in this chapter, you can tell Linux to confirm your file deletions, in the form of the <I>-i</I> option:</P>
<!-- CODE SNIP //-->
<PRE>
gilbert:~$ rm -i aardvark
rm: remove 'aardvark'?
</PRE>
<!-- END CODE SNIP //-->
<P>Type <B>y</B> if you want to remove <B>aardvark</B>, <B>n</B> (or any other key) if you do not.</P>
<P>Other options to the <B>rm</B> command are listed in Table 4.10.</P>
<CENTER>
<TABLE WIDTH="95%"><CAPTION><B>Table 4.10</B> Options to the Rm Command
<TR>
<TH WIDTH="25%" ALIGN="LEFT">Option
<TH WIDTH="75%" ALIGN="LEFT">Result
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TD>-f
<TD>Removes the file without any input from you.
<TR>
<TD>-i
<TD>Runs in interactive mode.
<TR>
<TD>-v
<TD>Runs in verbose mode, which means files are listed as they are removed.
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
</CENTER>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>WARNING: </B>Be warned that when you remove a file under Linux, you’re <I>really</I> removing the file from existence.
<P>If you’re a PC or Macintosh user, you may have gotten spoiled by utilities like The Norton Utilities, which can “unerase” files that have been erased. At this time, no such utilities exist for Linux.<HR></FONT>
</BLOCKQUOTE>
</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>WARNING: </B>Be careful when you combine the <B>rm</B> command and wildcards, because a wildcard—especially an asterisk—in the wrong spot can wreak havoc with your system. For example, let’s say that you wanted to delete all the files ending with <I>.golf</I> on your system (let’s say the boss is beginning to be a little suspicious about your afternoon field trips and you want to remove any incriminating evidence). So you tell Linux to remove all files ending with <I>golf</I>—or you think you are, anyway:
<!-- CODE SNIP //-->
<PRE>
gilbert:~/memos$ rm * golf
</PRE>
<!-- END CODE SNIP //-->
<P>Disaster ensues. Because you placed a space between the asterisk wildcard and the rest of the command line, the <B>rm</B> command uses only the asterisk as an argument, ignoring the <I>golf</I> part of the command line. Since <I>every</I> file is returned by the asterisk wildcard, you’ve just removed all the files in your current directory. (By the way, the chance of this happening is an excellent argument for using the <I>-i</I> option at all times and setting it up as an alias.)<HR></FONT>
</BLOCKQUOTE>
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="225-228.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="230-234.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 + -