
glob() () is a
libc function for ''globbing'', which is the archetypal use of pattern matching against the
names in a filesystem directory such that a name pattern is expanded into a list of names matching that pattern. Although ''globbing'' may now refer to glob()-style pattern matching of any string, not just expansion into a list of filesystem names, the original meaning of the term is still widespread.
The
glob()
function and the underlying
gmatch()
function originated at
Bell Labs
Nokia Bell Labs, commonly referred to as ''Bell Labs'', is an American industrial research and development company owned by Finnish technology company Nokia. With headquarters located in Murray Hill, New Jersey, Murray Hill, New Jersey, the compa ...
in the early 1970s alongside the original
AT&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()
and
gmatch()
derived from code used in
Bell Labs
Nokia Bell Labs, commonly referred to as ''Bell Labs'', is an American industrial research and development company owned by Finnish technology company Nokia. With headquarters located in Murray Hill, New Jersey, Murray Hill, New Jersey, the compa ...
in-house utilities that developed alongside the original Unix in the early 1970s. Among those utilities were also two command line tools called
glob
and
find
; each could be used to pass a list of matching filenames to other command line tools, and they shared the backend code subsequently formalized as
glob()
and
gmatch()
. Shell-statement-level globbing by default became commonplace following the
"builtin"-integration of globbing-functionality into the
7th edition of the
Unix shell
A Unix shell is a Command-line_interface#Command-line_interpreter, command-line interpreter or shell (computing), shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command languag ...
in 1978. The Unix shell's -f option to disable globbing — i.e. revert to literal "file" mode — appeared in the same version.
The glob ''pattern quantifiers'' now standardized by
POSIX.2 (IEEE Std 1003.2) fall into two groups, and can be applied to any character sequence ("string"), not just to directory entries.
* "Metacharacters" (also called "Wildcards"):
**
?
(not in brackets) matches any character exactly once.
**
*
(not in brackets) matches a string of zero or more characters.
* "Ranges/sets":
**
../nowiki>
, 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
.../nowiki>
matches any single character that is ''not'' among the characters specified in the brackets.
:: The characters in the brackets may be a list (
bc/nowiki>
) or a range (
-c/nowiki>
) or denote a character class (like
:space:
where the inner brackets are part of the classname). POSIX does not mandate multi-range (
-c0-3/nowiki>
) support, which derive originally from
regular expressions
A regular expression (shortened as regex or regexp), sometimes referred to as rational expression, is a sequence of character (computing), characters that specifies a pattern matching, match pattern in string (computer science), text. Usually ...
.
As reimplementations of
Bell Labs
Nokia Bell Labs, commonly referred to as ''Bell Labs'', is an American industrial research and development company owned by Finnish technology company Nokia. With headquarters located in Murray Hill, New Jersey, Murray Hill, New Jersey, the compa ...
' UNIX proliferated, so did reimplementations of its Bell Labs' libc and shell, and with them
glob()
and ''globbing''. Today,
glob()
and ''globbing'' are standardized by the
POSIX.2 specification and are integral part of every Unix-like libc ecosystem and shell, including AT&T Bourne shell-compatible
Korn shell (ksh),
Z shell (zsh),
Almquist shell (ash) and its derivatives and reimplementations such as
busybox,
toybox,
GNU bash,
Debian dash.
Origin
The glob command, short for ''global'', originates in the earliest versions of Bell Labs'
Unix
Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
.
The command interpreters of the early versions of Unix (1st through 6th Editions, 1969–1975) relied on a separate program to expand
wildcard character
In software, a wildcard character is a kind of placeholder represented by a single character (computing), character, such as an asterisk (), which can be interpreted as a number of literal characters or an empty string. It is often used in file ...
s 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 the
B programming language. It was the first piece of mainline Unix software to be developed in a
high-level programming language
A high-level programming language is a programming language with strong Abstraction (computer science), abstraction from the details of the computer. In contrast to low-level programming languages, it may use natural language ''elements'', be ea ...
.
Later, this functionality was provided as a C
library function,
glob()
, used by programs such as the
shell
Shell may refer to:
Architecture and design
* Shell (structure), a thin structure
** Concrete shell, a thin shell of concrete, usually with no interior columns or exterior buttresses
Science Biology
* Seashell, a hard outer layer of a marine ani ...
. It is usually defined based on a function named
fnmatch()
, 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 of
POSIX
The Portable Operating System Interface (POSIX; ) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines application programming interfaces (APIs), along with comm ...
: the functions defined in POSIX.1 since 2001, and the syntax defined in POSIX.2.
The idea of defining a separate match function started with
wildmat (wildcard match), a simple library to match strings against Bourne Shell globs.
Traditionally, globs do not match hidden files in the form of Unix
dotfiles; to match them the pattern must explicitly start with
.
. For example,
*
matches all visible files while
.*
matches all hidden files.
Syntax
The most common wildcards are , , and .
Normally, the path separator character ( on Linux/Unix, MacOS, etc. or on Windows) will never be matched. Some shells, such as
Unix shell
A Unix shell is a Command-line_interface#Command-line_interpreter, command-line interpreter or shell (computing), shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command languag ...
have functionality allowing users to circumvent this.
Unix-like
On
Unix-like
A Unix-like (sometimes referred to as UN*X, *nix or *NIX) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Uni ...
systems , is defined as above while has two additional meanings:
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.
Unix globbing is handled by the
shell
Shell may refer to:
Architecture and design
* Shell (structure), a thin structure
** Concrete shell, a thin shell of concrete, usually with no interior columns or exterior buttresses
Science Biology
* Seashell, a hard outer layer of a marine ani ...
per POSIX tradition. Globbing is provided on filenames at the
command line and in
shell scripts.
The POSIX-mandated
case
statement in shells provides pattern-matching using glob patterns.
Some shells (such as the
C shell and
Bash) support additional syntax known as
alternation or
brace expansion. Because it is not part of the glob syntax, it is not provided in
case
. It is only expanded on the command line before globbing.
The Bash shell also supports the following extensions:
* Extended globbing (extglob): allows other pattern matching operators to be used to match multiple occurrences of a pattern enclosed in parentheses, essentially providing the missing
kleene star
In mathematical logic and theoretical computer science, the Kleene star (or Kleene operator or Kleene closure) is a unary operation on a Set (mathematics), set to generate a set of all finite-length strings that are composed of zero or more repe ...
and alternation for describing regular languages. It can be enabled by setting the shell option. This option came from ksh93.
The GNU fnmatch and glob has an identical extension.
[
* globstar: allows ]**
on its own as a name component to recursively match any number of layers of non-hidden directories. Also supported by the JavaScript
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
libraries and Python's glob.
Windows and DOS
The original DOS was a clone of CP/M designed to work on Intel's 8088 and 8086 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:
* Windows PowerShell has all the common syntax defined as stated above without any additions.
* COMMAND.COM and cmd.exe have most of the common syntax with some limitations: There is no 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 the filename extension
A filename extension, file name extension or file extension is a suffix to the name of a computer file (for example, .txt, .mp3, .exe) that indicates a characteristic of the file contents or its intended use. A filename extension is typically d ...
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:
* The Microsoft C Runtime (msvcrt) command-line expander, which only supports and . Both ReactOS
ReactOS is a Free and open-source software, free and open-source operating system for i586/amd64 personal computers that is intended to be binary-code compatibility, binary-compatible with computer programs and device drivers developed for Wind ...
(crt/misc/getargs.c) and Wine
Wine is an alcoholic drink made from Fermentation in winemaking, fermented fruit. Yeast in winemaking, Yeast consumes the sugar in the fruit and converts it to ethanol and carbon dioxide, releasing heat in the process. Wine is most often made f ...
(msvcrt/data.c) contain a compatible open-source implementation of , the function operating under-the-hood, in their core CRT.
* The Cygwin and MSYS command-line expander, which uses the unix-style 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, the equivalent is , and corresponds to its underlying . (Another fnmatch analogue is .) Both open-source msvcrt expanders use , so 8.3 filename quirks will also apply in them.
SQL
The SQL operator has an equivalent to and but not .
Standard SQL uses a glob-like syntax for simple string matching in its LIKE
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 the LIKE
operator to allow a richer pattern-matching language, incorporating character ranges (), their negation, and elements of regular expressions.
Compared to regular expressions
Globs do not include syntax for the Kleene star
In mathematical logic and theoretical computer science, the Kleene star (or Kleene operator or Kleene closure) is a unary operation on a Set (mathematics), set to generate a set of all finite-length strings that are composed of zero or more repe ...
which allows multiple repetitions of the preceding part of the expression; thus they are not considered regular expression
A regular expression (shortened as regex or regexp), sometimes referred to as rational expression, is a sequence of characters that specifies a match pattern in text. Usually such patterns are used by string-searching algorithms for "find" ...
s, which can describe the full set of regular language
In theoretical computer science and formal language theory, a regular language (also called a rational language) is a formal language that can be defined by a regular expression, in the strict sense in theoretical computer science (as opposed to ...
s over any given finite alphabet.
Globs attempt to match the entire string (for example, matches S.DOC and SA.DOC, but not POST.DOC or SURREY.DOCKS), whereas, depending on implementation details, regular expressions may match a substring.
Implementing as regular expressions
The original Mozilla proxy 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.
Other implementations
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:
* C and C++ do not have built-in support for glob patterns in the ISO-defined standard libraries, however on Unix-like systems C and C++ may include
from the C POSIX library to use glob()
.
** C++ itself does not have direct support for glob patterns, however they may be approximated using the
and
headers, using std::filesystem::directory_iterator()
and std::regex_match()
.
* C# has multiple libraries available through NuGet such as Glob
or DotNet.Glob
.
* D has a globMatch
function in the std.path
module.
* JavaScript
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
has a library called minimatch
which is used internally by npm, and micromatch
, a purportedly more optimized, accurate and safer globbing implementation used by Babel and yarn.
* Go has a Glob
function in the filepath
package.
* Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
has a Files
class in the package java.nio.file
, containing methods that can operate on glob patterns.
* Haskell
Haskell () is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell pioneered several programming language ...
has a Glob
package with the main module System.FilePath.Glob
. The pattern syntax is based on a subset of Zsh's. It tries to optimize the given pattern and should be noticeably faster than a naïve character-by-character matcher.
* Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language".
Perl was developed ...
has both a glob
function (as discussed in Larry Wall's book '' Programming Perl'') and a ''Glob'' extension which mimics the BSD glob routine. Perl's angle brackets can be used to glob as well: <*.log>
.
* PHP has a glob
function.
* Python has a glob
module in the standard library which performs wildcard pattern matching on filenames, and an fnmatch
module with functions for matching strings or filtering lists based on these same wildcard patterns.[ Guido van Rossum, author of the Python programming language, wrote and contributed a ]glob
routine to BSD Unix
Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
in 1986. There were previous implementations of glob
, e.g., in the ex and ftp
The File Transfer Protocol (FTP) is a standard communication protocol used for the transfer of computer files from a server to a client on a computer network. FTP is built on a client–server model architecture using separate control and dat ...
programs in previous releases of BSD.
* Ruby
Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...
has a glob
method for the Dir
class which performs wildcard pattern matching on filenames. Several libraries such as Rant and Rake provide a FileList
class which has a glob method or use the method FileList.[]
identically.
* Rust (programming language), Rust has multiple libraries that can match glob patterns, the most popular of these being the glob
crate.
* SQLite has a GLOB
function.
* Tcl contains a globbing facility.
See also
* Regular expression
A regular expression (shortened as regex or regexp), sometimes referred to as rational expression, is a sequence of characters that specifies a match pattern in text. Usually such patterns are used by string-searching algorithms for "find" ...
* Wildcard character
In software, a wildcard character is a kind of placeholder represented by a single character (computing), character, such as an asterisk (), which can be interpreted as a number of literal characters or an empty string. It is often used in file ...
* Matching wildcards
References
{{Reflist, refs=
[{{cite web , url=http://cm.bell-labs.com/cm/cs/who/dmr/man71.pdf , archive-url=https://web.archive.org/web/20000829224359/http://cm.bell-labs.com/cm/cs/who/dmr/man71.pdf , url-status=dead , archive-date=2000-08-29 , title=First Edition Unix manual 'Miscellaneous' section (PDF) , access-date=2011-05-11]
[{{cite web , title=The Open Group Base Specifications Issue 7 IEEE Std 1003.1, 2013 Edition, 2.13. Pattern Matching Notation , url=http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_13 , access-date=2015-10-26 , archive-date=2014-04-27 , archive-url=https://web.archive.org/web/20140427082439/http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_13 , url-status=live ]
[{{cite web , title=Linux Programmer's Manual, GLOB(7) , url=http://man7.org/linux/man-pages/man7/glob.7.html , access-date=2015-10-26 , archive-date=2015-10-31 , archive-url=https://web.archive.org/web/20151031215157/http://man7.org/linux/man-pages/man7/glob.7.html , url-status=live ]
[{{cite web , website=Bash Reference Manual , title=Pattern Matching , url=https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html , access-date=2016-01-11 , archive-date=2016-02-11 , archive-url=https://web.archive.org/web/20160211205104/http://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html , url-status=live ]
[{{cite web , title=Supporting Wildcard Characters in Cmdlet Parameters , url=https://docs.microsoft.com/en-us/powershell/scripting/developer/cmdlet/supporting-wildcard-characters-in-cmdlet-parameters, publisher=Microsoft Developer Network , website=]Microsoft
Microsoft Corporation is an American multinational corporation and technology company, technology conglomerate headquartered in Redmond, Washington. Founded in 1975, the company became influential in the History of personal computers#The ear ...
, date=18 December 2023
[{{cite web , title=LIKE (Transact-SQL) , date=23 May 2023 , url=https://docs.microsoft.com/en-us/sql/t-sql/language-elements/like-transact-sql , access-date=2017-08-01 , archive-date=2017-08-02 , archive-url=https://web.archive.org/web/20170802044711/https://docs.microsoft.com/en-us/sql/t-sql/language-elements/like-transact-sql , url-status=live ]
[The "Advanced Bash-Scripting Guide, Chapter 19.2: Globbing" (Mendel Cooper, 2003) has a concise set of examples of filename globbing patterns.]
[{{cite web , url=http://msdn.microsoft.com/en-us/library/e1w828yy.aspx , title=Wildcard Expansion , publisher=Microsoft Developer Network , year=2013 , access-date=2013-10-16 , archive-date=2014-08-22 , archive-url=https://web.archive.org/web/20140822042708/http://msdn.microsoft.com/en-us/library/e1w828yy.aspx , url-status=live ]
[{{cite web , url=http://dlang.org/phobos/std_path.html#.globMatch , title=std.path - D Programming Language - Digital Mars , publisher=dlang.org , access-date=2014-09-08 , archive-date=2014-09-08 , archive-url=https://web.archive.org/web/20140908203040/http://dlang.org/phobos/std_path.html#.globMatch , url-status=live ]
[{{Cite web , url=https://github.com/isaacs/minimatch , title=isaacs/minimatch , website=GitHub , access-date=2016-08-10 , archive-date=2016-07-28 , archive-url=https://web.archive.org/web/20160728063829/https://github.com/isaacs/minimatch , url-status=live ]
[{{Cite web , url=https://github.com/jonschlinkert/micromatch , title=jonschlinkert/micromatch , website=GitHub , access-date=2017-04-04 , archive-date=2016-02-11 , archive-url=https://web.archive.org/web/20160211020457/https://github.com/jonschlinkert/micromatch , url-status=live ]
[{{cite web , url=http://golang.org/pkg/path/filepath/#Glob , title=Package filepath - The Go Programming Language , publisher=Golang.org , access-date=2011-05-11 , archive-date=2011-05-25 , archive-url=https://web.archive.org/web/20110525035625/http://golang.org/pkg/path/filepath/#Glob , url-status=live ]
[{{cite web , url=http://docs.oracle.com/javase/tutorial/essential/io/fileOps.html#glob , title=File Operations , publisher=Oracle , access-date=2013-12-16 , archive-date=2013-09-20 , archive-url=https://web.archive.org/web/20130920172102/http://docs.oracle.com/javase/tutorial/essential/io/fileOps.html#glob , url-status=live ]
[{{cite web , url=http://hackage.haskell.org/package/Glob-0.7.4/docs/System-FilePath-Glob.html , title=Glob-0.7.4: Globbing library , access-date=2014-05-07 , archive-date=2014-05-08 , archive-url=https://web.archive.org/web/20140508025811/http://hackage.haskell.org/package/Glob-0.7.4/docs/System-FilePath-Glob.html , url-status=live ]
[{{cite web , url=http://perldoc.perl.org/File/Glob.html , title=File::Glob - Perl extension for BSD glob routine , publisher=perldoc.perl.org , access-date=2011-05-11]
[{{cite web , url=http://www.php.net/glob , title=glob - Manual , publisher=PHP , date=2011-05-06 , access-date=2011-05-11 , archive-date=2017-11-13 , archive-url=https://web.archive.org/web/20171113011131/http://php.net/glob , url-status=live ]
[{{cite web , url=https://docs.python.org/library/glob.html , title=10.7. glob — Unix style pathname pattern expansion — Python v2.7.1 documentation , publisher=Docs.python.org , access-date=2011-05-11 , archive-date=2011-05-16 , archive-url=https://web.archive.org/web/20110516205728/http://docs.python.org/library/glob.html , url-status=live ]
[{{cite web , url=http://www.isc.org/sources/devel/func/glob.txt , title='Globbing' library routine , access-date=2011-05-11 , archive-url=https://web.archive.org/web/20071219090708/http://www.isc.org/sources/devel/func/glob.txt , archive-date=2007-12-19]
[{{cite web , url=https://docs.ruby-lang.org/en/master/Dir.html , title=Class: Dir , publisher=Ruby-doc.org , access-date=2011-05-11 , archive-date=2011-05-15 , archive-url=https://web.archive.org/web/20110515140813/http://ruby-doc.org/core/classes/Dir.html#M000629 , url-status=live ]
[{{cite web , url=http://www.tcl.tk/man/tcl8.5/TclCmd/glob.htm , title=TCL glob manual page , access-date=2011-11-16 , archive-date=2011-12-08 , archive-url=https://web.archive.org/web/20111208112250/http://www.tcl.tk/man/tcl8.5/TclCmd/glob.htm , url-status=live ]
[{{Cite web , url=https://github.com/kthompson/glob , title=kthompson/glob , website=GitHub , access-date=2020-11-06 , archive-date=2020-10-26 , archive-url=https://web.archive.org/web/20201026174625/https://github.com/kthompson/glob , url-status=live ]
[{{Cite web , url=https://github.com/dazinator/DotNet.Glob , title=dazinator/dotnet.glob , website=GitHub , access-date=2022-06-22 , archive-date=2022-06-22 , archive-url=https://web.archive.org/web/20220622181350/https://github.com/dazinator/DotNet.Glob , url-status=live ]
C POSIX library
Pattern matching
Unix programming tools