Wildcard
Wildcard is a "generalized" tool that helps you to operate in a pattern manner rather than a full name command / object. POSIX shell has 2 forms of wildcard:
- Globbing
- Regular Expression
Here, we will focus on #1 - Globbing in terminal command. In this example, we're going to use a directory that has the following files:
holloway:demo$ ls
abc abcdefghijk abc.php abc.txt ABC.txt def mydoc.odt xyz.xml
abcdef abcdef.odt abctxt abc.TXT alphabet DEF xyz xyz.yaml
Globbing
Globbing is the symbolic wildcards. There are useful in command searches like searching a list of item with X or Y pattern.
*
Asterisk symbolizes anything else that matches regardless length. Example:
holloway:demo$ ls a*
abc abcdefghijk abc.php abc.txt alphabet
abcdef abcdef.odt abctxt abc.TXT
holloway:demo$ ls A*
ABC.txt
holloway:demo$ ls *.txt
abc.txt ABC.txt
holloway:demo$ ls *txt
abctxt abc.txt ABC.txt
holloway:demo$ ls a*b*
abc abcdefghijk abc.php abc.txt alphabet
abcdef abcdef.odt abctxt abc.TXT
holloway:demo$ ls *h*
abcdefghijk abc.php alphabet
?
Question mark is a length-specific symbolic wildcard regardless any character. Example:
holloway:demo$ ls D??
DEF
holloway:demo$ ls D?F
DEF
holloway:demo$ ls d??
def
holloway:demo$ ls *.???
abcdef.odt abc.php abc.txt abc.TXT ABC.txt mydoc.odt xyz.xml
holloway:demo$ ls *.????
xyz.yaml
[]
Character set to search one character listed inside the set. Example:
holloway:demo$ ls a[a-z]c.txt
abc.txt
holloway:demo$ ls a[0-9]c.txt
ls: cannot access 'a[0-9]c.txt': No such file or directory
holloway:demo$ ls a[a-z]?.txt
abc.txt
holloway:demo$ ls a[[:alpha:]]?.txt
abc.txt
holloway:demo$ ls a[[:alnum:]]?.txt
abc.txt
holloway:demo$ ls a[[:blank:]]?.txt
ls: cannot access 'a[[:blank:]]?.txt': No such file or directory
holloway:demo$ ls a[[:digit:]]?.txt
ls: cannot access 'a[[:digit:]]?.txt': No such file or directory
holloway:demo$ ls a[[:lower:]]?.txt
abc.txt
holloway:demo$ ls [[:upper:]][[:upper:]][[:upper:]].txt
ABC.txt
holloway:demo$ ls [[:xdigit:]][[:xdigit:]][[:xdigit:]].txt
abc.txt ABC.txt
There are a list of regular expression ranges:
[[:alnum:]]
- A-Z, a-z, 0-9[[:alpha:]]
- A-Z, a-z[[:blank:]]
- Space, tab[[:cntl:]]
- ASCII characters 0-31 (non-printing control characters)[[:digit:]]
- 0-9[[:graph:]]
- ASCII characters 0-31 (non-printing control characters)[[:lower:]]
- a-z[[:print:]]
- ASCII characters 32-127 (printable characters)[[:punct:]]
- Punctuation (printable characters other than A-Z, a-z, 0-9)[[:space:]]
- Space, Tab, LF (10), VT (11), FF (12), CR (13)[[:upper:]]
- A-Z[[:xdigit:]]
- 0-9, A-F, a-f
Toggle Globing and Shell Options Command
Sometimes, you might want to toggle the glob settings in your terminal. For this function, you need to refer back to the settings offered by your shell program. However, you have to double check each manually toggled options is POSIX compatible before use.
Not Supported
{}
curly brace expression is not supported in POSIX shell. Hence, you can't do something like:
holloway:demo$ ls abc.{txt,TXT}
You can however, workaround with grep
regular expression:
holloway:demo$ ls abc.* | grep 'txt\|TXT'
abc.txt
abc.TXT
holloway:demo$
That's all about the wildcards. Feel free to proceed to the next section.