📄 unx03.htm
字号:
<BR></P>
<P>For the file prufrock in the directory tmp, the fully qualified pathname is the following:
<BR></P>
<PRE>/tmp/prufrock</PRE>
<P>You construct this pathname the same way that you constructed that of the prufrock file in the jane directory. This time, you climbed down the file tree from the root directory to the directory tmp and then to the file prufrock, adding slash characters
to separate the directories. Even though both files end in the name prufrock, UNIX can tell them apart because each has a unique pathname.
<BR></P>
<P>Relative pathnames begin with something other than the slash character. Using relative pathnames is usually convenient when specifying files that are in your CWD—for example, cat prufrock. But what if you want to refer to a file that is not in your
CWD? Suppose that your CWD is /home/jane and you want to look at the file /tmp/prufrock. You can do this in two ways. First, you can enter the following command:
<BR></P>
<PRE>$ cat /tmp/prufrock</PRE>
<P>This command tells your shell unambiguously which file you want to see.
<BR></P>
<P>Secondly, you can tell your shell to move through the file tree to /tmp and then use a relative pathname. For example, if your shell's command to change your CWD is cd, you would enter the following:
<BR></P>
<PRE>$ cd /tmp</PRE>
<P>(Note that, unlike cat and ls, cd is "silent" when it succeeds. Most UNIX commands print nothing when all goes well.) Now your CWD is the directory /tmp. If you enter <B>cat prufrock</B>, you see the contents of the file /tmp/prufrock rather
than /home/jane/prufrock.
<BR></P>
<P>As noted earlier, the name / has special significance to UNIX because it separates the components of pathnames and is the name of the file tree's root directory. For convenience, every UNIX directory also has two special names: . (dot) and .. (dot-dot).
By convention, ls doesn't show these filenames because they begin with dot, but you can use the -a option to list these files, as follows:
<BR></P>
<PRE>$ ls -a
. .. cowboys prufrock</PRE>
<P>Dot is a synonym for the CWD, and dot-dot for the CWD's parent directory. If your CWD is /home/jane and you want to move to /home, you can enter either
<BR></P>
<PRE>$ cd /home</PRE>
<P>or
<BR></P>
<PRE>$ cd ..</PRE>
<P>The result of both commands is the same. When you enter <B>cd /home</B>, your shell begins with the root directory and moves down one level to /home. When you enter <B>cd ..</B>, your shell starts in /home/jane and moves up one level; if you enter the
command again, you move up to the parent directory of /home, which is /. If you enter the command once again, where do you go—hyperspace? Don't worry. Because / doesn't have a parent directory, its dot-dot entry points back on itself, so your CWD is
still /. In the UNIX file system, the root directory is the only directory whose dot-dot entry points to itself.
<BR></P>
<P>Along with your CWD, your shell also remembers your home directory. This is the directory in which you automatically begin when you first log in. You spend most of your time in the home directory, because it is the directory in which you keep your
files. If you get lost climbing around the file tree with cd, you can always return to your home directory by typing the cd command without any arguments:
<BR></P>
<PRE>$ <B>cd</B></PRE>
<P>Your home directory looks like any other directory to UNIX—only your shell considers the home directory to be special.
<BR></P>
<P>Now that you are familiar with the cd command, you can move around the file tree. If you forget where you are, you can use the pwd (print working directory) command to find out. This command doesn't take any command-line arguments. The following example
demonstrates how to use cd and pwd to move around the file tree and keep track of where you are:
<BR></P>
<PRE>$ <B>pwd</B>
/home/jane
$ cd /tmp
$ <B>pwd</B>
/tmp
$ <B>cd</B>
$ <B>pwd</B>
/home/jane</PRE>
<P>Of course, while you're moving around, you might also want to use ls and cat to list and view files. Moving around the file tree to view the standard system files distributed with UNIX is a good way to learn more about the UNIX file tree.
<BR></P>
<H3 ALIGN="CENTER">
<CENTER><A ID="I7" NAME="I7">
<FONT SIZE=4><B>File and Directory Names</B>
<BR></FONT></A></CENTER></H3>
<P>Unlike some operating systems, UNIX gives you great flexibility in how you name files and directories. As previously mentioned, you cannot use the slash character because it is the pathname separator and the name of the file tree's root directory .
However, almost everything else is legal. Filenames can contain alphabetic (both upper- and lowercase), numeric, and punctuation characters, control characters, shell wild-card characters (such as *), and even spaces, tabs, and newlines. However, just
because you can do something doesn't mean you should. Your life will be much simpler if you stick with upper- and lowercase alphabetics, digits, and punctuation characters such as ., -, and _.
<BR></P>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="caution.gif" WIDTH = 37 HEIGHT = 35><B>CAUTION: </B>Using shell wild-card characters such as * in filenames can cause problems. Your shell expands such wild-card characters to match other files. Suppose that you create a filenamed * that you want
to display with cat, and you still have your files cowboys and prufrock. You might think that the command cat * will do the trick, but remember that * is a shell wild card that matches anything. Your shell expands * to match the files *, cowboys, and
prufrock, so cat displays all three. You can avoid this problem by quoting the asterisk with a backslash:
<BR>
<BR>$ cat \*
<BR>
<BR>Quoting, which is explained in detail in Chapters 12 ("Korn Shell") and 13 ("C Shell"), temporarily removes a wild card's special meaning and prevents your shell from expanding it. However, having to quote wild cards is
inconvenient, so you should avoid using such special characters in filenames.
<BR>
<BR>You also should avoid using a hyphen or plus sign as the first character of a filename, because command options begin with those characters. Suppose that you name a file -X and name another unix_lore. If you enter <B>cat *</B>, your shell expands the *
wild card and runs cat as follows:
<BR>
<BR>$ cat -X unix_lore
<BR>
<BR>The cat command interprets -X as an option string rather than a filename. Because cat doesn't have a -X option, the preceding command results in an error message. But if cat did have a -X option, the result might be even worse, as the command might do
something completely unexpected. For these reasons, you should avoid using hyphens at the beginning of filenames.
<BR></NOTE>
<HR ALIGN=CENTER>
<P>Filenames can be as long as 255 characters in System V Release 4 UNIX. Unlike DOS, which uses the dot character to separate the filename from a three character suffix, UNIX does not attach any intrinsic significance to dot—a filenamed
lots.of.italian.recipes is as legal as lots-of-italian-recipes. However, most UNIX users follow the dot-suffix conventions listed in Table 3.1. Some language compilers like cc require that their input files follow these conventions, so the table labels
these conventions as "Required" in the last column.
<BR></P>
<UL>
<LH><B>Table 3.1. File suffix conventions.</B>
<BR></LH></UL>
<TABLE BORDER>
<TR>
<TD>
<PRE><I>Suffix</I>
<BR></PRE>
<TD>
<PRE><I>Program</I>
<BR></PRE>
<TD>
<PRE><I>Example</I>
<BR></PRE>
<TD>
<PRE><I>Required</I>
<BR></PRE>
<TR>
<TD>
<P>.c</P>
<TD>
<P>C program files</P>
<TD>
<P>ls.c</P>
<TD>
<P>Yes</P>
<TR>
<TD>
<P>.f</P>
<TD>
<P>FORTRAN program files</P>
<TD>
<P>math.f</P>
<TD>
<P>Yes</P>
<TR>
<TD>
<P>.pl</P>
<TD>
<P>Perl program files</P>
<TD>
<P>hose.pl</P>
<TD>
<P>No</P>
<TR>
<TD>
<P>.h</P>
<TD>
<P>include files</P>
<TD>
<P>term.h</P>
<TD>
<P>No</P>
<TR>
<TD>
<P>.d, .dir</P>
<TD>
<P>The file is a directory</P>
<TD>
<P>recipes.d,</P>
<TD>
<P>No</P>
<TR>
<TD>
<P><BR></P>
<TD>
<P><BR></P>
<TD>
<P>recipes.dir</P>
<TD>
<P>No</P>
<TR>
<TD>
<P>.gz</P>
<TD>
<P>A file compressed with the</P>
<TD>
<P>foo.gz</P>
<TD>
<P>Yes</P>
<TR>
<TD>
<P><BR></P>
<TD>
<P>GNV project's gzip</P>
<TD>
<P><BR></P>
<TD>
<P><BR></P>
<TR>
<TD>
<P>.Z</P>
<TD>
<P>A compressed file</P>
<TD>
<P>term.h.Z</P>
<TD>
<P>Yes</P>
<TR>
<TD>
<P>.zip</P>
<TD>
<P>A file compressed with PKZIP</P>
<TD>
<P>book.zip</P>
<TD>
<P>Yes</P></TABLE>
<P>Choosing good filenames is harder than it looks. Although long names may seem appealing at first, you may change your mind after you enter cat lots-of-italian-recipes a few times. Of course, shell wild cards can help (as in cat lots-of*), but as you
gain experience, you'll find that you prefer shorter names.
<BR></P>
<P>Organizing your files into directories with well-chosen names can help. For instance, Figure 3.2 shows how Joe organizes his recipes.
<BR></P>
<P>
<BR><B><A HREF="03unx02.gif">Figure 3.2. Organizing your files within a </B><B>directory.</A></B>
<BR></P>
<P>Joe could have put all his recipes in a single directory, but chose to use the directories italian, french, and creole to separate and categorize the recipes. Instead of using filenames like recipe-italian-linguini, he can use cd to move to the
directory recipes and then move to the subdirectory italian; then Joe can use ls and cat to examine only the files that he wants to see. You may think that Joe is carrying this organizing a bit too far (after all, he has only four recipes to organize), but
he's planning for that happy day when he's collected several thousand.
<BR></P>
<P>Similarly, if you keep a journal, you might be tempted to put the files in a directory named journal and use filenames like Dec_93 and Jan_94. This approach isn't bad, but if you intend to keep your journal for ten years, you might want to plan ahead by
removing the year from the filename and making it a directory in the pathname, as shown in Figure 3.3.
<BR></P>
<P>
<BR><B><A HREF="03unx03.gif">Figure 3.3. Organizing your file for a </B><B><I>journal</I></B><B> </B><B>directory.</A></B>
<BR></P>
<P>By using this approach, you can work with just the files for a particular year. To get to that year, however, you have to use one more cd command. Thus you must consider the trade-off between having to enter long filenames and having to climb around the
file tree with cd to find your files. You should experiment until you find your own compromise between too-long filenames and too many levels of directories.
<BR></P>
<H3 ALIGN="CENTER">
<CENTER><A ID="I8" NAME="I8">
<FONT SIZE=4><B>Creating Directories with </B><B><I>mkdir</I></B>
<BR></FONT></A></CENTER></H3>
<P>Now that you know the advantages of organizing your files into directories, you'll want to create some. The mkdir (make directory) command is one of the simplest UNIX commands. To create a single directory named journal, enter the following:
<BR></P>
<PRE>$ mkdir journal</PRE>
<P>(Like cd, mkdir prints no output when it works.) To make a subdirectory of journal named 94, enter the following:
<BR></P>
<PRE>$ mkdir journal/94</PRE>
<P>Or if you prefer, you can enter the following:
<BR></P>
<PRE>$ mkdir journal
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -