📄 unx03.htm
字号:
<HTML>
<HEAD>
<TITLE>UNIX Unleashed unx03.htm</TITLE>
<LINK REL="ToC" HREF="index.htm">
<LINK REL="Next" HREF="unx04.htm">
<LINK REL="Previous" HREF="unx02.htm"></HEAD>
<BODY TEXT="#000000" LINK="#0000FF" VLINK="#800080" bgcolor=white>
<P><A HREF="unx02.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>
<A HREF="index.htm"><IMG SRC="blutoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>
<A HREF="unx04.htm"><IMG SRC="blunext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>
<A HREF="index.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Home"></A>
</P><UL>
<LI>
<A HREF="#I1">3 — The UNIX File System Go Climb a Tree</A></LI>
<UL>
<UL>
<UL>
<UL>
<LI>
<A HREF="#I3">By Jeff Smith</A></LI></UL></UL>
<LI>
<A HREF="#I4">The Types of UNIX Files</A></LI>
<LI>
<A HREF="#I5">Creating Listing, and Viewing Files</A></LI>
<LI>
<A HREF="#I6">The UNIX File Tree</A></LI>
<LI>
<A HREF="#I7">File and Directory Names</A></LI>
<LI>
<A HREF="#I8">Creating Directories with mkdir</A></LI>
<LI>
<A HREF="#I9">Working with Files</A></LI>
<UL>
<LI>
<A HREF="#I10">Copying Files with cp</A></LI>
<LI>
<A HREF="#I11">Moving Files with mv</A></LI>
<LI>
<A HREF="#I12">Removing Files with rm</A></LI></UL>
<LI>
<A HREF="#I13">Working with Directories</A></LI>
<UL>
<LI>
<A HREF="#I14">Creating Multiple Directories with mkdir</A></LI>
<LI>
<A HREF="#I15">Removing a Directory with rmdir</A></LI>
<LI>
<A HREF="#I16">Renaming Directories with mv</A></LI>
<LI>
<A HREF="#I17">Keeping Secrets — File and Directory Permissions</A></LI></UL>
<LI>
<A HREF="#I18">Default File and Directory Permissions—Your umask</A></LI>
<LI>
<A HREF="#I19">Hard and Symbolic Links</A></LI>
<LI>
<A HREF="#I20">Summary</A></LI></UL></UL></UL>
<H1 ALIGN="CENTER">
<CENTER><A ID="I1" NAME="I1">
<BR>
<FONT SIZE=5><A ID="I2" NAME="I2"></A><B>3 — The UNIX File System Go Climb a Tree</B>
<BR></FONT></A></CENTER></H1>
<H5 ALIGN="CENTER">
<CENTER><A ID="I3" NAME="I3">
<FONT SIZE=3><B>By Jeff Smith</B>
<BR></FONT></A></CENTER></H5>
<P>When you work with UNIX, one way or another you spend most of your time working with files. In this chapter, you learn how to create and remove files, copy and rename them, create links to them, and use directories to organize your files so that you can
find them later. You also learn how to view your files, list their names and sizes, and move around in the UNIX file tree. Finally, this chapter shows how you can choose to share or restrict the information in your files.
<BR></P>
<P>One of UNIX's greatest strengths is the consistent way in which it treats files. Although some operating systems use different types of files that each require unique handling, you can handle most UNIX files the same. For instance, the cat command,
which displays a disk file on your terminal screen, can also send the file to the printer. As far as cat (and UNIX) are concerned, the printer and your terminal look the same, and they look like any other UNIX file. UNIX also doesn't distinguish between
files that you create and the standard files that come with the operating system—as far as UNIX is concerned, a file is a file is a file. This consistency makes it easy to work with files because you don't have to learn special commands for every new
task. Often, as in the cat example, you can use the same command for several purposes. This makes it easy to write UNIX programs because you usually don't have to worry whether you're talking to a terminal, a printer, or an ordinary file on a disk drive.
<BR></P>
<H3 ALIGN="CENTER">
<CENTER><A ID="I4" NAME="I4">
<FONT SIZE=4><B>The Types of UNIX Files</B>
<BR></FONT></A></CENTER></H3>
<P>There are three types of UNIX files: regular files, directories, and device files. Regular files hold executable programs and data. Executable programs are the commands (such as cat) that you enter. Data is information that you store for later use. Such
information can be virtually anything: a USENET news article with a promising-looking recipe for linguini, a book that you are writing, a homework assignment, or a saved spreadsheet.
<BR></P>
<P>Directories are files that contain other files and subdirectories, just as a filing cabinet's drawers hold related folders. Directories help you organize your information by keeping closely related files in the same place so you can find them later. For
instance, you might save all your spreadsheets in a single directory instead of mixing them with your linguini recipes and book chapters.
<BR></P>
<P>As in the cat example, files can also refer to computer hardware such as terminals and printers. These device files can also refer to tape and disk drives, CD-ROM players, modems, network interfaces, scanners, and any other piece of computer hardware.
Under UNIX, even the computer's memory is a file.
<BR></P>
<P>Although UNIX treats all files similarly, some require slightly unique treatment. For example, UNIX treats directories specially in some ways. Also, because they refer directly to the computer's hardware, device files sometimes must be treated
differently from ordinary files. For instance, most files have a definite size in bytes—the number of characters they contain. Your terminal's keyboard is a device file, but how many characters does it hold? The question of file size doesn't make
sense in this case. Despite these differences, UNIX commands usually don't distinguish among the various types of files.
<BR></P>
<H3 ALIGN="CENTER">
<CENTER><A ID="I5" NAME="I5">
<FONT SIZE=4><B>Creating Listing, and Viewing Files</B>
<BR></FONT></A></CENTER></H3>
<P>You can create files in many ways, even if you don't yet know how to use a text editor. One of the easiest ways is to use the touch command, as follows:
<BR></P>
<PRE>$ touch myfile</PRE>
<P>This command creates an empty filenamed myfile.
<BR></P>
<P>An empty file isn't much good except as a place holder that you can fill in later. If you want to create a file that contains some text, you can use either the echo or cat command. The echo command is a simple but useful command that prints its
command-line arguments to stdout, the standard output file, which by default is your terminal screen. For instance, enter the following:
<BR></P>
<PRE>$ echo Will Rogers
Will Rogers</PRE>
<P>The words Will Rogers are echoed to your terminal screen.
<BR></P>
<P>You can save the words by using your shell's file redirection capability to redirect echo's standard output to a different file:
<BR></P>
<PRE>$ echo Will Rogers > cowboys</PRE>
<P>Notice that the preceding command does not send output to your terminal; the greater-than sign tells your shell to redirect echo's output into cowboys.
<BR></P>
<P>You can view the contents of cowboys with cat, as follows:
<BR></P>
<PRE>$ cat cowboys
Will Rogers</PRE>
<P>If you want to add more text to a file, use two greater-than signs:
<BR></P>
<PRE>$ echo Roy Rogers >> cowboys</PRE>
<P>Now cat shows both lines:
<BR></P>
<PRE>$ cat cowboys
Will Rogers
Roy Rogers</PRE>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="caution.gif" WIDTH = 37 HEIGHT = 35><B>CAUTION: </B>When you use the greater-than sign to create a file, your shell creates a zero-length file (just as touch does) and then fills it. If the file already exists, your shell first destroys its
contents to make it zero-length. You must use two greater-than signs to append new text to a file or you will destroy your earlier work.
<BR></NOTE>
<HR ALIGN=CENTER>
<P>The cat command doesn't just display files. It also can create them by using shell redirection. If you plan to enter several lines of text, cat is more convenient than echo:
<BR></P>
<PRE>$ cat > prufrock
Let us go then, you and I,
When the evening is spread out against the sky
Like a patient etherised upon a table;</PRE>
<P>Then press Ctrl+D. This keystroke is the default end-of-file character; it tells cat that you are done typing.
<BR></P>
<P>Now you have a filenamed prufrock, and you can view it by using the cat command:
<BR></P>
<PRE>$ cat prufrock
Let us go then, you and I,
When the evening is spread out against the sky
Like a patient etherised upon a table;</PRE>
<P>Note that cat does not print the end-of-file character when you display the file.
<BR></P>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> When you create a file with cat, you can use your character-erase, word-erase, and line-kill characters (see Chapter 7, "Text Editing with vi, EMACS, and sed Files") to correct typing
mistakes in the current line. After you press Enter, you cannot make corrections. To correct such a mistake, you must learn to use a text editor (see Chapter 7).
<BR></NOTE>
<HR ALIGN=CENTER>
<P>It may seem odd that cat both creates and displays files, but this is normal for UNIX; that is, it's normal for commands not to know one type of file from another. The name cat derives from the word catenate, which means to connect in a series or to
link together. The cat command doesn't care which file it receives as input or where the output goes. Because UNIX handles your terminal keyboard and screen as ordinary files, when you enter cat cowboys, cat catenates cowboys to your terminal screen, and
when you enter cat > prufrock, the command catenates what you enter into a disk file. You can even run cat without specifying an input or output file:
<BR></P>
<PRE>$ <B>cat</B>
Let us go then, you and I,
Let us go then, you and I,
When the evening is spread out against the sky
When the evening is spread out against the sky</PRE>
<P>Press Ctrl+D to insert an end-of-file.
<BR></P>
<P>The cat command echos to your screen every line that you enter before Ctrl+D because, by default, cat uses your terminal keyboard as its input file and your screen as its output file. Like other UNIX commands, cat treats files quite consistently and
therefore is very flexible.
<BR></P>
<P>The cat command works well for short files that fit on a single terminal screen, but if you try to display a longer file, all but the last lines of it scroll off your screen. To view long files, you can temporarily freeze your terminal screen by typing
Ctrl+S and restart it by typing Ctrl+Q. However, if your terminal is fast, you may not be able to stop it quickly enough. Pagers like pg and more pause after every screen. (See Chapter 4, "Listing Files.")
<BR></P>
<P>Now that you have some files, you may want to list them or view their names. The ls (list files) command can display each file's name, size, and time of creation, and also which users have permission to view, modify, and remove them.
<BR></P>
<P>If you want to know only the names of the files, enter the following:
<BR></P>
<PRE>$ <B>ls</B>
cowboys prufrock</PRE>
<P>If you have many files, you may want to view only some of them. If you want ls to list specific files, you can specify their names on the command line:
<BR></P>
<PRE>$ ls prufrock
prufrock</PRE>
<P>This output isn't very useful; you already know the name of the file, so there's not much point in listing it. However, you can use ls in this way to find out whether a certain file exists. If the file doesn't exist, ls prints an error message, as
follows:
<BR></P>
<PRE>$ ls alfred_j
alfred_j: No such file or directory</PRE>
<P>The message No such file or directory means exactly what it says: You don't have a filenamed alfred_j.
<BR></P>
<P>A better application of this feature of ls is to use your shell's metacharacters or wild cards to list a file when you know only part of its name. (For more information on metacharacters and wild cards, see Chapter 11, "Bourne Shell," Chapter
12, "Korn Shell," and Chapter 13, "C Shell.") With shell wild cards, you can specify parts of filenames and let your shell fill in the rest. Suppose that you can't remember the name of the file that includes the linguini recipe, but you
remember that it starts with the letter l. You could enter <B>ls</B> and then search through a list of all your files to find the one that you want. However, the following command makes the search easier:
<BR></P>
<PRE>$ ls l*
linguini local_lore</PRE>
<P>The l* argument narrows your listing by telling ls that you're interested only in files that begin with an l, followed by zero or more of any other characters. The ls command ignores the files cowboys and prufrock, and lists only those files beginning
with the letter l.
<BR></P>
<P>Wild cards are a powerful method for narrowing your file listings. Throughout this chapter, you'll see many uses for wild cards. Because they are a characteristic of your shell and not the commands you invoke from your shell, wild cards work equally
well with other commands, such as cat. For instance, you could enter the following command to display both your linguini recipe and the file local_lore:
<BR></P>
<PRE>$ cat l*</PRE>
<P>However, different shells may use different wild cards, or use the same ones in different ways. This chapter provides examples only of the wild cards that are common to all shells. To learn how your shell uses wild cards, see Chapters 12 ("Korn
Shell") and 13 ("C Shell") and your shell's manual page.
<BR></P>
<H3 ALIGN="CENTER">
<CENTER><A ID="I6" NAME="I6">
<FONT SIZE=4><B>The UNIX File Tree</B>
<BR></FONT></A></CENTER></H3>
<P>As mentioned in the introduction to this chapter, your personal files usually contain data—information that you want the computer to save when you're not logged in. If you use UNIX for a long time, you'll accumulate hundreds or even thousands of
files, and thousands more system files that are a standard part of UNIX. How can you keep all these files organized and find the ones that you want when you need them?
<BR></P>
<P>The designers of UNIX solved this problem by using directories to organize the UNIX file system into a structure that is shaped like an upside-down tree. Directories enable you to keep related files in one place, where you see them only when you
want—after all, you needn't clutter your file listings with recipes when you're working with a spreadsheet.
<BR></P>
<P>Figure 3.1 shows part of the file tree for a typical UNIX system. In this drawing, which looks somewhat like an upside-down tree, names like home and jane are followed by a slash (/), which indicates that they are directories, or files of files. Note
that ordinary files, such as cowboys and prufrock, are not followed by a slash. Such files are called leaves because they aren't connected to anything else. The connecting lines are the paths through the UNIX file tree. You can move around the tree by
following the paths.
<BR></P>
<P>
<BR><B><A HREF="03unx01.gif">Figure 3.1. The file tree for a typical UNIX </B><B>system.</A></B>
<BR></P>
<P>Notice also that two files are named prufrock. How can two files have the same name? And when you enter <B>cat prufrock</B>, how does UNIX know which one you want? Don't worry—your shell can distinguish one prufrock file from the other, for two
reasons.
<BR></P>
<P>First, UNIX shells always remember their current working directory (CWD). The CWD is the directory in the file tree that you're in at any particular time. If you move somewhere else in the tree, the CWD changes. For example, if you're in the directory
jane and you enter <B>cat </B><B>prufrock</B>, you see the prufrock file that is attached to that directory; if you're in the tmp directory, you see the file attached to that directory.
<BR></P>
<P>Second, although so far you have named files by using relative pathnames, UNIX translates these pathnames into fully qualified pathnames. Fully qualified pathnames (or full pathnames) begin with a slash. Every file in the file tree has a unique, fully
qualified pathname, which you construct by following the connecting lines from the root to the file. For instance, the following is the fully qualified pathname of the file prufrock in the directory jane:
<BR></P>
<PRE>/home/jane/prufrock</PRE>
<P>To construct this unique name, you follow the path from the root directory (/) through the directories home and jane, and end with the file prufrock. UNIX uses the slash to separate the different parts of the pathname. This character is also the special
name for the root of the tree. Because it has this special meaning, the slash is one of the few characters that you cannot use in a UNIX filename.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -