156-158.html
来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 142 行
HTML
142 行
<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=156-158//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="153-156.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="158-161.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>Someone else on the system might also have a directory called <TT><B>novel</B></TT> in his or her home directory. Perhaps it even contains a file called <TT><B>chapter_1</B></TT>. In this case, you can’t distinguish the two files by using the relative filename <TT><B>novel/chapter_1</B></TT>. However, the absolute filenames <I>will</I> be different—for instance, <TT><B>/home/fido/novel/chapter_1</B></TT> as opposed to <TT><B>/home/mary/novel/chapter_1</B></TT>. The <TT><B>novel</B></TT> subdirectory in <TT><B>/home/fido</B></TT> is <I>not</I> the same subdirectory as the <TT><B>novel</B></TT> directory in <TT><B>/home/mary</B></TT>! The two are in quite separate locations and only coincidentally do they share the same name. The two files will have completely different contents, too.</P>
<H4 ALIGN="LEFT"><A NAME="Heading13"></A><FONT COLOR="#000077">Going Places: The cd Command</FONT></H4>
<P>The <TT><B>cd</B></TT> (change directory) command lets you change your working directory. You can think of it as moving to another directory. If you’ve worked with DOS or the DOS prompt in Windows, you’ve seen this command before (yes, it was swiped from UNIX!).</P>
<P>The syntax of the <TT><B>cd</B></TT> command is</P>
<!-- CODE SNIP //-->
<PRE>
cd <<I>directory</I>>
</PRE>
<!-- END CODE SNIP //-->
<P>There must be a space between <TT><B>cd</B></TT> and the directory specification. You should specify only one directory name to be changed into. The directory specification can be an absolute or relative one. For instance, type <TT><B>cd ..</B></TT> followed by <TT><B>pwd:</B></TT></P>
<!-- CODE //-->
<PRE>
darkstar:~$ <B>pwd</B>
darkstar:/home$ <B>/home/fido</B>
darkstar:~$ <B>cd ..</B>
darkstar:/home$ <B>pwd</B>
/home
darkstar:/home$ <B>cd ..</B>
darkstar:/$ <B>pwd</B>
/
darkstar:/$ <B>cd ..</B>
darkstar:/$ <B>pwd</B>
/
</PRE>
<!-- END CODE //-->
<P>As you can see in the preceding example, we started in <TT><B>/home/fido</B></TT> (that’s the absolute path name) and then moved up one directory level with the <B>..</B> command. That put us in <TT><B>/home</B></TT>. Another move to the parent and we’re in the root directory. We can’t go any higher than the root directory because there is no parent directory for the root directory. Typing <TT><B>cd ..</B></TT> when in the root directory simply leaves you in the root directory.</P>
<P>Note that the Linux command prompt usually shows you which directory you are currently in, so you don’t have to type <TT><B>pwd</B></TT> all the time. (We’ll continue to use <TT><B>pwd</B></TT> for clarity.) Not all Linux systems do show your current directory in the shell prompt because the system administrator may have customized the prompt for you.</P>
<P>Let’s suppose you want to go into a subdirectory of your home directory. We can <B>cd</B> back to your home directory and then <TT><B>cd</B></TT> into a subdirectory called <TT><B>book</B>:</TT></P>
<!-- CODE SNIP //-->
<PRE>
darkstar:/$ <B>cd /home/fido</B>
darkstar:~$ <B>pwd</B>
darkstar:/home$ <B>/home/fido</B>
darkstar:~$ <B>cd book</B>
darkstar:~/book$ <B>pwd</B>
/home/fido/book
</PRE>
<!-- END CODE SNIP //-->
<P>In this case, we used <TT><B>cd</B></TT> to get back <TT><B>home</B></TT> (verified with the </TT><B>pwd</B></TT> command), then told Linux to make the subdirectory book our current directory. We know the directory book is below our home directory, so we used relative filenames to move into it. We could have specified the absolute pathname, too, but this was much easier. To avoid any confusion or mistakes, use the absolute directory names when using the <TT><B>cd</B></TT> command:</P>
<!-- CODE SNIP //-->
<PRE>
darkstar:/$ cd /usr/bin
darkstar:/usr/bin$ pwd
/usr/bin
</PRE>
<!-- END CODE SNIP //-->
<P>When you type an absolute directory name, you go to that directory, no matter where you started from. When you type <TT><B>cd ..</B></TT>, where you end up depends on where you started.</P>
<P>To see the effect of changing your working directory, type <TT><B>ls</B></TT>. The list of files is so long that the first part scrolls off your screen. The <TT><B>ls</B></TT> command shows you the contents of your current directory (as always), but now your current directory is <TT><B>/usr/bin</B></TT>, which contains many more files than your home directory.</P>
<H4 ALIGN="LEFT"><A NAME="Heading14"></A><FONT COLOR="#000077">There’s No Place Like Home</FONT></H4>
<P>Here’s a handy trick that many UNIX and Linux users don’t use. Type </TT><B>cd</B></TT> without any directory specification:</P>
<!-- CODE SNIP //-->
<PRE>
darkstar:/usr/bin$ <B>cd</B>
darkstar:~$ <B>pwd</B>
/home/fido
</PRE>
<!-- END CODE SNIP //-->
<P>Typing <TT><B>cd</B></TT> by itself always returns you to your home directory, no matter where you are when you type <TT><B>cd</B></TT>. When exploring the file system, you sometimes wind up deep in a blind alley of subdirectories. Type <TT><B>cd</B></TT> to quickly return home or type <B>cd /</B> to return to the root directory.</P>
<P>The <B>~</B> in your prompt is another special character. It stands for your home directory. There’s no reason to type <TT><B>cd ~</B></TT> when <TT><B>cd</B></TT> works just as well, and is much easier to type! A tilde (~) by itself indicates your own home directory.</P>
<P>Linux also uses the ~ symbol to mean the parent directory of user directories. When you type <TT><B>cd</B>~<<I>user</I>></TT>, you move to that user’s home directory. This is a very useful trick, especially on large systems with many users and more complicated directory structures than the simple <TT>/<B>home</B>/<<I>user</I>></TT> on your Linux system.</P>
<P>When you’re changing to a distant directory, it’s often a good idea to take several steps. If you mistype a very long directory specification, you will have to retype the entire specification. Sometimes it may not even be clear why <TT><B>cd</B></TT> gave you an error! Taking a number of shorter steps means less retyping in case of an error. Consider this example:</P>
<!-- CODE SNIP //-->
<PRE>
darkstar:~$ cd /usr/docs/faq/unix
bash: /usr/docs/faq/unix: No such file or directory
</PRE>
<!-- END CODE SNIP //-->
<P>You’re pretty sure that this path is correct. Let’s change directories one step at a time:
</P>
<!-- CODE SNIP //-->
<PRE>
darkstar:~$ cd /usr
darkstar:/usr$ cd docs
bash: docs: No such file or directory
</PRE>
<!-- END CODE SNIP //-->
<P>There’s a problem with <TT><B>docs</B></TT>. The directory is actually named <TT><B>doc</B>:</TT></P>
<!-- CODE SNIP //-->
<PRE>
darkstar:/usr$ <B>ls
</B>bin/ doc/ games/ info/ man/ sbin/ spool/
darkstar:/usr$ <B>cd doc</B>
darkstar:/usr/doc$ <B>cd faq/unix</B>
darkstar:/usr/doc/faq/unix$ <B>pwd/usr/doc/faq/unix</B>
</PRE>
<!-- END CODE SNIP //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="153-156.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="158-161.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?