Wildcards (which may also be called shell metacharacters, or, more accurately, globbing characters ) are special characters used in filename patterns. When the shell encounters a wildcard pattern on a command-line, it replaces it, if possible, with a list of paths that match the wildcard pattern.
Some notes on where to use Wildcards:
The following sequences of characters are interpreted as wildcards by the shell. Enclosing characters in any type of quotes (single- or double-) hides them from the shell.
Operator | Meaning | Example | Matches Objects Whose Names Are |
---|---|---|---|
* | anything, including nothing | A* | capital A followed by any number (including zero) of any combination of characters |
? | a single character | ??? | exactly three characters long |
a string of characters enclosed in [ ] | a single character that is one of the characters enclosed in the brackets. | [abc] | a single character that is either a or b or c |
two characters separated by a dash enclosed in [ ] | one character whose value is greater than or equal to the first character and less than or equal to the second character | [a-z] | a single character whose value is greater than or equal to 'a' and less than or equal to 'z' . (i.e., a lowercase character) Note: uppercase characters have consecutive values, as do lowercase characters, as do digits. However, [a-Z] does not do what you want. Use [a-zA-Z] instead. |
a character class enclosed in [ ] | a single character that is a member of that character class | [[:alpha:]] | the character class is alpha. It is indicated by [:alpha:] The inner brackets indicate the character class. The outer brackets indicate the character set (that it is a single character) Both sets of brackets are needed. Classes include: alpha, upper, lower, digit, alnum, blank, space(whitespace), punct, print, ascii, cntrl, ascii | See Character Classes |
if the first character inside of the [ ] is ! or ^ | ! negates the character set. a single character that is not a member of the set. | [^[:lower:]] | a single character that is not a member of the character class [:lower:] Thus, any character that is not lowercase. |
Note: * and ? will not match a leading . in a name (indicating a hidden file)
Note: ! is the traditional negation operator in character sets. ^ is also recognized by bash for this function to make it consistent with regular expressions, which we will cover later. We will use them interchangeably, but ^ is more important to know.
bash also offers brace expansion, which uses braces to enclose a comma-separated list of alternatives. Brace expansion is not a wildcard, as this sequence is expanded whether or not each possibility exists, rather it is a pattern generator:
[[:alpha:]]*
[^012]?
z*a?
[0-9!a-z]*
*a*z
[[:alpha:]]
*[^[:digit:][:punct:]]*
Use the sample wildcard directory in our public workspace /pub/cs/grwoo/cs160a/wildcards
From this point on we will be practicing using wildcards with the ls command. As you just learned, it will be less confusing if you use the options -dF when using ls in the remainder of this exercise.
Write commands to list objects in the current directory whose names
Next, write commands to list objects whose paths are
Last, to show you that this really has nothing at all to do with ls:
The set of wildcards match anything in the current directory whose name
ls -dF ???
ls -dF [a-zA-Z]* # or [[:alpha:]]*
ls -dF *[0-9]* # or *[[:digit:]]*
ls -dF *[^0-9] # or *[^[:digit:]]
ls -dF *''* # Note you must use quotes to 'hide' the blank OR ls -df *\ * # use a backslash before the blank. You can also use the ls -dF *[[:blank:]]* # :blank: class. Careful, *[' ']* or *[\ ]* do not work!
ls -dF .*
ls -dF .?? # Why did this file not appear in the output of #8?
ls -dF *[^a-zA-Z0-9]* # can you do this using character classes?
ls -dF *[][]* # a character set containing the characters [ and ]
ls -dF *a*q* *q*a*
ls -dF */*
ls -dF [0-9]*/[a-zA-Z]???? # or [[:digit:]]*/[[:alpha:]]????
cat a*
For the original version of these materials, see Greg Boyd's Exercises Wildcards