Arguments to commands are frequently filenames. These might be the names of files the command should read, copy, or move. If you want to act on a number of files, you don't want to have to type every filename, especially when all the filenames have some pattern in common. For example, you might want to do something with a group of files whose names all start with Hello.

That's where wildcards come in. Wildcards (often called glob-patterns ) are special characters you can type in a command line to make a command apply to a group of files whose names match some patternfor example, all files ending in .jpg.

When the shell reads a command line, it expands any glob-patterns by replacing them with all the filenames that match. The shell then executes the command line, using the new list of arguments with the command.

To use a glob-pattern to match all filenames starting with Hello:

  • ls Hello*

    The asterisk ( * ) is the glob character that matches any number (zero or more) of characters.

    The shell finds all the filenames that begin with Hello and substitutes that list for the Hello* on the command line.

    So if the directory contains files with the names Hello, HelloTest, and HelloGoodbye, then the shell changes the command line with the wildcard into ls -l Hello HelloTest HelloGoodbye


  • You can use more than one glob-pattern in a command line, such as rm *.jpg *.gif

    This removes all the .jpg and .gif files from the current directory.

To use a glob-pattern to match only one single character:

  • ls File?

    The ? character matches any single character. So the example above would match files with names such as FileA, File3, Files, and so on. It would not match File23 because the pattern matches only one character.


  • You can combine the ? and * glob characters together. For example,

    ls ??.*

    This would list files whose names begin with exactly two characters, followed by a period, followed by anything. (The period is matched literally.)

More-specialized glob-patterns

Sometimes you want to use a list of files that match a more specific pattern. For this you might use a more complex kind of pattern.

To match a range of characters:

ls /var/log/system.log.[0-3].gz

would result in output similar to that shown in Figure 2.32 .

Figure 2.32. This shows the output when you use a glob-patternin this case [0-3] for a range of characters.
 user-vc8f9gd:~ vanilla$ ls /var/log/system.log.[0-3].gz /var/log/system.log.0.gz       /var/log/system.log.2.gz /var/log/system.log.1.gz       /var/log/system.log.3.gz user-vc8f9gd:~ vanilla$ 

The [ and ] characters are used to create a glob-pattern called a character class . The resulting pattern matches any single character in the class. A range of characters can be indicated by using the hyphen, so that [0-3] is the same as [0123].

Ranges may be alphabetical as well as numeric:

ls Alpha-[A-D]

Unix filenames are case sensitive. You can match either case by including both in the character class:

ls Alpha-[A-Da-d]


  • You can create a character class that is quite arbitraryfor example, the glob-pattern


    matches only Photo-A and Photo-D .

To negate a character class:

  • Use the ^ character as the first character in the character class.

    When you do this, the glob-pattern *[^3-8] matches anything that does not end in 3, 4, 5, 6, 7, or 8 (the * matches anything; the [^3-8] means "do not match 3-8").

Patterns and rules similar to those described here are used in many different Unix tools, especially in a set of tools called regular expressions. See Chapter 4, "Useful Unix Utilities," for more on regular expressions.

Unix for Mac OS X 10. 4 Tiger. Visual QuickPro Guide
Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)
ISBN: 0321246683
EAN: 2147483647
Year: 2004
Pages: 161
Authors: Matisse Enzer

Similar book on Amazon

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