Shell Globbing Patterns

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 with .txt. We also used it heavily in the last section. But there is more to globbing than just *.

When you type a command like ls *.txt and press Enter, the task of finding which files match the *.txt pattern 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 the shell 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 too:

Here are some patterns and their meanings:



[5] Beware! While this is true for most languages, this may not be true under your own language setting (locale). This depends on the collating order. On some language configurations, [a-z] will match a, A, b, B, (...), z. And we do not even mention the fact that some languages have accentuated characters...