đź“ť Edit page
âž• Add page
Globbing
How to match paths using 'glob' patterns
Globbing is a programming concept that involves the use of wildcards and special characters to match and filter. Glob patterns are similar to regex patterns, but simpler and limited in scope.
This guide is based on Bash and ZSH.
Resources
- GNU/Linux Command-Line Tools Summary: Wildcards
- Bash: globbing
- Wikipedia: glob (programming)
- Linux Programmer’s Manual: GLOB(7)
- Globs Bash guide.
Some links copied from begin/globbing
Basics
Wildcard matches are used alongside literals.
Basic wildcards:
*
- Match any character zero or more times.**
- Match any character zero or more times, including/
unlike the others.?
- Match any single character one time.[...]
- Match any of the characters. e.g.[abc]
,[123]
[X-Y]
- Match a range. e.g.[0-9a-z]
,[\w]
Example:
*/*
- will matchfoo/bar
.*
will match all files and directories in the current directory.echo * ls * for P in *; do echo $P; done
Advanced
Wildcard expansion can be done. This can be previewed with echo
but might be used with ls
.
echo foo/{bar,baz}
# => foo/bar foo/baz
POSIX character classes
You can also use named ranges, such as:
[::alpha::]
for any letter.[::alnum::]
for any letter or number.[::lower::]
for any lowercase character.
e.g.
Match a1
but not aa
with:
[[:alpha:][:digit:]]
Match hidden characters
Bash 4
shopt -s dotglob
ZSH
setopt dotglob
Globstar
See the Globstar section.