⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 353-355.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTML
字号:
<HTML>

<HEAD>

<TITLE>Special Edition Using Linux, Fourth Edition:Understanding Linux Shells</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=0789717468//-->

<!--TITLE=Special Edition Using Linux, Fourth Edition//-->

<!--AUTHOR=Jack Tackett//-->

<!--AUTHOR=Jr.//-->

<!--AUTHOR=Steve Burnett//-->

<!--PUBLISHER=Macmillan Computer Publishing//-->

<!--IMPRINT=Que//-->

<!--CHAPTER=18//-->

<!--PAGES=353-355//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="351-353.html">Previous</A></TD>

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

<TD><A HREF="355-357.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<P><FONT SIZE="+1"><B>The * Wild Card</B></FONT></P>

<P>The asterisk (*) is the most universal wild card used. It simply means any and all characters. For example, the string <TT>a*</TT> means all files beginning with a. You can use as many asterisks in a single expression as you need to define a set of files. For example, the expression <TT>*xx*.gif</TT> means any filename with the .gif extension that has xx anywhere in the rest of the name. Matches include the filenames abxx.gif, xxyyzz.gif, and xx.gif.</P>

<P>Use the asterisk character (*) to represent any sequence of characters. For example, to print all files in your current directory with names that end with .txt, enter</P>

<!-- CODE SNIP //-->

<PRE>

lp *.txt

</PRE>

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

<P>Pay attention when using the asterisk wild card. If you enter the following command, you print all files whose names end with txt:

</P>

<!-- CODE SNIP //-->

<PRE>

lp *txt

</PRE>

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

<P>The file named reportxt is included with the files printed with the second command but not with the first. If you enter the following command, the shell passes the name of every file in your directory, as well as the single file named txt, to the command <TT>lp</TT> (the file named txt in your directory is passed twice to <TT>lp</TT>):</P>

<!-- CODE SNIP //-->

<PRE>

lp * txt

</PRE>

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

