161-164.html

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

HTML
147
字号
<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=161-164//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

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

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

<TD><A HREF="164-166.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H4 ALIGN="LEFT"><A NAME="Heading17"></A><FONT COLOR="#000077">Creating Directories</FONT></H4>

<P>To create a new directory, use the <TT><B>mkdir</B></TT> command. The syntax is <TT><B>mkdir</B>&lt;<I>name</I>&gt;</TT>, where &lt;<I>name</I>&gt; is replaced by whatever you want the directory to be called. This creates a subdirectory with the specified name in your current directory:</P>

<!-- CODE SNIP //-->

<PRE>

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

anotherfile    newfile   thirdfile

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

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

anotherfile  /newdir    newfile    thirdfile

</PRE>

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

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Tip:&nbsp;&nbsp;</B><BR>The <TT>mkdir</TT> command is already familiar to you if you have used MS-DOS systems. In MS-DOS, you can abbreviate <TT>mkdir</TT> as <TT>md</TT>. You might think that <TT>md</TT> would work in Linux, because, after all, most of the commands we&#146;ve seen have extremely concise names. However, Linux doesn&#146;t recognize <TT>md</TT>; it insists on the full <TT>mkdir</TT>.

<P>If you frequently switch between Linux and MS-DOS, you may want to use <TT>mkdir</TT> for both systems. However, be warned that you could start typing other Linux commands in MS-DOS&#151;for example, typing <TT>ls</TT> instead of <TT>dir</TT>!<HR></FONT>

</BLOCKQUOTE>

</P>

<P>The <TT><B>mkdir</B></TT> command creates an entry for that subdirectory in Linux&#146;s table of all files and directories, which is called the I-node table. When Linux creates a directory name, it doesn&#146;t actually write anything to the disk drive because a directory name has no physical contents until you save a file to it. Directories are used by Linux as a convenience for the user.</P>

<P>You can use both relative and absolute pathnames with <TT><B>mkdir</B></TT>, for example:</P>

<!-- CODE //-->

<PRE>

&#36; pwd

/usr/tparker/temp

&#36; ls

&#36; mkdir book1

&#36; ls

book1

&#36; mkdir /usr/tparker/temp/book2

&#36; ls

book1

book2

</PRE>

<!-- END CODE //-->

<P>In the first case we&#146;ve used relative names to create a directory called <TT><B>book1</B></TT> under the current directory. In the second example, we&#146;ve used absolute addressing to create <TT><B>book2</B></TT> in the same location. We can use whichever format is more convenient. Both methods accomplish the same result.</P>

<H4 ALIGN="LEFT"><A NAME="Heading18"></A><FONT COLOR="#000077">Moving and Copying Files</FONT></H4>

<P>You often need to move or copy files. The <TT><B>mv</B></TT> command moves files (which is the same as renaming them) and the <TT><B>cp</B></TT> command copies files. The syntax for the two commands is similar:</P>

<!-- CODE SNIP //-->

<PRE>

mv &lt;<I>source</I>&gt; &lt;<I>destination</I>&gt;

cp &lt;<I>source</I>&gt; &lt;<I>destination</I>&gt;

</PRE>

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

<P>As you can see, <TT><B>mv</B></TT> and <TT><B>cp</B></TT> are very simple commands. Here&#146;s an example:</P>

<!-- CODE SNIP //-->

<PRE>

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

anotherfile  /newdir         newfile    thirdfile

darkstar:~&#36; <B>mv anotherfile movedfile</B>

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

movedfile  /newdir         newfile    thirdfile

darkstar:~&#36; <B>cp thirdfile xyz</B>

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

anotherfile  /newdir    newfile    thirdfile    xyz

</PRE>

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

<P>You can use <TT><B>cat</B></TT> (or <TT><B>more</B></TT> or <TT><B>less</B></TT>) at any time to verify that <TT><B>anotherfile</B></TT> became <TT><B>movedfile</B></TT> and that the contents of file <B>xyz</B> are identical to the contents of <TT><B>thirdfile</B></TT>.</P>

