-->
Previous Table of Contents Next


The * Wild Card

The asterisk (*) is the most universal wild card used. It simply means any and all characters. For example, the string a* 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 *xx*.gif 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.

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


lp *.txt

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


lp *txt

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 lp (the file named txt in your directory is passed twice to lp):


lp * txt

In the last example, the lp command first prints the files represented by the *; that is, it prints all files. The lp 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—in effect, as a comma in an English command). The lp command processes txt as the name of the next file it is to print.

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


ls *rep*

Linux lists files with names such as frep.data, report, and janrep. There’s one exception: Files with names starting with a period aren’t listed. To list files with names starting with a period (often called hidden files), 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:


ls .*rep*


CAUTION:  
Be careful of using the asterisk wild card when you’re deleting or removing files. The command rm * 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 rm *txt (which would remove all files with names ending in txt), you enter rm * txt, Linux first deletes all files and then attempts to delete a file named txt. But at that point, no files are left.

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


The ? Wild Card

Use the question mark (?) 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 lp rep* command prints all the files, but to print just the first two (report1 and reportb), enter this:


lp report?

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


ls ??x

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

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’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:


ls *.???


NOTE:  Remember that Linux isn’t MS-DOS; filenames aren’t limited to eight characters with a three-character extension. Also remember that filenames are case-sensitive under Linux.

The [] Expression

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’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.

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:


ls [A-Z]*

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:


cp sales.9[0-2] oldstuff

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–Z], which represent any character between uppercase A and uppercase Z, inclusive.

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–Z,a–z]. In the ASCII character set, there are special characters between ASCII Z and ASCII a; if you specified [A–z], you include those special characters in your request.

Connecting Processes with Pipes

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 (|).

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


sort allsales | lp

The name pipe 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:


cat sales* | sort | lp


Previous Table of Contents Next