The Wild Side of Unix

There are special characters in Unix which are "wild card" characters; they can represent other characters. Their official name is Filename Metacharacters. The metacharacters were briefly mentioned in the last Tipsheet (Vol. 1, No. 10). Due to their special properties, these metacharacters should not be used as regular parts of a file name. You can force them to be used in a file name, but you must then always use quotation marks around the filename to keep the metacharacters from being interpreted as special characters.

The first example metacharacter is one that has been used in these Tipsheets a number of times before. It is the asterisk, "*". It can represent zero or more characters. For instance, to list all files that begin with the letter "f" you could type

ls f*

or to list all files that ended with the suffix ".txt" you could type

ls *.txt

It can be used with commands that work on files, such as

grep word *.txt

which would look for the word "word" in all files ending with ".txt". If you have a series of files you wish to print and they share a common part in the filename you can use the asterisk. For example, if they all have ".ps" as a suffix, you could print them all at once with

lp *.ps

It can also be used in a change-directory command. This is very helpful to the lazy typist, but works only if there is no ambiguity in the directory name you want to change into. So, to go into "public_html" from your home directory, which is usually a challenge for any typist, you could simply type

cd pu*

If there are two or more directories that start with "pu" this command will complain, but you can always arrange not to have this problem by not creating conflicting directory names.

The next metacharacter is the question mark, "?". It will match any single character. It helps you create a more restricted filename pattern. This metacharacter can be repeated, either side-by-side or apart, if necessary. So, to print files that start with any two characters (no more, no less) and then have data.dat as the rest of their name you would type

lp ??data.dat

Now this really gets wild when you start to mix the two metacharacters. It is perfectly all right to use

lp ??data.*

if that is what you want to do.

There are still more metacharacters. You can enclose characters in "[" and "]" to specify a list or range of allowable characters. This would look like "[abguz]" to pick any one of those characters or "[A-D]" to match a range of capital A through capital D. Of course, this syntax can be mixed with the "*" or "?" if you need to do this.

A metacharacter that is usefile for moving around your directory structure is "~". It refers to your home directory. For instance, if you are off somewhere deep in your directory structure and you want to get to your "public_html" directory very quickly you could type

cd ~/public_html

or, remembering the earlier syntax using wild cards,

cd ~/pu*

The examples shown here are common to the different shells (Korn shell, Bourne shell, C shell, etc). In the Bourne and Korn shells you can also use "[!A-D]" to match any character not in the range "A-D" (or a list of characters as was shown earlier). In the Korn shell you can also use ]

  • ?(pattern) to match zero or one instance of pattern
  • *(pattern) to match zero or more instances of pattern
  • +(pattern) to match one or more instance of pattern
  • @(pattern) to match exacly one instance of pattern
  • !(pattern) to match any filenames that don't contain pattern
  • ~_ to match the previous working directory (the one you were in before the last "cd"

For the pattern you can also have a series of patterns separated by "|" (the vertical bar).

There are a few other lesser know metacharacters. You will find these listed in any Unix book. All the metacharacters greatly speed your ability to move around your directories and to work on files by reducing the amount of typing you need to do.

 

© 1998, The University of Alabama. The information included here is for the University of Alabama central computing facility as it was configured on the document date. It may or may not apply to other Unix systems.