glob
– the owner isdmr
, short forDennis MacAlistair Ritchie.glob() (/ɡlɒb/) is alibc function forglobbing, which is the archetypal use of pattern matching against thenames in a filesystem directory such that a name pattern is expanded into a list of names matching that pattern. Althoughglobbing may now refer toglob()
-style pattern matching of any string, not just expansion into a list of filesystem names, the original meaning of the term is still widespread.
Theglob()
function and the underlyinggmatch()
function originated atBell Labs in the early 1970s alongside the originalAT&T UNIX itself and had a formative influence on the syntax of UNIX command line utilities and therefore also on the present-day reimplementations thereof.
In their original form,glob()
andgmatch()
derived from code used inBell Labs in-house utilities that developed alongside the original Unix in the early 1970s. Among those utilities were also two command line tools calledglob
andfind
; each could be used to pass a list of matching filenames to other command line tools, and they shared the backend code subsequently formalized asglob()
andgmatch()
. Shell-statement-level globbing by default became commonplace following the"builtin"-integration of globbing-functionality into the7th edition of theUnix shell in 1978. The Unix shell's -f option to disable globbing — i.e. revert to literal "file" mode — appeared in the same version.
The globpattern quantifiers now standardized byPOSIX.2 (IEEE Std 1003.2) fall into two groups, and can be applied to any character sequence ("string"), not just to directory entries.
?
(not in brackets) matches any character exactly once.*
(not in brackets) matches a string of zero or more characters.[...]
, where the first character within the brackets is not '!', matches any single character among the characters specified in the brackets. If the first character within brackets is '!', then the[!...]
matches any single character that isnot among the characters specified in the brackets.[abc]
) or a range ([a-c]
) or denote a character class (like[[:space:]]
where the inner brackets are part of the classname). POSIX does not mandate multi-range ([a-c0-3]
) support, which derive originally fromregular expressions.As reimplementations ofBell Labs' UNIX proliferated, so did reimplementations of its Bell Labs' libc and shell, and with themglob()
andglobbing. Today,glob()
andglobbing are standardized by thePOSIX.2 specification and are integral part of every Unix-like libc ecosystem and shell, including AT&T Bourne shell-compatibleKorn shell (ksh),Z shell (zsh),Almquist shell (ash) and its derivatives and reimplementations such asbusybox,toybox,GNU bash,Debian dash.
The glob command, short forglobal, originates in the earliest versions of Bell Labs'Unix.[1] The command interpreters of the early versions of Unix (1st through 6th Editions, 1969–1975) relied on a separate program to expandwildcard characters in unquoted arguments to a command:/etc/glob. That program performed the expansion and supplied the expanded list of file paths to the command for execution.
Glob was originally written in theB programming language. It was the first piece of mainline Unix software to be developed in ahigh-level programming language.[2] Later, this functionality was provided as a Clibrary function,glob()
, used by programs such as theshell. It is usually defined based on a function namedfnmatch()
, which tests for whether a string matches a given pattern - the program using this function can then iterate through a series of strings (usually filenames) to determine which ones match. Both functions are a part ofPOSIX: the functions defined in POSIX.1 since 2001, and the syntax defined in POSIX.2.[3][4] The idea of defining a separate match function started withwildmat (wildcard match), a simple library to match strings against Bourne Shell globs.
Traditionally, globs do not match hidden files in the form of Unixdotfiles; to match them the pattern must explicitly start with.
. For example,*
matches all visible files while.*
matches all hidden files.
The most common wildcards are*
,?
, and[…]
.
Wildcard | Description | Example | Matches | Does not match |
---|---|---|---|---|
* | matches any number of any characters including none | Law* | Law ,Laws , orLawyer | GrokLaw ,La , oraw |
*Law* | Law ,GrokLaw , orLawyer . | La , oraw | ||
? | matches any single character | ?at | Cat ,cat ,Bat orbat | at |
[abc] | matches one character given in the bracket | [CB]at | Cat orBat | cat ,bat orCBat |
[a-z] | matches one character from the (locale-dependent) range given in the bracket | Letter[0-9] | Letter0 ,Letter1 ,Letter2 up toLetter9 | Letters ,Letter orLetter10 |
Normally, the path separator character (/
on Linux/Unix, MacOS, etc. or\
on Windows) will never be matched. Some shells, such asUnix shell have functionality allowing users to circumvent this.[5]
OnUnix-like systems*
,?
is defined as above while[…]
has two additional meanings:[6][7]
Wildcard | Description | Example | Matches | Does not match |
---|---|---|---|---|
[!abc] | matches one character that is not given in the bracket | [!C]at | Bat ,bat , orcat | Cat |
[!a-z] | matches one character that is not from the range given in the bracket | Letter[!3-5] | Letter1 ,Letter2 ,Letter6 up toLetter9 andLetterx etc. | Letter3 ,Letter4 ,Letter5 orLetterxx |
The ranges are also allowed to include pre-defined character classes, equivalence classes for accented characters, and collation symbols for hard-to-type characters. They are defined to match up with the brackets in POSIX regular expressions.[6][7]
Unix globbing is handled by theshell per POSIX tradition. Globbing is provided on filenames at thecommand line and inshell scripts.[8] The POSIX-mandatedcase
statement in shells provides pattern-matching using glob patterns.
Some shells (such as theC shell andBash) support additional syntax known asalternation orbrace expansion. Because it is not part of the glob syntax, it is not provided incase
. It is only expanded on the command line before globbing.
The Bash shell also supports the following extensions:[9]
extglob
shell option. This option came from ksh93.[10] The GNU fnmatch and glob has an identical extension.[3]**
on its own as a name component to recursively match any number of layers of non-hidden directories.[10] Also supported by theJavaScript libraries andPython's glob.dir
command with a glob pattern inIBM PC DOS 1.0.The originalDOS was a clone ofCP/M designed to work on Intel's8088 and8086 processors. Windows shells, following DOS, do not traditionally perform any glob expansion in arguments passed to external programs. Shells may use an expansion for their own builtin commands:
[…]
and for COMMAND.COM the*
may only appear at the end of the pattern. It can not appear in the middle of a pattern, except immediately preceding thefilename extension separator dot.Windows and DOS programs receive a long command-line string instead of argv-style parameters, and it is their responsibility to perform any splitting, quoting, or glob expansion. There is technically no fixed way of describing wildcards in programs since they are free to do what they wish. Two common glob expanders include:[12]
?
and*
.[13] BothReactOS (crt/misc/getargs.c) andWine (msvcrt/data.c) contain a compatible open-source implementation of__getmainargs
, the function operating under-the-hood, in their core CRT.dcrt0.cc
command-line expander, which uses the unix-styleglob()
routine under-the-hood, after splitting the arguments.Most other parts of Windows, including the Indexing Service, use the MS-DOS style of wildcards found in CMD. A relic of the 8.3 filename age, this syntax pays special attention to dots in the pattern and the text (filename). Internally this is done using three extra wildcard characters,<>"
. On the Windows API end, theglob() equivalent isFindFirstFile, andfnmatch() corresponds to its underlyingRtlIsNameInExpression.[14] (Another fnmatch analogue isPathMatchSpec.) Both open-source msvcrt expanders useFindFirstFile, so 8.3 filename quirks will also apply in them.
TheSQLLIKE
operator has an equivalent to?
and*
but not[…]
.
Common wildcard | SQL wildcard | Description |
---|---|---|
? | _ | matches any single character |
* | % | matches any number of any characters including none |
Standard SQL uses a glob-like syntax for simple string matching in itsLIKE
operator, although the term "glob" is not generally used in the SQL community. The percent sign (%
) matches zero or more characters and the underscore (_
) matches exactly one.
Many implementations of SQL have extended theLIKE
operator to allow a richer pattern-matching language, incorporating character ranges ([…]
), their negation, and elements of regular expressions.[15]
Globs do not include syntax for theKleene star which allows multiple repetitions of the preceding part of the expression; thus they are not consideredregular expressions, which can describe the full set ofregular languages over any given finite alphabet.[16]
Common wildcard | Equivalent regular expression |
---|---|
? | . |
* | .* |
Globs attempt to match the entire string (for example,S*.DOC
matches S.DOC and SA.DOC, but not POST.DOC or SURREY.DOCKS), whereas, depending on implementation details, regular expressions may match a substring.
The original Mozillaproxy auto-config implementation, which provides a glob-matching function on strings, uses a replace-as-RegExp implementation as above. The bracket syntax happens to be covered by regex in such an example.
Python's fnmatch uses a more elaborate procedure to transform the pattern into a regular expression.[17]
Beyond their uses in shells, globs patterns also find use in a variety of programming languages, mainly to process human input. A glob-style interface for returning files or an fnmatch-style interface for matching strings are found in the following programming languages:
<glob.h>
from theC POSIX library to use::glob()
.<filesystem>
and<regex>
headers, usingstd::filesystem::directory_iterator
andstd::regex_match()
.Glob
class which can act on glob patterns.[18]Microsoft.Extensions.FileSystemGlobbing
[19], which contains classMatcher
.[20]globMatch
function in thestd.path
module.[23]Glob
function in thefilepath
package.[24]java.nio.file
which contains classFiles
[25], which has methods such asFiles.newDirectoryStream(Pathdir,Stringglob)
for operating over glob patterns and returnsDirectoryStream<Path>
. The package also contains the classesFileSystems
andPathMatcher
for matching glob patterns.Glob
package with the main moduleSystem.FilePath.Glob
. The pattern syntax is based on a subset ofZsh's. It tries to optimize the given pattern and should be noticeably faster than a naïve character-by-character matcher.[26]glob
function in thenode:fs
module.[27]glob
function (as discussed inLarry Wall's bookProgramming Perl) and aGlob extension which mimics the BSD glob routine.[28] Perl's angle brackets can be used to glob as well:<*.log>
.glob
function.[29]glob
module in the standard library which performs wildcard pattern matching on filenames,[30] and anfnmatch
module with functions for matching strings or filtering lists based on these same wildcard patterns.[17]Guido van Rossum, author of the Python programming language, wrote and contributed aglob
routine toBSDUnix in 1986.[31] There were previous implementations ofglob
, e.g., in theex andftp programs in previous releases of BSD.glob
method for theDir
class which performs wildcard pattern matching on filenames.[32] Several libraries such as Rant and Rake provide aFileList
class which has a glob method or use the methodFileList.[]
identically.glob
crate which itself has aglob()
function.[34]GLOB
function.fnmatch(3)
– Linux Programmer'sManual – Library Functions from Manned.orgglob(3)
– Linux Programmer'sManual – Library Functions from Manned.org