Using Wildcards


The shell gives you a way to abbreviate filenames through the use of special patterns called wildcards. You can use wildcards to specify a whole set of files at once, or to search for a file when you know only part of its name. The most commonly used wildcard is the *, which you encountered in Chapter 3. The asterisk matches a string of any length (including a string with zero characters). For example,

*html

Matches any filenames ending in html, including html and index.html.

note*

Matches any filenames beginning in note, such as note:8.28.

*kili*

Matches any filename containing the string “kili” anywhere in the name.

The standard UNIX System shell provides two other filename wildcards: ? and []. The question mark matches any single character. For example,

email?

-Matches any filename consisting of “email” followed by exactly one character, including email1 but not email or email.1.

Brackets match any one of a set of characters that they enclose. For example,

[Jj]mf

Matches either of the filenames Jmf or jmf.

You can indicate a range or sequence of characters in brackets with a -. For example,

output[a-c]

Matches outputa, outputb, and outputc, but not outputd.

The range includes all characters in the ASCII character sequence from the first to the last. For example,

[A-N]

Matches any of the uppercase characters between A and N.

[a-z]

Matches any lowercase character.

[A-z]

Includes all upper- and lowercase characters.

[0–9]

Includes all digits.

You can use more than one of these wildcards at a time. For example,

[Rr]esearch*

Matches any filename that starts with Research or research.

The wildcard characters are the same for all the shells (sh, csh, bash, etc).

The shell’s use of wildcards to match filenames is somewhat similar to the regular expressions used by many UNIX System commands, including ed, grep, and awk. Wildcards are not the same as regular expressions, however. (The meaning of the * character in particular is different.) Regular expressions are discussed at several points later in this book, including the section on grep in Chapter 19.

Wildcards and Hidden Files

There is one important exception to the statement that * matches any sequence of characters. It does not match a . (dot) at the beginning of a filename. As discussed in Chapter 3, files with names beginning with . are treated as hidden files. They are used to hold information that is needed by the system or by particular commands, but that you are not usually interested in seeing.

To match a filename beginning with . (dot), you have to include a dot in the pattern. The following command will display the files profile and old_profile but will not print out your .profile:

 $ cat *profile

If you want to view your .profile, the following command will work:

 $ cat .pro*

How Wildcards Work

When the shell processes a command line, it replaces any word containing filename wildcards with the matching filenames, in sorted order. For example, suppose your current directory contains files named note1.tmp, note2.tmp, and note3.tmp. If you want to remove all of these, you could type the following short command:

 $ rm *.tmp

Before rm is run, the shell replaces *.tmp with all of the matching filenames: note1.tmp, note2.tmp, and note3.tmp. The shell then passes these arguments to rm exactly as if you had typed them.

If no filename matches the pattern you specify, the shell makes no substitution. Instead, it passes the wildcard to the command as if it were part of a regular filename. So if you type

 $ cat *.bk

and there is no file ending in .bk, the cat command will look for a (nonexistent) file named *.bk. When it doesn’t find one, it will give you an error message, as follows:

 cat: cannot open *.bk

Whether you get an error message when this happens, and if so, what it says, depends on the command you are running. If you used the same wildcard with the command vi, the result would be the creation of a new file with the name *.bk. Although this doesn’t produce an error message, it is probably not what you wanted.

As noted in Chapter 3, the power of the * wildcard can cause problems if you are not careful in using it. If you try to enter the command

 $ rm temp*

but accidentally type

 $ rm temp *

you will remove all of the visible files in the current directory




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net