Next | Prev | Up | Top | Contents | Index

Using Regular Expressions and Metacharacters

There are shortcuts available to you when you wish to define large numbers of files or directories in your commands. These shortcuts are known as "regular expressions." Regular expressions are made up of a combination of alpha-numeric characters and a series of punctuation characters that have special meaning to the IRIX shells. These punctuation characters are called metacharacters when they are used for their special meanings with shell commands.

These shortcuts are useful because they minimize keystrokes. While minimizing keystrokes may seem to be a minor concern at first glance, an administrator who issues lengthy and complex command lines repeatedly may find these shortcuts a handy and necessary time-saving feature.

The following is a list of the IRIX metacharacters:

IRIX Metacharacters
MetacharacterMeaning
*wildcard
?single character wildcard
[]set definition marks

The asterisk (*) metacharacter is a universal wildcard. This means that the shell interprets the character to mean any and all files. For example, the command:

cat *

tells the shell to concatenate all the files in a directory, in alphabetical order by filename. The command:

rm *

tells the shell to remove everything in the directory. Only files will be removed, though, since a different command, rmdir(1) is used to remove directories. However, the asterisk character does not always have to refer to whole files. It can be used to denote parts of files as well. For example, the command:

rm *.old

will remove all files with the suffix.old on their names.

The single character wildcard is a question mark (?). This metacharacter is used to denote a wildcard character in one position. For example, if your directory contains the following files:

file1

file2

file3

file.different

and you wish to remove file1, file2, and file3, but not file.different, you would use the command:

rm file?

If you used an asterisk in place of the question mark, all your files would be removed, but since the question mark is a wildcard for a single space, your desired file is not chosen.

Square brackets denote members of a set. For example, consider the list of files used in the example of the single character wildcard. If you wanted to remove file1 and file2, but not file3 or file.different, you would use the following command:

rm file[12]

This command tells the shell to remove any files with names starting with file and with the character 1 or 2 following, and no other characters in the name. Each character in the brackets is taken separately. Thus, if our example directory had included a file named file12, it would not have been removed by the above command. You can also use a dash (-) to indicate a span of characters. For example, to remove file1, file2, and file3, use the following command:

rm file[1-3]

Alphabet characters can be spanned as well, in alphabetical order. The shell does pay attention to upper case and lower case letter, though, so to select all alphabet characters within square brackets, use the following syntax:

[a-z,A-Z]

You can use the square brackets in combination with other metacharacters as well. For example, the command:

rm *[2,3]

removes any files with names ending with a 2 or 3, but not file1 or file.different.


Next | Prev | Up | Top | Contents | Index