<P>It can get more confusing if you&#146;re moving or copying files from one directory to another. This is because a file&#146;s <I>real</I> name includes its absolute path&#151;for instance, <TT><B>/home/fido/newfile</B></TT>. However, Linux lets you leave off parts of the file&#146;s name because it&#146;s more convenient to refer to <TT><B>newfile</B></TT> rather than <TT><B>/home/fido/newfile</B></TT>.</P>

<P>For example, suppose you want to move <TT><B>newfile</B></TT> from your current directory into the <TT><B>newdir</B></TT> subdirectory. If you want the file to keep the same name, you can type</P>

<!-- CODE SNIP //-->

<PRE>

darkstar:~&#36; <B>mv newfile newdir/newfile</B>

</PRE>

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

<P>However, it&#146;s much more common to type

</P>

<!-- CODE SNIP //-->

<PRE>

darkstar:~&#36; <B>mv newfile newdir</B>

</PRE>

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

<P>primarily because it&#146;s a little shorter. In this case, because you have typed a directory name for the destination, Linux assumes that you want the file to be placed in the specified directory with the same name as the existing file.

</P>

<P>You can also use <TT><B>cd</B></TT> to change to the directory you want to move the file to:</P>

<!-- CODE SNIP //-->

<PRE>

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

darkstar:~newdir&#36; <B>copy ../newfile .</B>

</PRE>

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

<P>This example is a bit less intuitive than the first two. You specify that the source is <TT><B>../newfile</B></TT> which means &#147;the file <TT><B>newfile</B></TT> in the current directory&#146;s parent directory.&#148; The destination you simply specify as &#147;.&#148;, which is short for &#147;the current directory.&#148; In other words, you&#146;re telling <TT><B>mv</B></TT> to &#147;go up one level, grab <TT><B>newfile</B></TT>, and move it to right here.&#148; Because this is less intuitive, you might find yourself automatically <I>pushing</I> a file from your current directory to another directory rather than <I>pulling</I> a file from another directory into your current directory.</P>

<P>You can also change the name of the file while moving or copying it to another directory by specifying the new name you want. The following is just one possible way:</P>

<!-- CODE SNIP //-->

<PRE>

darkstar:~&#36; <B>cp newfile newdir/anothername</B>

</PRE>

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

<P>This creates a copy of <TT><B>newfile</B></TT> in the directory <TT><B>newdir</B></TT> and names the copied file <TT><B>anothername</B></TT>.</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Warning:&nbsp;&nbsp;</B><BR>When moving or copying files between directories, you should always double-check that the file&#146;s destination directory exists and verify the directory&#146;s name. Otherwise, the results of your command can be unexpected, as the following two examples show.<P>If in the example just shown, let&#146;s say you mistyped <TT>newdir</TT> as <TT>mv newfile mewdir</TT>&#151;you wind up with a file called <TT>mewdir</TT> in your current directory and no file <TT>newfile</TT> in the <TT>newdir</TT> subdirectory!</P>

<P>Another way you get an unexpected result is to type <TT>cp newfile newdir</TT> if you didn&#146;t realize that the directory <TT>newdir</TT> already existed. In this case, you are expecting to create an identical file called <TT>newdir</TT> in your current directory. What you are actually doing is creating a copy of <TT>newfile</TT>, called <TT>newfile</TT>, in the subdirectory <TT>newdir</TT>.<HR></FONT>

</BLOCKQUOTE>

</P>

<P>The <TT><B>mv</B></TT> command is much more efficient than the <TT><B>cp</B></TT> command. When you use <TT><B>mv</B></TT>, the file&#146;s contents are not moved at all; rather, Linux makes a note that the file is to be found elsewhere within the file system&#146;s structure of directories.</P><P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

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

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

<TD><A HREF="164-166.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





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

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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