Login

Please fill in your details to login.





globbing

Globbing is for filename completion.
The shell uses globbing for filename completion ('glob' is short for 'global'). Glob patterns are to filenames what regular expressions are to text, though glob patterns are much more restrictive.

Asterisk
*


An asterisk (not between brackets) matches any string, including the empty string. NOTE: This is different to regular expressions where the asterisk denotes zero or more of the preceding thing.


Question mark
?


A question mark (not between brackets) matches any single character. NOTE: This is different to regex where the question mark denotes zero or one of the preceding thing.


Character classes
[


An expression "[...]" where the first character after the leading '[' is not an '!' matches a single character, namely any of the characters enclosed by the brackets. The string enclosed by the brackets cannot be empty; therefore ']' can be allowed between the brackets, provided that it is the first character.


Ranges
[?-?]


Two characters separated by '-' denote a range. The characters have to be contiguous unicode characters.

[A-Z]
or
[:upper:]
matches any single character from the range A through Z.
[0-9]
or
[:digit:]
matches any single character from the range 0 through 9.
[a-z]
or
[:lower:]
matches any single character from the range a through z.
[a-zA-Z]
or
[:alpha:]
matches any single character from the range a through z or A through Z.
Subranges are also allowed, so
[a-f]
would match any single character from a to f.


Negation
!


Known as Complementation. NOTE: In regular expressions, negation is denoted with the caret or hat symbol
^
. The caret has no meaning in globbing (but it does in grepping). The expression
[!...]
matches a single character which is not matched by the expression inside the brackets obtained by removing the first
!
.


Escaping


Known as Complementation. You can remove the special meaning of
*
,
?
and
[
by preceding (escaping) them with a backslash
\
or enclosing them in quotation marks. Between brackets, these characters stand for themselves.


The Globstar
**


The globstar may be disabled. If it is,
**
acts like
*
. Check with
shopt
(SHell OPTions)...

$ shopt globstar     # See if globstar is enabled
$ shopt -s globstar  # Enable globstar
$ shopt -u globstar  # Disable globstar


The globstar matches zero or more directories and it's subdirectories. This allows for recursive directory searching easily.


Lists
{a,b,c}


Matches exactly one of the "parts" provided.

Patterns
(a|b|c)


Patterns are part of extended globbing and must be enabled before you can use them. Check with
shopt
(SHell OPTions)...

$ shopt extglob     # See if extended globbing is enabled
$ shopt -s extglob  # Enable extended globbing
$ shopt -u extglob  # Disable extended globbing


?(a|b|c)
Zero or one of the patterns.
@(a|b|c)
Exactly one of the patterns.
*(a|b|c)
Zero or more of the patterns.
+(a|b|c)
One or more of the patterns.
!(a|b|c)
None of the patterns.

Last modified: April 24th, 2022
The Computing Café works best in landscape mode.
Rotate your device.
Dismiss Warning