Wildcards, Sets, and Brace Expansion

We’ve already used wildcards in a few places in this chapter, but there’s a lot more you can do with them than just using the * and ? operators to match strings and characters, respectively, as shown in Table 7-5.

Table 7-5. Wildcards and their meanings

Wildcard

Meaning

*

Any sequence of characters, except for a leading period

?

Any single character, except for a leading period

[set]

Any one of the characters in the set

[ch1-ch2]

Any one of the characters from ch1 through ch2

[^set]

Any single character not in the set

[^ch1-ch2]

Any single character not from ch1 through ch2

{ch,str,etc…}

Match all the characters and/or strings

A set of characters placed within a pair of rectangular brackets can match a single character in a file or folder name. This is a more precise version of the ? single-character wildcard. For example, the set [abcdef] will match any of the six characters shown so that [abcdef]ark.txt will match the filenames bark.txt and dark.txt, as long as they both exist. You can also save on typing by using a hyphen to indicate a range of characters so that [a-f] is equivalent to [abcdef].

Another neat thing you can do with a set is force all upper- or all lowercase matches using the ranges [A-Z] or [a-z]. Or you can limit the matching to only alphabetical characters of either case (excluding digits and other characters) by using the set [a-zA-z]. And you can also use numbers in a range, as in [0-9].

Alternatively, you can exclude characters from matching by using the ^ operator. In this case, [^b]ark.txt would prevent the file bark.txt from being matched, but would let through dark.txt, lark.txt, and so on.

You can also use ranges with the ^ operator so that [^a-l]ark.txt will allow through only filenames starting from mark.txt onward, as all the first letters prior to m are excluded.

With brace expansion you can offer sets of alternatives strings of any length, so you aren’t limited to the single characters of sets. For example, all three terms in the expression ca{ree,mpe,tere}rs.txt will be expanded, allowing all of the files careers.txt, campers.txt and caterers.txt through the filter (if they exist), with the matching portions shown in bold.

Unlike sets and wildcards, brace expansions are also supported in other parts of the command line, so the following echo command will show you the result of using the expression just shown:

echo ca{ree,mpe,tere}rs

The following is the displayed result:

careers campers caterers

By the way, did you notice that there are no quotation marks around the argument to the echo command (unlike previous examples in this chapter)? That’s because quotation marks tell Ubuntu to use the exact contents of the string, rather than supporting brace expansion and other features. Therefore the following command results in displaying only the expression itself, so make sure you know when you do and when you don’t need quotation marks:

echo "ca{ree,mpe,tere}rs"

The displayed result of entering this command is simply:

ca{ree,mpe,tere}rs