⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 323-325.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 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=323-325//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="320-323.html">Previous</A></TD>

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

<TD><A HREF="325-327.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H3><A NAME="Heading5"></A><FONT COLOR="#000077">Moving and Renaming Files</FONT></H3>

<P>In Linux, moving and renaming files are accomplished with the same command: <TT>mv</TT>. The syntax and rules are the same for <TT>mv</TT> as they are for the copy command, <TT>cp</TT>. That is, you can move as many files as you want to a directory, but the directory name must be last in the list and you must have write permission to that directory.</P>

<P>One thing you can do with <TT>mv</TT> that you can&#146;t do with <TT>cp</TT> is move or rename directories. When you move or rename a file, the only thing that happens is the entry in the directory file is changed. Unless the new location is on another physical disk or partition, the file and the contents of the directory are physically moved.</P>

<P>If you try to use <TT>rm</TT> (for <I>remove</I>) or <TT>cp</TT> without options on a directory, the command fails and displays a message telling you that the item you&#146;re dealing with is a directory. To remove or copy directories, you must use the <TT>-r</TT> flag (for <I>recursive</I>) with <TT>rm</TT> and <TT>cp</TT>. The <TT>mv</TT> command, however, moves directories quite happily.</P>

<H3><A NAME="Heading6"></A><FONT COLOR="#000077">Removing Files or Directories</FONT></H3>

<P>The command to remove a file is <TT>rm</TT>. To delete a file you don&#146;t own, you need read and write permission. If you own the file, you&#146;re allowed to delete it, provided that you haven&#146;t closed off your own permission to the file. For example, if you turn off write permission to a file by typing <TT><B>chmod 000 <I>file</I></B></TT>, you must open permission again with the <TT>chmod</TT> command (by typing <TT><B>chmod 644 <I>file</I></B></TT>) before you can delete it.</P>

<P>If you accidentally type <TT><B>rm *</B></TT>, you delete all the files you have permission to delete in the current directory; you don&#146;t delete the subdirectories. To delete subdirectories, you must use the recursive option (<TT>-r</TT>).</P>

<P>Some versions of <TT>rm</TT> stop and ask whether you really want to delete files that you own but don&#146;t have at least write permission for. Other versions of <TT>rm</TT> prompt you for any files marked for removal with wildcards. Indeed, you can write a macro or shell script that gives you a second chance before actually deleting a file.</P>

<P>If your version of <TT>rm</TT> balks at removing files you own but don&#146;t have write permission for, you can partially protect yourself from accidentally deleting everything in your directory by following these steps:</P>

<DL>

<DD><B>1.</B>&nbsp;&nbsp;Create a file named 0. In the ASCII string sequence, the number 0 is listed before any files that begin with letters.

<DD><B>2.</B>&nbsp;&nbsp;Remove all permissions from the file named 0 by typing the command <TT><B>chmod 000 0</B></TT>. This command removes read, write, and execute permissions for everyone, including yourself.

<DD><B>3.</B>&nbsp;&nbsp;If you type the command <TT><B>rm *</B></TT>, the file named 0 is the first file that <TT>rm</TT> attempts to remove.

</DL>

<P>If your version of <TT>rm</TT> balks at removing the 0 file when you type <TT><B>rm *</B></TT>, you have the chance to think about what you just did. If you didn&#146;t intend to delete everything in your directory, press &lt;Del&gt; or &lt;Ctrl-c&gt; to kill the <TT>rm</TT> process. To test this out, try removing just the file named 0. Don&#146;t use <TT>rm *</TT> because if your version of <TT>rm</TT> doesn&#146;t stop at the file 0, you&#146;ll erase all the files in your directory.</P>

<P>A better way to protect yourself from accidentally deleting files is to use the <TT>-i</TT> flag with <TT>rm</TT>. The <TT>-i</TT> flag stands for <I>interactive</I>. If you give the command <TT>rm -i <I>filename</I></TT>, you&#146;re asked whether you really want to delete the file. You must answer yes before the file is actually deleted. If you type the command <TT><B>rm -i *</B></TT>, you must answer yes for every file in your directory. This should give you enough time to think about what you really want to do.</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>CAUTION:&nbsp;&nbsp;</B><BR>Think before you delete files. Unlike in Windows, DOS, or MAC, when you delete a file (in most versions of Linux), it&#146;s gone and the only way to recover a lost file is from a backup. You did make a backup, didn&#146;t you?<HR></FONT>

</BLOCKQUOTE>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR>&#149; <B>See</B> &#147;Performing Backups and Restoring Files,&#148; <B>p. 229</B><HR></FONT>

</BLOCKQUOTE>

<P>If you use the <TT>rm -i</TT> command frequently, you can implement it in two ways: by writing a shell script or by creating a shell function. If you write a shell script, remember that the shell searches for commands in the directories listed in your <TT>PATH</TT> variable in the order in which they&#146;re listed. If your $HOME/bin directory is listed last, a shell script named <TT>rm</TT> will never be found. You can place your $HOME/bin directory first in the <TT>PATH</TT> variable&#146;s list or create a new command, such as <TT>del</TT>. If you create a shell script called <TT>del</TT>, you must mark it as executable with the <TT>chmod</TT> command for the shell to recognize it. When you create your <TT>del</TT> command, you need to give it only one command: <TT>rm -i $*</TT>. If you then type the command <TT><B>del *</B></TT>, the shell translates it into <TT>rm -i *</TT>.</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR>&#149; <B>See</B> &#147;Editing and Aliasing Shell Commands,&#148; <B>p. 364</B><HR></FONT>

</BLOCKQUOTE>

<P>Another way to accomplish the same task is with an <I>alias</I>, which takes precedence over commands that must be looked up. You can think of an alias as an internal shell command (similar to the <TT>doskey</TT> commands introduced in MS-DOS version 5.0).</P>

<P>To add an alias if you&#146;re using the C shell, you must edit the file named .cshrc. You can use any text editor, such as <TT>vi</TT> (see Chapter 8, &#147;Using the <TT>vi</TT> Editor&#148;), to edit this file. For the C shell, add the following lines to the top of your .cshrc file:</P>

<!-- CODE SNIP //-->

<PRE>

rm ()

&#123;

/bin/rm -i $*

&#125;

</PRE>

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

<P>To add an alias to the Korn shell, add the following line to your $HOME/.kshrc file:

</P>

<!-- CODE SNIP //-->

<PRE>

alias rm &#145;rm -i $*&#146;

</PRE>

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

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="320-323.html">Previous</A></TD>

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

<TD><A HREF="325-327.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 + -