📄 unx04.htm
字号:
-rw-r—r— 1 marsha adept 117 Jul 19 1991 .profile
-rwxr-xr— 1 asm adept 512 Dec 14 16:16 21x
-rw-rw-r— 1 marsha adept 1024 Jan 20 14:14 LINES.dat
-rw-rw-r— 1 marsha adept 3072 Jan 20 14:14 LINES.idx
-rw-rw-r— 1 marsha adept 256 Jan 20 14:14 PAGES.dat
-rw-rw-r— 1 marsha adept 3072 Jan 20 14:14 PAGES.idx
-rw-rw-r— 1 marsha acct 240 May 5 1992 acct.pds
-rw-rw-r— 1 marsha adept 1024 Nov 22 15:42 marsha.pds
-rwxrwxr— 4 root sys 243072 Aug 22 1991 p11
-rwxrwxr— 4 root sys 256041 Aug 22 1991 t11
drw-rw-r— 1 marsha adept 3072 Oct 12 11:42 users</PRE>
<H4 ALIGN="CENTER">
<CENTER><A ID="I7" NAME="I7">
<FONT SIZE=3><B>Showing File Types with </B><B><I>-F</I></B>
<BR></FONT></A></CENTER></H4>
<P>Another useful option is -F, which distinguishes directory and executable files from ordinary files. The -F option causes a slash (/) to be appended to the filename for directories and an asterisk (*) to be appended to files which are executable. For
example,
<BR></P>
<PRE>$ ls -F
21x* LINES.dat LINES.idx PAGES.dat PAGES.idx
acct.pds marsha.pds p11* t11* users/</PRE>
<H4 ALIGN="CENTER">
<CENTER><A ID="I8" NAME="I8">
<FONT SIZE=3><B>Listing Files Whose Names Contain Nonprintable Characters with </B><B><I>-q</I></B>
<BR></FONT></A></CENTER></H4>
<P>When a file is created, the filename can inadvertently acquire nonprintable characters. Suppose that a filename contained a backspace character (represented here as ^H). The file named abcd^Hefg would display in a normal ls command as abcefg. Because
you cannot see the backspace character, you might be confused about the actual filename. With the ls -q option, this filename would display as abcd?efg.
<BR></P>
<P>Even if you don't know what the mystery character is, you can still work with the file by using filename substitution (discussed in the next section). If you need to know the exact nature of the mystery character, you can use the -b option, which causes
the nonprintable character to print in octal mode. With the b option, the filename would display as abcd\010efg, in which \010 is the octal representation of a backspace.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I9" NAME="I9">
<FONT SIZE=3><B>Other Useful </B><B><I>ls</I></B><B> Options</B>
<BR></FONT></A></CENTER></H4>
<P>Additional ls options include the following:
<BR></P>
<TABLE BORDER>
<TR>
<TD>
<P>-u</P>
<TD>
<P>Used with -l, causes the last access time stamp to be displayed instead of the last modification time.</P>
<TR>
<TD>
<P>-s</P>
<TD>
<P>Used with -l, gives the file size in blocks instead of bytes.</P>
<TR>
<TD>
<P>-t</P>
<TD>
<P>Sorts the output by time stamp instead of name. Used with -u sorts the output by access time.</P>
<TR>
<TD>
<P>-r</P>
<TD>
<P>Reverses the order of the output. By itself, displays the output in reverse alphabetic order, used with -t, displays the output by the most recent time stamp.</P>
<TR>
<TD>
<P>-x</P>
<TD>
<P>Forces the output into multicolumn</P></TABLE>
<H3 ALIGN="CENTER">
<CENTER><A ID="I10" NAME="I10">
<FONT SIZE=4><B>Using Metacharacters When Referring to Filenames</B>
<BR></FONT></A></CENTER></H3>
<P>So far you've learned how to work with files by referring to their complete names. Sometimes, however, it is useful to refer to several files without having to name each one of them. Likewise, if you can remember only part of a filename, it is useful to
list all the files whose names contain that part. UNIX provides metacharacters, also known as wildcards, which enable you to refer to files in these ways.
<BR></P>
<P>There are two metacharacters: the question mark (?) and the asterisk (*). In addition to metacharacters, filename substitution can be done on character sets. For more information about metacharacters, see Chapter 11, "Bourne Shell," Chapter
12, "Korn Shell," and Chapter 13, "C Shell."
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I11" NAME="I11">
<FONT SIZE=3><B>Pattern Matching on a Single Character</B>
<BR></FONT></A></CENTER></H4>
<P>In filename substitution, the question mark (?) stands for any single character. Consider the following directory:
<BR></P>
<PRE>$<B>ls</B>
21x LINES.dat LINES.idx PAGES.dat PAGES.idx
acct.pds marsha.pds p10 p101 p11
t11 z11</PRE>
<P>You can use the question mark (?) in any position. For example,
<BR></P>
<PRE>$ ls ?11
p11 t11 z11</PRE>
<P>You can also use more than one question mark in a single substitution. For example,
<BR></P>
<PRE>$ ls p??
p10 p11</PRE>
<P>The following command gives you all three-character filenames:
<BR></P>
<PRE>$ ls ???
21x p10 p11 t11 z11</PRE>
<P>Suppose that you wanted to list all of the files that begin with LINES. We could do this successfully with
<BR></P>
<PRE>$ ls LINES.???
LINES.dat LINES.idx</PRE>
<P>Now suppose that you wanted to find the files that end in .pds. The following two commands illustrate how to do this:
<BR></P>
<PRE>$ ls ????.pds
acct.pds
$
$ ls ?????.pds
marsha.pds</PRE>
<H4 ALIGN="CENTER">
<CENTER><A ID="I12" NAME="I12">
<FONT SIZE=3><B>Pattern Matching on a Group of Characters</B>
<BR></FONT></A></CENTER></H4>
<P>In the previous example, to list all of the files ending in .pds using single character substitution, you would have to know exactly how many characters precede the period. To overcome this problem, you use the asterisk (*), which matches a character
string of any length, including a length of zero. Consider the following two examples:
<BR></P>
<PRE>$ ls *.pds
acct.pds marsha.pds
$ ls p10*
p10 p101</PRE>
<P>As with single character substitution, more than one asterisk (*) can be used in a single substitution. For example,
<BR></P>
<PRE>$ ls *.*
LINES.dat LINES.idx PAGES.dat PAGES.idx acct.pds
marsha.pds</PRE>
<H4 ALIGN="CENTER">
<CENTER><A ID="I13" NAME="I13">
<FONT SIZE=3><B>Pattern Matching on Character Sets</B>
<BR></FONT></A></CENTER></H4>
<P>You have seen how you can access a group of files whose names are similar. What do you do, though, if you need to be more specific? Another way to do filename substitution is by matching on character sets. A character set is any number of single
alphanumeric characters enclosed in square brackets—[ and ].
<BR></P>
<P>Suppose that you wanted a list of all the filenames that start with p or t followed by 11. You could use the following command:
<BR></P>
<PRE>$ ls [pt]11
p11 t11</PRE>
<P>You can combine character sets with the metacharacters. To list the names of all the files that begin with p or t, you could use
<BR></P>
<PRE>$ ls [pt]*
p10 p101 p11 t11</PRE>
<P>Now suppose that you wanted a list of all the filenames that begin with an uppercase alphabetic character. You could use
<BR></P>
<PRE>$ ls [ABCDEFGHIJKLMNOPQRSTUVWXYZ]*
LINES.dat LINES.idx PAGES.dat PAGES.idx</PRE>
<P>If you're guessing that there might be a better way to do this, you're right. When the characters in a character set substitution are in sequence, you can use a hyphen (-) to denote all of the characters in the sequence. Therefore, you can abbreviate
the previous command in this way:
<BR></P>
<PRE>$ ls [A-Z]*</PRE>
<P>If a character sequence is broken, you can still use the hyphen for the portion of the character set that is in sequence. For example, the following command lists all the three-character filenames that begin with p, q, r, s, t, and z:
<BR></P>
<PRE>$ ls [p-tz]??
p10 p11 t11 z11</PRE>
<H3 ALIGN="CENTER">
<CENTER><A ID="I14" NAME="I14">
<FONT SIZE=4><B>How File Substitution Works</B>
<BR></FONT></A></CENTER></H3>
<P>It is important to understand how file substitution actually works. In the previous examples, the ls command doesn't do the work of file substitution—the shell does. (Refer to Chapter 10, "What Is a Shell," for more information.) Even
though all the previous examples employ the ls command, any command that accepts filenames on the command line can use file substitution. In fact, using the simple echo command is a good way to experiment with file substitution without having to worry
about unexpected results. For example,
<BR></P>
<PRE>$ echo p*
p10 p101 p11</PRE>
<P>When a metacharacter is encountered in a UNIX command, the shell looks for patterns in filenames that match the metacharacter. When a match is found, the shell substitutes the actual filename in place of the string containing the metacharacter so that
the command sees only a list of valid filenames. If the shell finds no filenames that match the pattern, it passes an empty string to the command.
<BR></P>
<P>The shell can expand more than one pattern on a single line. Therefore, the shell interprets the command
<BR></P>
<PRE>$ ls LINES.* PAGES.*</PRE>
<P>as
<BR></P>
<PRE>$ ls LINES.dat LINES.idx PAGES.dat PAGES.idx</PRE>
<P>There are file substitution situations that you should be wary of. You should be careful about the use of whitespace (extra blanks) in a command line. If you enter the following command, for example, the results might surprise you:
<BR></P>
<PRE>$ ls LINES. *
LINES.: not found
21x LINES.dat LINES.idx PAGES.dat PAGES.idx
acct.pds marsha.pds p10 p101 p11
t11 z11</PRE>
<P>What has happened is that the shell interpreted the first parameter as the filename LINES. with no metacharacters and passed it directly on to ls. Next, the shell saw the single asterisk (*), and matched it to any character string, which matches every
file in the directory. This is not a big problem if you are simply listing the files, but it could mean disaster if you were using the command to delete data files!
<BR></P>
<P>Unusual results can also occur if you use the period (.) in a shell command. Suppose that you are using the
<BR></P>
<PRE>$ ls .*</PRE>
<P>command to view the hidden files. What the shell would see after it finishes interpreting the metacharacter is
<BR></P>
<PRE>$ ls . .. .profile</PRE>
<P>which gives you a complete directory listing of both the current and parent directories.
<BR></P>
<P>When you think about how filename substitution works, you might assume that the default form of the ls command is actually
<BR></P>
<PRE>$ ls *</PRE>
<P>However, in this case the shell passes to ls the names of directories, which causes ls to list all the files in the subdirectories. The actual form of the default ls command is
<BR></P>
<PRE>$ ls .</PRE>
<H3 ALIGN="CENTER">
<CENTER><A ID="I15" NAME="I15">
<FONT SIZE=4><B>The </B><B><I>find</I></B><B> Command</B>
<BR></FONT></A></CENTER></H3>
<P>One of the wonderful things about UNIX is its unlimited path names. A directory can have a subdirectory that itself has a subdirectory, and so on. This provides great flexibility in organizing your data.
<BR></P>
<P>Unlimited path names have a drawback, though. To perform any operation on a file that is not in your current working directory, you must have its complete path name. Disk files are a lot like flashlights: You store them in what seem to be perfectly
logical places, but when you need them again, you can't remember where you put them. Fortunately, UNIX has the find command.
<BR></P>
<P>The find command begins at a specified point on a directory tree and searches all lower branches for files that meet some criteria. Since find searches by path name, the search crosses file systems, including those residing on a network, unless you
specifically instruct it otherwise. Once it finds a file, find can perform operations on it.
<BR></P>
<P>Suppose you have a file named urgent.todo, but you cannot remember the directory where you stored it. You can use the find command to locate the file.
<BR></P>
<PRE>$ find / -name urgent.todo -print
/usr/home/stuff/urgent.todo</PRE>
<P>The syntax of the find command is a little different, but the remainder of this section should clear up any questions.
<BR></P>
<P>The find command is different from most UNIX commands in that each of the argument expressions following the beginning path name is considered a Boolean expression. At any given stop along a branch, the entire expression is true—file found—if
all of the expressions are true; or false—file not found—if any one of the expressions is false. In other words, a file is found only if all the search criteria are met. For example,
<BR></P>
<PRE>$ find /usr/home -user marsha -size +50</PRE>
<P>is true for every file beginning at /usr/home that is owned by Marsha and is larger than 50 blocks. It is not true for Marsha's files that are 50 or fewer blocks long, nor is it true for large files owned by someone else.
<BR></P>
<P>An important point to remember is that expressions are evaluated from left to right. Since the entire expression is false if any one expression is false, the program stops evaluating a file as soon as it fails to pass a test. In the previous example, a
file that is not owned by Marsha is not evaluated for its size. If the order of the expressions is reversed, each file is evaluated first for size, and then for ownership.
<BR></P>
<P>Another unusual thing about the find command is that it has no natural output. In the previous example, find dutifully searches all the paths and finds all of Marsha's large files, but it takes no action. For the find command to be useful, you must
specify an expression that causes an action to be taken. For example,
<BR></P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -