218-221.html

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

HTML
187
字号
<HTML>

<HEAD>

<TITLE>Linux Configuration and Installation:Basic Linux Tools</TITLE>

<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!--ISBN=1558285660//-->

<!--TITLE=Linux Configuration and Installation//-->

<!--AUTHOR=Patrick Volkerding//-->

<!--AUTHOR=Kevin Reichard//-->

<!--AUTHOR=Eric Foster//-->

<!--PUBLISHER=IDG Books Worldwide, Inc.//-->

<!--IMPRINT=M & T Books//-->

<!--CHAPTER=4//-->

<!--PAGES=218-221//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="215-218.html">Previous</A></TD>

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

<TD><A HREF="221-225.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H3><A NAME="Heading16"></A><FONT COLOR="#000077">Wildcards</FONT></H3>

<P>Like UNIX (and DOS, for that matter), Linux supports wildcards in command lines and shells scripts. A <I>wildcard</I> is merely shorthand for a character or a string of characters. Wildcards can come in handy if you&#146;re looking for a file and you&#146;ve forgotten the specific filename (geez, I <I>know</I> the file ends in <I>1996</I>), or if you want to see a list of files that fall within specific parameters (such as ending with <I>.c</I>, useful if you plan on using Linux for software development).</P>

<P>There are three types of Linux wildcards: <B>*</B>, <B>?</B>, and [<B>...</B>]. Each will be explained.</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>NOTE:&nbsp;&nbsp;</B>Technically, wildcards are the province of the shell, and in theory a discussion of wildcards should take place with a discussion of shells. For our purposes, however, we&#146;re going to discuss wildcards at this point in the Linux discussion, because what we&#146;re saying here applies to all shells.<HR></FONT>

</BLOCKQUOTE>

<P>In the previous section covering the <B>ls</B> command, we covered the command&#146;s use when it&#146;s applied to single files. However, there may be times when you want to list a set of files that share a common characteristic, such as ending with <I>.c</I>. In this instance, you can tell <B>ls</B> to look for every file that ends with <I>.c</I>, using the following command line:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~$ ls *.c

     aardvark.c     stuff.c     titles.c     xylophone.c

</PRE>

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

<P>In this instance, <B>ls</B> is told to substitute <B>*</B> for any portion of a filename preceding an ending of <I>.c</I>. And, as you can see from the list of files, the command was successful. The <B>ls</B> is used to match any number of characters in a string, including zero characters:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~$ ls titles*

     titles     titles.c

</PRE>

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

<P>In the case of <I>titles</I>, the wildcard matched zero characters.</P>

<P>The asterisk (<B>*</B>) can be used at the beginning or end of a wildcard expression. You can also use more than one asterisk in an expression:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~$ ls t*.*

     titles.c

</PRE>

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

<P>If you wanted to list the files with the string <I>titles</I> anywhere in the filename, you could use the following command line:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~$ ls *titles*

     subtitles     titles     titles.c

</PRE>

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

<P>The asterisk wildcard is the most expansive wildcard available. On the other end of the spectrum is the question-mark wildcard, which is used to match a single character:

</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~$ ls title?

     titles

</PRE>

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

<P>In this instance, <B>ls</B> did not match <B>titles.c</B>, which contains <I>two</I> characters after the search string of <I>title</I>. <B>Titles</B>, meanwhile, contained only one character after the search string of <I>title</I>, which matched the parameters of the <B>ls</B> command.</P>

<P>The final wildcard is used to return specific characters, as defined by brackets (<B>[]</B>). For example, you&#146;re looking through a directory filled with memos from the last 12 months. Since you&#146;ve been a good Linux user, you&#146;ve been placing a number at the end of every file, signifying the month it was written. (Yes, we know you&#146;re not likely to have too many files if you&#146;ve just installed Linux. Think of this advice as something you&#146;ll need in the future.) You want to track down a memo you wrote sometime in the summer, but you can&#146;t remember the name of the file, and a reading through the directory listings don&#146;t spark a memory. In this instance, you&#146;ll want to narrow down the directory listings to files ending in <I>6</I>, <I>7</I>, or <I>8</I> (corresponding to June, July, and August). To do this with the <B>ls</B> command, you&#146;d enter <B>6</B>&#150;<B>8</B> in brackets:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~/memos$ ls *[6-8]

     golf.8        golfanne.8     golfpat.6     golfjim.6

     golftod.6     golftom.7

</PRE>

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

<P>This narrows down the list of files returned by <B>ls</B>. It also means you probably play too much golf.</P>

<P>In the preceding example, we asked <B>ls</B> to return files that ended with a range of characters, i.e., in <I>6</I>, <I>7</I>, or <I>8</I>. You can also use this wildcard to return a single character:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~/memos$ ls *[6]

     golfpat.6     golfjim.6     golftod.6

</PRE>

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

<P>If you&#146;re searching for a character (remembering, of course, that Linux distinguishes between uppercase and lowercase letters at all times) or range of characters, you can list them in the brackets:

</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~/memos$ ls report.[Ee]rc

     report.Erc     report.erc

</PRE>

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

<P>Wildcards can be used with any Linux command.

</P>

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

<P>The <B>mkdir</B> command is used to create directories. If you plan on using Linux for most of your day-to-day stuff, we advise creating directories to help organize the many files Linux (and any other version of UNIX, for that matter) creates. Using <B>mkdir</B> is simple:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~$ mkdir <I>directory</I>

</PRE>

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

<P>where <I>directory</I> is the name of the directory you want to create. To create a directory named <B>letters</B> in your home directory, you&#146;d use the following command:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~$ mkdir letters

</PRE>

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

<P>To see if the directory was really created, you can use the <B>ls</B> command:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~$ ls

     letters/     text

</PRE>

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

<P>You can also use it to create a new directory elsewhere in the directory hierarchy:

</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~$ mkdir /users/kevin/letters

     gilbert:~$ ls /users/kevin

     letters/

</PRE>

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

<P><B>Mkdir</B> can create more than one directory on a command line:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~$ mkdir letters data

     gilbert:~$ ls

     data/          letters/     text

</PRE>

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

<P><B>Mkdir</B> can also create a directory and a subdirectory in a single command line:</P>

<!-- CODE SNIP //-->

<PRE>

     gilbert:~$ mkdir -p /letters/eric

</PRE>

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

<P>Other options to <B>mkdir</B> are listed in Table 4.6.</P>

<CENTER>

<TABLE WIDTH="95%"><CAPTION><B>Table 4.6</B> Other Options to the Mkdir Command

<TR>

<TH WIDTH="30%" ALIGN="LEFT">Option

<TH WIDTH="70%" ALIGN="LEFT">Result

<TR>

<TH COLSPAN="2"><HR>

<TR>

<TD>m <I>mode</I>

<TD>Sets the mode for the new directory.

<TR>

<TD COLSPAN="2"><HR>

</TABLE>

</CENTER>

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="215-218.html">Previous</A></TD>

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

<TD><A HREF="221-225.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





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

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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