HOME

TheInfoList



OR:

In
computer programming Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as anal ...
, glob () patterns specify sets of filenames with wildcard characters. For example, the Unix
Bash shell Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been used as the default login shell for most Linux distributions. Bash was o ...
command mv *.txt textfiles/ moves (mv) all files with names ending in .txt from the current directory to the directory textfiles. Here, * is a wildcard standing for "any string of characters except /" and *.txt is a glob pattern. The other common wildcard is the question mark (?), which stands for one character. For example, mv ?.txt shorttextfiles/ will move all files named with a single character followed by .txt from the current directory to directory shorttextfiles, while ??.txt would match all files whose name consists of 2 characters followed by .txt. In addition to matching filenames, globs are also used widely for matching arbitrary strings ( wildcard matching). In this capacity a common interface is fnmatch.


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, multiuser 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, ...
. 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, such as an asterisk (), which can be interpreted as a number of literal characters or an empty string. It is often used in file searches so the full na ...
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 In computer science, a high-level programming language is a programming language with strong abstraction from the details of the computer. In contrast to low-level programming languages, it may use natural language ''elements'', be easier to u ...
. 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 ** Thin-shell structure Science Biology * Seashell, a hard o ...
. 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 both the system- and user-level application programming in ...
: 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 wildmat is a pattern matching library developed by Rich Salz. Based on the wildcard syntax already used in the Bourne shell, wildmat provides a uniform mechanism for matching patterns across applications with simpler syntax than that typically ...
(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 In computing, a hidden folder (sometimes hidden directory) or hidden file is a folder or file which filesystem utilities do not display by default when showing a directory listing. They are commonly used for storing user preferences or preservi ...
; 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
Bash Bash or BASH may refer to: Arts and entertainment * ''Bash!'' (Rockapella album), 1992 * ''Bash!'' (Dave Bailey album), 1961 * '' Bash: Latter-Day Plays'', a dramatic triptych * ''BASH!'' (role-playing game), a 2005 superhero game * "Bash" ('' ...
have functionality allowing users to circumvent this.


Unix-like

On
Unix-like A Unix-like (sometimes referred to as UN*X 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 Unix-li ...
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 ** Thin-shell structure Science Biology * Seashell, a hard o ...
per POSIX tradition. Globbing is provided on filenames at the
command line A command-line interpreter or command-line processor uses a command-line interface (CLI) to receive commands from a user in the form of lines of text. This provides a means of setting parameters for the environment, invoking executables and pro ...
and in
shell script A shell script is a computer program designed to be run by a Unix shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file man ...
s. The POSIX-mandated case statement in shells provides pattern-matching using glob patterns. Some shells (such as the
C shell The C shell (csh or the improved version, tcsh) is a Unix shell created by Bill Joy while he was a graduate student at University of California, Berkeley in the late 1970s. It has been widely distributed, beginning with the 2BSD release of th ...
and
Bash Bash or BASH may refer to: Arts and entertainment * ''Bash!'' (Rockapella album), 1992 * ''Bash!'' (Dave Bailey album), 1961 * '' Bash: Latter-Day Plays'', a dramatic triptych * ''BASH!'' (role-playing game), a 2005 superhero game * "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 computer science, the Kleene star (or Kleene operator or Kleene closure) is a unary operation, either on sets of strings or on sets of symbols or characters. In mathematics, it is more commonly known as the free monoid ...
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 JS libraries and Python's glob.


Windows and DOS

The original
DOS DOS is shorthand for the MS-DOS and IBM PC DOS family of operating systems. DOS may also refer to: Computing * Data over signalling (DoS), multiplexing data onto a signalling channel * Denial-of-service attack (DoS), an attack on a communicat ...
was a clone of
CP/M CP/M, originally standing for Control Program/Monitor and later Control Program for Microcomputers, is a mass-market operating system created in 1974 for Intel 8080/ 85-based microcomputers by Gary Kildall of Digital Research, Inc. Initi ...
designed to work on Intel's
8088 The Intel 8088 ("''eighty-eighty-eight''", also called iAPX 88) microprocessor is a variant of the Intel 8086. Introduced on June 1, 1979, the 8088 has an eight-bit external data bus instead of the 16-bit bus of the 8086. The 16-bit registers a ...
and
8086 The 8086 (also called iAPX 86) is a 16-bit microprocessor chip designed by Intel between early 1976 and June 8, 1978, when it was released. The Intel 8088, released July 1, 1979, is a slightly modified chip with an external 8-bit data bus (allo ...
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 PowerShell is a task automation and configuration management program from Microsoft, consisting of a command-line shell and the associated scripting language. Initially a Windows component only, known as Windows PowerShell, it was made open-so ...
has all the common syntax defined as stated above without any additions. *
COMMAND.COM COMMAND.COM is the default command-line interpreter for MS-DOS, Windows 95, Windows 98 and Windows Me. In the case of DOS, it is the default user interface as well. It has an additional role as the usual first program run after boot (init proc ...
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 (e.g., .txt, .docx, .md). The extension indicates a characteristic of the file contents or its intended use. A filename extension is typically ...
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 operating system for amd64/i686 personal computers intended to be binary-compatible with computer programs and device drivers made for Windows Server 2003 and later versions of Windows. ReactOS has been not ...
(crt/misc/getargs.c) and
Wine Wine is an alcoholic drink typically made from Fermentation in winemaking, fermented grapes. Yeast in winemaking, Yeast consumes the sugar in the grapes and converts it to ethanol and carbon dioxide, releasing heat in the process. Different ...
(msvcrt/data.c) contain a compatible open-source implementation of , the function operating under-the-hood, in their core CRT. * The
Cygwin Cygwin ( ) is a POSIX-compatible programming and runtime environment that runs natively on Microsoft Windows. Under Cygwin, source code designed for Unix-like operating systems may be compiled with minimal modification and executed. The Cygwin in ...
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 computer science, the Kleene star (or Kleene operator or Kleene closure) is a unary operation, either on sets of strings or on sets of symbols or characters. In mathematics, it is more commonly known as the free monoid ...
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 search 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 A proxy auto-config (PAC) file defines how web browsers and other user agents can automatically choose the appropriate proxy server (access method) for fetching a given URL. A PAC file contains a JavaScript function FindProxyForURL(url, host) ...
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# has multiple libraries available through
NuGet NuGet (pronounced "New Get")And The Winner Is, NuGet
haacke ...
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 that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of websites use JavaScript on the client side for webpage behavior, of ...
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 Babel is a name used in the Hebrew Bible for the city of Babylon and may refer to: Arts and media Written works Books * ''Babel'' (book), by Patti Smith * ''Babel'' (2012 manga), by Narumi Shigematsu * ''Babel'' (2017 manga), by Yūgo Ishika ...
and yarn. * Go has a Glob function in the filepath package. *
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
has a Files class containing methods that 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 has pioneered a number of programming lan ...
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 family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offic ...
has both a glob function (as discussed in
Larry Wall Larry Arnold Wall (born September 27, 1954) is an American computer programmer and author. He created the Perl programming language. Personal life Wall grew up in Los Angeles and then Bremerton, Washington, before starting higher education at ...
'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 Guido van Rossum (; born 31 January 1956) is a Dutch programmer best known as the creator of the Python programming language, for which he was the "benevolent dictator for life" (BDFL) until he stepped down from the position on 12 July 2018 ...
, author of the Python programming language, wrote and contributed a glob routine to BSD
Unix Unix (; trademarked as UNIX) is a family of multitasking, multiuser 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, ...
in 1986. There were previous implementations of glob, e.g., in the ex and ftp programs in previous releases of BSD. *
Ruby A 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 ...
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. * 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 search 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, such as an asterisk (), which can be interpreted as a number of literal characters or an empty string. It is often used in file searches so the full na ...
*
Matching wildcards In computer science, an algorithm for matching wildcards (also known as globbing) is useful in comparing text strings that may contain wildcard syntax. Common uses of these algorithms include command-line interfaces, e.g. the Bourne shell or Micro ...


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 {{cite web , title=Linux Programmer's Manual, GLOB(7) , url=http://man7.org/linux/man-pages/man7/glob.7.html {{cite web , website=Bash Reference Manual , title=Pattern Matching , url=https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html {{cite web , title=Supporting Wildcard Characters in Cmdlet Parameters , url=https://docs.microsoft.com/en-us/powershell/developer/cmdlet/supporting-wildcard-characters-in-cmdlet-parameters, publisher=Microsoft Developer Network , website=
Microsoft Microsoft Corporation is an American multinational technology corporation producing computer software, consumer electronics, personal computers, and related services headquartered at the Microsoft Redmond campus located in Redmond, Washi ...
{{cite web , title=LIKE (Transact-SQL) , url=https://docs.microsoft.com/en-us/sql/t-sql/language-elements/like-transact-sql 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 {{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 {{Cite web , url=https://github.com/isaacs/minimatch , title=isaacs/minimatch , website=GitHub , access-date=2016-08-10 {{Cite web , url=https://github.com/jonschlinkert/micromatch , title=jonschlinkert/micromatch , website=GitHub , access-date=2017-04-04 {{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 {{cite web , url=http://docs.oracle.com/javase/tutorial/essential/io/fileOps.html#glob , title=File Operations , publisher=Oracle , access-date=2013-12-16 {{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 {{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 {{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 {{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=http://www.ruby-doc.org/core/classes/Dir.html#M000629 , title=Class: Dir , publisher=Ruby-doc.org , access-date=2011-05-11 {{cite web , url=http://www.tcl.tk/man/tcl8.5/TclCmd/glob.htm , title=TCL glob manual page , access-date=2011-11-16 {{Cite web , url=https://github.com/kthompson/glob , title=kthompson/glob , website=GitHub , access-date=2020-11-06 {{Cite web , url=https://github.com/dazinator/DotNet.Glob , title=dazinator/dotnet.glob , website=GitHub , access-date=2022-06-22 C POSIX library Pattern matching Unix programming tools