Wildcard is a "generalized" tool that helps you to operate in a pattern manner rather than a full name command / object. BASH 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.yamlListing set the search to have the options available within the list. Example:
holloway:demo$ ls abc.*abc.php abc.txt abc.TXTholloway:demo$ ls abc.{txt,TXT}abc.txt abc.TXTCharacter 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. You can do so by both BASH and terminal means.
To enable or disable Globbing, use:
$ set -o noglob$ set +o noglobNormally, we disable glob in situation where we really want the glob as a character, not a symbol. Example, naming a file with a glob character:
$ ls -l *d*.odt-rw-rw-r-- 1 steve steve 505 Nov 13 10:13 abcdef.odt -rw-rw-r-- 1 steve steve 355 Dec 20 09:17 mydoc.odt $ touch *d*.odt$ ls -l *d*.odt-rw-rw-r-- 1 steve steve 505 Dec 22 11:51 abcdef.odt -rw-rw-r-- 1 steve steve 355 Dec 22 11:51 mydoc.odt $ set -o noglob$ touch *d*.odt$ set +o noglob$ ls -l *d*.odt-rw-rw-r-- 1 steve steve 505 Dec 22 11:51 abcdef.odt -rw-rw-r-- 1 steve steve 0 Dec 22 11:52 *d*.odt -rw-rw-r-- 1 steve steve 355 Dec 22 11:51 mydoc.odt $ Sometimes, you might want to set option to use the glob in different way. You can do so by both BASH and terminal means using shopt command. It has 5 common commands arguments:
-o - restricts OPTNAMEs to those defined for the use with set -o.-p - print each shell options with an indication of its status-q - quiet the output (suppress output)-s - set (enable) the OPTNAME-u - unset (disable) the OPTNAMETo start, let's list all the available OPTNAMEs with shopt -p:
holloway:demo$ shopt -pshopt -u autocdshopt -u cdable_varsshopt -u cdspellshopt -u checkhashshopt -u checkjobsshopt -s checkwinsizeshopt -s cmdhistshopt -u compat31shopt -u compat32shopt -u compat40shopt -u compat41shopt -u compat42shopt -u compat43shopt -s complete_fullquoteshopt -u direxpandshopt -u dirspellshopt -u dotglobshopt -u execfailshopt -s expand_aliasesshopt -u extdebugshopt -s extglobshopt -s extquoteshopt -u failglobshopt -s force_fignoreshopt -u globasciirangesshopt -u globstarshopt -u gnu_errfmtshopt -s histappendshopt -u histreeditshopt -u histverifyshopt -u hostcompleteshopt -u huponexitshopt -u inherit_errexitshopt -s interactive_commentsshopt -u lastpipeshopt -u lithistshopt -u login_shellshopt -u mailwarnshopt -u no_empty_cmd_completionshopt -u nocaseglobshopt -u nocasematchshopt -u nullglobshopt -s progcompshopt -s promptvarsshopt -u restricted_shellshopt -u shift_verboseshopt -s sourcepathshopt -u xpg_echoholloway:demo$As you can see, those with -s are set while those with -u are unset.
If you want to view one particular option, simply supply the OPTNAME into it:
holloway:demo$ shopt -p xpg_echoshopt -u xpg_echoholloway:demo$Now that you have the list, you probably want to set some OPTNAME based on your preferences. You can set them using the shopt -s OPTNAME command. If you did not provide OPTNAME, shopt will set everything. Example:
holloway:demo$ shopt -p nullglobshopt -u nullglobholloway:demo$ shopt -s nullglobholloway:demo$ shopt -p nullglobshopt -s nullglobholloway:demo$If you plan to unset some of the existing, you can proceed to use shopt -u OPTNAME instead. If you did not provide OPTNAME, shopt will unset everything. Example:
holloway:demo$ shopt -p nullglobshopt -s nullglobholloway:demo$ shopt -u nullglobholloway:demo$ shopt -p nullglobshopt -u nullglobholloway:demo$As you may be curious, how can I study each of the options printed out for me? You can refer to the built-in manual. You probably not needing to toggle these options unless there is a special needs. Most of the stock configurations are well set. However, just for introduction, there are some you might find it interesting.
This is usually default to unset. If the shell match no pattern, it returns an error number instead.
Usually set. However, some are not. This extends your glob options reach like AND or OR, one or many matches, etc.
?(pattern-list)- Zero or one of the patterns
*(pattern-list) - Zero or more of the patterns
+(pattern-list) - One or more of the patterns
@(pattern-list) - Exactly one of the patterns
!(pattern-list) - Anything except one of the patterns
The most useful one would be !(...), @(...), and ?(...) which are commonly used. Here's an example:
$ ls abc@(.txt|.php)abc.php abc.txt $ touch abc.txt.txt$ ls abc@(.txt|.php)abc.php abc.txt $ ls abc+(.txt|.php)abc.php abc.txt abc.txt.txt $ ls abc*(.txt|.php)abc abc.php abc.txt abc.txt.txt $ ls abc@(.txt|.php)abc.php abc.txt $ ls abc!(.txt|.php)abc abcdef abcdefghijk abcdef.odt abctxt abc.TXT abc.txt.txt $ ls abc?(.)txtabctxt abc.txt $ ls abc?(def)abc abcdef $ ls abc?(def|.txt)abc abcdef abc.txt $ Turn the glob pattern into case-insensitive or case-sensitive.
That's all about the wildcards. Feel free to proceed to the next section.