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:
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$ lsabc abcdefghijk abc.php abc.txt ABC.txt def mydoc.odt xyz.xmlabcdef abcdef.odt abctxt abc.TXT alphabet DEF xyz xyz.yamlGlobbing 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 alphabetabcdef abcdef.odt abctxt abc.TXTholloway:demo$ ls A*ABC.txtholloway:demo$ ls *.txtabc.txt ABC.txtholloway:demo$ ls *txtabctxt abc.txt ABC.txtholloway:demo$ ls a*b*abc abcdefghijk abc.php abc.txt alphabetabcdef abcdef.odt abctxt abc.TXTholloway:demo$ ls *h*abcdefghijk abc.php alphabetQuestion mark is a length-specific symbolic wildcard regardless any character. Example:
holloway:demo$ ls D??DEFholloway:demo$ ls D?FDEFholloway:demo$ ls d??defholloway:demo$ ls *.???abcdef.odt abc.php abc.txt abc.TXT ABC.txt mydoc.odt xyz.xmlholloway:demo$ ls *.????xyz.yamlCharacter set to search one character listed inside the set. Example:
holloway:demo$ ls a[a-z]c.txtabc.txtholloway:demo$ ls a[0-9]c.txtls: cannot access 'a[0-9]c.txt': No such file or directoryholloway:demo$ ls a[a-z]?.txtabc.txtholloway:demo$ ls a[[:alpha:]]?.txtabc.txtholloway:demo$ ls a[[:alnum:]]?.txtabc.txtholloway:demo$ ls a[[:blank:]]?.txtls: cannot access 'a[[:blank:]]?.txt': No such file or directoryholloway:demo$ ls a[[:digit:]]?.txtls: cannot access 'a[[:digit:]]?.txt': No such file or directoryholloway:demo$ ls a[[:lower:]]?.txtabc.txtholloway:demo$ ls [[:upper:]][[:upper:]][[:upper:]].txtABC.txtholloway:demo$ ls [[:xdigit:]][[:xdigit:]][[:xdigit:]].txtabc.txt ABC.txtThere 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-fSometimes, 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.
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.txtabc.TXTholloway:demo$That's all about the wildcards. Feel free to proceed to the next section.