3.3. shell globbing patterns and regular expressions

You probably already use globbing characters without knowing it. When you specify a file in Windows or when you look for a file, you use * to match a random string. For example, *.txt matches all files with names ending in .txt. We also used it heavily in the last section. But there is more to globbing than *.

When you type a command like ls *.txt and press Return, the task of finding which files match the pattern *.txt is not done by the ls command, but by the shell itself. This requires a little explanation about how a command line is interpreted by the shell. When you type:
$ ls *.txt
readme.txt  recipes.txt
the command line is first split into words (ls and *.txt in this example) . When it sees a * in a word, it will interpret the whole word as a globbing pattern and will replace it with the names of all matching files. Therefore, the command, just before the shell executes it, has become ls readme.txt recipe.txt, which gives the expected result. Other characters make the shell react this way:

  1. ? matches one and only one character, regardless of what that character is;

  2. [...] matches any character found in the brackets; characters can be referred to either as a range of characters (e.g, 1-9) or discrete values, or even both. Example: [a-zBE5-7] will match all characters a to z, a B, a E, a 5, a 6 or a 7;

  3. [!...] matches any character not found in the brackets. [!a-z], for example, will match any character which is not a lowercase letter;

  4. {c1,c2} matches c1 or c2, where c1 and c2 are also globbing patterns.

Here are some patterns and their meaning:

  1. /etc/*conf All files in the /etc directory with names ending in conf. It can match /etc/inetd.conf, /etc/conf.linuxconf, and also /etc/conf if such a file exists. Remember that * can also match an empty string.

  2. image/cars,space[0-9]/*.jpg All filenames ending with .jpg in the directories image/cars, image/space0, ... , image/space9, if those directories exist.

  3. /usr/doc/*/README All files named README in all immediate subdirectories of the /usr/doc directory. This will make /usr/doc/mandrake/README match for example, but not /usr/doc/myprog/doc/README.

  4. *[!a-z] All files with names that do not end with a lowercase letter in the current directory.


Tux on Star from MandrakeSoft Linux is a registered trademark of Linus Torvalds. All other trademarks and copyrights are the property of their respective owners.
Unless otherwise stated, all the content of these pages and all images are Copyright MandrakeSoft S.A. and MandrakeSoft Inc. 2000.
http://www.linux-mandrake.com/