📄 0066-0068.html
字号:
<HTML>
<HEAD>
<TITLE>Sams Teach Yourself Linux in 24 Hours:Manipulation and Searching Commands:EarthWeb Inc.-</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=0672311623 //-->
<!-- TITLE=Sams Teach Yourself Linux in 24 Hours//-->
<!-- AUTHOR=Bill Ball//-->
<!-- PUBLISHER=Macmillan Computer Publishing//-->
<!-- IMPRINT=Sams//-->
<!-- CHAPTER=05 //-->
<!-- PAGES=0063-0082 //-->
<!-- UNASSIGNED1 //-->
<!-- UNASSIGNED2 //-->
<P><CENTER>
<a href="0063-0065.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0069-0071.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-66"><P>Page 66</P></A>
<P>
directory. This is a safe, but not fail-safe, approach to deleting or recovering files. You
may also be able to use the mc command, or Midnight Commander, discussed later in
this chapter.
</P>
<H4><A NAME="ch05_ 7">
Creating Directories with the mkdir Command
</A></H4>
<P>The mkdir command can create one or several directories with a single command line.
You may also be surprised to know that mkdir can also create a whole hierarchy of
directories, which includes parent and children, with a single command line.
</P>
<P>This command is one of the basic tools (along with
cp and mv) you'll use to organize your information. Now, take a look at some examples. The following simple command
line creates a single directory:
</P>
<!-- CODE SNIP //-->
<PRE>
# mkdir temp
</PRE>
<!-- END CODE SNIP //-->
<P>But you can also create multiple directories with
</P>
<!-- CODE SNIP //-->
<PRE>
# mkdir temp2 temp3 temp4
</PRE>
<!-- END CODE SNIP //-->
<P>You'd think that you could also type the following to make a directory named
child under temp:
</P>
<!-- CODE SNIP //-->
<PRE>
# mkdir temp/child
</PRE>
<!-- END CODE SNIP //-->
<P>And you can, because the temp directory exists (you just created it). But,
suppose you type
</P>
<!-- CODE SNIP //-->
<PRE>
# mkdir temp5/child
mkdir: cannot make directory `temp5/child': No such file or directory
</PRE>
<!-- END CODE SNIP //-->
<P>As you can see, mkdir complained because the
temp5 directory did not exist. To build a hierarchy of directories with
mkdir, you must use the -p, or parent option, for example:
</P>
<!-- CODE //-->
<PRE>
# mkdir -p temp5/parent/child
# tree temp5
temp5
`-- parent
`-- child
2 directories, 0 files
</PRE>
<!-- END CODE //-->
<P>As you can see, mkdir created not only the
temp5 directory, but also a subdirectory called
parent, and a subdirectory under parent called
child.
</P>
<P>Now that you know how to create directories, take a look at how to remove them.
</P>
<H4><A NAME="ch05_ 8">
Removing Directories with the rmdir Command
</A></H4>
<A NAME="PAGENUM-67"><P>Page 67</P></A>
<P>The rmdir command is used to remove directories. To remove a directory, all you have
to do is type
</P>
<!-- CODE SNIP //-->
<PRE>
# rmdir tempdirectory
</PRE>
<!-- END CODE SNIP //-->
<P>But there's a catch: the directory must be empty first! If you try to delete a directory
with any files, you'll get an error message like this:
</P>
<!-- CODE SNIP //-->
<PRE>
# rmdir temp5
rmdir: temp5: Directory not empty
</PRE>
<!-- END CODE SNIP //-->
<P>In this example, temp5 also contains other directories. The
rmdir command would also complain if a directory contains only files and not directories. You can use the
rm command to remove the files first (remember to be careful if you use the
-fr option), or you can move the files somewhere else, or rename the directory, with the
mv command, discussed next.
</P>
<P>The rmdir command, like mkdir, also has a -p, or parent, option. You can use this
option to remove directory hierarchies, for example:
</P>
<!-- CODE SNIP //-->
<PRE>
# rmdir -p temp5
rmdir: temp5: Directory not empty
</PRE>
<!-- END CODE SNIP //-->
<P>Hmm. That didn't work! How about
</P>
<!-- CODE SNIP //-->
<PRE>
# rmdir -p temp5/parent
rmdir: temp5/parent: Directory not empty
</PRE>
<!-- END CODE SNIP //-->
<P>Hey! That didn't work either. Now try
</P>
<!-- CODE SNIP //-->
<PRE>
# rmdir -p temp5/*
rmdir: temp5/parent: Directory not empty
</PRE>
<!-- END CODE SNIP //-->
<P>This is getting frustrating! Try it one more time:
</P>
<!-- CODE SNIP //-->
<PRE>
# rmdir -p temp5/parent/child
</PRE>
<!-- END CODE SNIP //-->
<P>Finally! As you can see, you must specify the complete directory tree to delete it. If
you use the same command line, but without the -p option, only the child directory would
be deleted. But what if there are two or more subdirectories, for example:
</P>
<!-- CODE //-->
<PRE>
# mkdir -p temp5/parent/child
# mkdir temp5/parent/child2
# tree temp5
temp5
`-- parent
|-- child
`-- child2
3 directories, 0 files
</PRE>
<!-- END CODE //-->
<P>In order to delete the entire directory system of
temp5, you'd need to use
</P>
<A NAME="PAGENUM-68"><P>Page 68</P></A>
<!-- CODE SNIP //-->
<PRE>
# rmdir temp5/parent/*
</PRE>
<!-- END CODE SNIP //-->
<P>So far, you've seen how to create and remove directories. Next, you'll learn about the
mv command, which you can use to move or rename files and directories.
</P>
<H4><A NAME="ch05_ 9">
Renaming Files with the mv Command
</A></H4>
<P>The mv command, called a rename command but known to many as a move
command, will indeed rename files or directories, but it will also move them around your file system.
</P>
<P>Actually, in the technical sense, the files or directories are not really moved. If you
insist on knowing all the gory details, read the Linux System Administrator's
Guide, available through
</P>
<!-- CODE SNIP //-->
<PRE>
<a href="http://sunsite.unc.edu/ldp/ldp/sag/index.html">
http://sunsite.unc.edu/LDP/LDP/sag/index.html</A>
</PRE>
<!-- END CODE SNIP //-->
<P>In its simplest form, mv can rename files, for example:
</P>
<!-- CODE SNIP //-->
<PRE>
# touch file1
# mv file1 file2
</PRE>
<!-- END CODE SNIP //-->
<P>This command renames file1 to file2. However, besides renaming files,
mv can rename directories, whether empty or not, for example:
</P>
<!-- CODE SNIP //-->
<PRE>
# mkdir -p temp/temp2/temp3
# mv temp newtemp
</PRE>
<!-- END CODE SNIP //-->
<P>Although mv has nine different options, this section concentrates on
the two most commonly used. These options, -b and
-i, allow you to use mv in a fairly safe way,
because mv will not only rename, but overwrite silently and quickly! The first option,
-b, creates a backup of any file or directory you rename to an existing name, for example:
</P>
<!-- CODE SNIP //-->
<PRE>
# touch file1 file2 file3
# ls file*
file1 file2 file3
# mv file1 file2
# ls file*
file1 file2
</PRE>
<!-- END CODE SNIP //-->
<P>As you can see, without using -b, mv not only renamed
file1 to file2, but deleted file2 in the process. Is this dangerous? You bet! Now, try the
-b option:
</P>
<!-- CODE SNIP //-->
<PRE>
# touch file1
# ls file*
file1 file2 file3
# mv -b file1 file2
# ls file*
file2 file2~ file3
</PRE>
<!-- END CODE SNIP //-->
<P>This example shows that although file1 has been renamed, replacing
file2, a backup of file2 with a default extension of the tilde
(~) has been created.
</P>
<P><CENTER>
<a href="0063-0065.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0069-0071.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -