VMS-to-Unix Phrase Book
<=  Return           

2.3  Matching Multiple Files Using Wildcards

Problem

You want to interactively select groups of files to be processed based on file naming conventions.

Solution

Use the shell's File Name Substitution characters, which are similar to those used in DCL.

Discussion

The 'wildcard' pattern matching characters in Unix are very similar to those used in VMS, but there are distinct and sometimes subtle differences. This can be a problem because you many not match the files you intended or match other files that you did not want to include.

First, you need to be aware of some important differences between VMS filenaming and Unix:

With these differences in mind, consider the following pattern matching characters:

DCL sh Action
* * Match any string, including the null string.
% ? Matches any one character.
  [...] Matches any one of the characters enclosed in brackets. The hyphen may be used to indicate ranges of characters such as [0-9] for any digit and [a-z] for any lowercase letter.
  [!...] Match any character other than those that follow the exclamation point within the brackets.

Do not confuse the shell's special pattern matching characters with 'regular expression' pattern matching in programs like grep, sed, and perl. For example, in a regular expression, * matches zero or more of the preceeding character. It does not by itself match characters the way as it would when used in a shell expression.

Again, because the period is not a special character (except when it is first) it will be matched by the ? and * wildcards. Given the following existing filenames in the current directory:

    .foo-bar
    foo-bar
    foo.bar
    foo.foo
    foosbar
    very.foo.bar
consider the following example:

unix> ls foo?bar
foo-bar  foo.bar  foosbar
unix> ls foo*
foo-bar  foo.bar  foo.foo  foosbar
unix> ls *foo*
foo-bar       foo.bar       foo.foo       foosbar       very.foo.bar
unix> ls .*foo*
.foo-bar
unix>

Here is an example of using the [...] pattern matching construct:

unix> ls file.~[0-9]~
file.~1~  file.~3~  file.~5~  file.~7~  file.~9~
file.~2~  file.~4~  file.~6~  file.~8~
unix> ls file.~[0-9][0-9]~
file.~10~
unix> ls file.~[0-9]*~
file.~101~  file.~1~    file.~3~    file.~5~    file.~7~    file.~9~
file.~10~   file.~2~    file.~4~    file.~6~    file.~8~
unix>

As an exercise for the reader consider the following given the files:

    myfile
    myfile.1
    myfile.2
    myfile.x
why does the last command in the following example not match the file myfile?

unix> ls myfile*
myfile    myfile.1  myfile.2  myfile.x
unix> ls myfile*[!x]
myfile.1  myfile.2
unix>

One last important note -- always remember that it is the shell that expands the wildcard characters before starting up the program. Use the echo command to easily illustrate this.

unix> echo myfile*
myfile    myfile.1  myfile.2  myfile.x
unix>
The application only sees wildcards if you protect them with 'strong quotes' or if the wildcard ends up matching nothing. Again, using echo to illustrate:

unix> echo no*such*file
no*such*file
unix>

See Also

2.4 - Finding Files;
Chapter 2 of Unix for OpenVMS Users ;
Chapter 1 of Unix Power Tools .


<=  Return           

Colophon:
Best Viewed With Any Browser
This page maintained by:
    Bill.Costa@unh.edu
    of the Enterprise Computing Group
    in the dept of Computing & Information Sevices
    at the University of New Hampshire

Typographical
Conventions

Created:  31-Jan-2001 BC
Revised:  27-Mar-2001 BC