<P>In the last example, the <TT>lp</TT> command first prints the files represented by the *; that is, it prints all files. The <TT>lp</TT> command then moves to the second item in the list of files it is to print (Linux interprets the space character between the * and txt as a delimiter&#151;in effect, as a comma in an English command). The <TT>lp</TT> command processes txt as the name of the next file it is to print.</P>

<P>The * symbol can be used anywhere in a string of characters. For example, if you want to use the <TT>ls</TT> command to list the names of all files in your current directory whose names contain the characters <BIG>rep</BIG>, enter this command:</P>

<!-- CODE SNIP //-->

<PRE>

ls *rep*

</PRE>

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

<P>Linux lists files with names such as frep.data, report, and janrep. There&#146;s one exception: Files with names starting with a period aren&#146;t listed. To list files with names starting with a period (often called <BIG>hidden files</BIG>), you must specify the leading period. For example, if you have a file named .reportrc and want to see it listed, enter the following variation of the preceding command:</P>

<!-- CODE SNIP //-->

<PRE>

ls .*rep*

</PRE>

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

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>CAUTION:&nbsp;&nbsp;</B><BR>Be careful of using the asterisk wild card when you&#146;re deleting or removing files. The command <TT>rm *</TT> removes all files in your directory. An all-too-common mistake is to accidentally delete all files when you mean to delete a collection of files with a common suffix or prefix. If, instead of <TT>rm *txt</TT> (which would remove all files with names ending in txt), you enter <TT>rm * txt</TT>, Linux first deletes all files and then attempts to delete a file named txt. But at that point, no files are left.

<P>To be safe, use the <TT>-i</TT> option with <TT>rm</TT> if you use the asterisk for filename completion. The <TT>rm -i *txt</TT> command prompts you for confirmation before each file is deleted.<HR></FONT>

</BLOCKQUOTE>

</P>

<P><FONT SIZE="+1"><B>The ? Wild Card</B></FONT></P>

<P>Use the question mark (<TT>?</TT>) wild card to represent a single character. Suppose that you have the files report1, reportb, report10, reportb3, report.dft, and report.fin in your current directory. You know that the <TT>lp rep*</TT> command prints all the files, but to print just the first two (report1 and reportb), enter this:</P>

<!-- CODE SNIP //-->

<PRE>

lp report?

</PRE>

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

<P>To list the names of all files whose names are three characters long and end with the character <TT>x</TT>, enter the following:</P>

<!-- CODE SNIP //-->

<PRE>

ls ??x

</PRE>

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

<P>This command lists a file with the name tax but not trax.

</P>

<P>Because the question mark represents a single occurrence of any character, the string ??? represents all files consisting of just three letters. You can generate a list of files with three-letter extensions with the string *.???. For example, if you&#146;re searching a directory containing graphic images as well as other data, the following command lists all files with extensions such as .tif, .jpg, and .gif, as well as any other files with three-letter extensions:</P>

<!-- CODE SNIP //-->

<PRE>

ls *.???

</PRE>

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

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>NOTE:&nbsp;&nbsp;</B>Remember that Linux isn&#146;t MS-DOS; filenames aren&#146;t limited to eight characters with a three-character extension. Also remember that filenames are case-sensitive under Linux.<HR></FONT>

</BLOCKQUOTE>

<P><FONT SIZE="+1"><B>The [] Expression</B></FONT></P>

<P>Sometimes you must be more selective than either of the more general-purpose wild cards allow. Suppose that you want to select the files job1, job2, and job3, but not jobx. You can&#146;t select the right files with the ? wild card because it represents one occurrence of any character. You can, however, use job[123] as the file descriptor.

</P>

<P>You can also represent a single character by enclosing a range of characters within a pair of square brackets. To list the names of all files that begin with an uppercase letter, enter the following:</P>

<!-- CODE SNIP //-->

<PRE>

ls [A-Z]*

</PRE>

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

<P>Suppose that you have files named sales.90, sales.91, sales.92, and sales.93 and want to copy the first three to a subdirectory named oldstuff. Assuming that the subdirectory oldstuff exists, you could enter this:

</P>

<!-- CODE SNIP //-->

<PRE>

cp sales.9[0-2] oldstuff

</PRE>

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

<P>Like the question mark, items inside square brackets ([]) represent exactly one character. You can describe a discrete series of permissible values, such as [123], which permits only the characters 1, 2, or 3; you can also describe a range of characters, as in [A&#150;Z], which represent any character between uppercase A and uppercase Z, inclusive.

</P>

<P>You can also specify a set of ranges, which incorporates more than one range. For example, if you want to specify only alphabetic characters, you can use [A&#150;Z,a&#150;z]. In the ASCII character set, there are special characters between ASCII Z and ASCII a; if you specified [A&#150;z], you include those special characters in your request.</P>

<H4 ALIGN="LEFT"><A NAME="Heading10"></A><FONT COLOR="#000077">Connecting Processes with Pipes</FONT></H4>

<P>Frequently, you need to use the output of one program or command as the input of another. Rather than enter each command separately and save results in intermediate files, you can connect a sequence of commands by using a pipe (|).

</P>

<P>For example, to sort a file named allsales and then print it, enter this:</P>

<!-- CODE SNIP //-->

<PRE>

sort allsales | lp

</PRE>

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

<P>The name <BIG>pipe</BIG> is appropriate. The output of the program on the left of the pipe (the vertical bar) is sent through the pipe and used as the input of the program on the right. You can connect several processes with pipes. For example, to print a sorted list of the data in all files with names that begin with sales, enter the following command:</P>

<!-- CODE SNIP //-->

<PRE>

cat sales* | sort | lp

</PRE>

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

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="351-353.html">Previous</A></TD>

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

<TD><A HREF="355-357.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





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

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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