Linux Command Reference ยท Bash Syntax
Bash globbing
Reference for Bash pathname expansion: * ? [] wildcards, POSIX classes, hidden files, globstar **, extglob patterns, nullglob, failglob and dotglob.
Globbing, also called pathname expansion, turns a pattern containing wildcards into the list of existing file names that match it. The shell does the matching, so ls *.txt really runs ls a.txt b.txt c.txt. If nothing matches, Bash passes the pattern through unchanged by default.
Patterns are not regular expressions. They are a smaller language built from *, ? and bracket expressions, with optional extensions switched on by shopt.
ls *.log # any name ending in .log ls file?.txt # file1.txt, fileA.txt ... ls [abc]* # names starting with a, b or c
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
* ? [set] [!set] [[:class:]] ** # needs shopt -s globstar ?(a|b) *(a|b) +(a|b) @(a|b) !(a|b) # needs shopt -s extglob
| Pattern | Matches |
|---|---|
* |
Any string, including empty, but not / and not a leading dot. |
? |
Exactly one character. |
[abc], [a-f] |
One character from the set or range. |
[!abc], [^abc] |
One character not in the set. |
[[:digit:]] |
A POSIX class: alpha, alnum, digit, lower, upper, space, punct, xdigit and others. |
** |
With globstar, any number of directories (and **/ matches zero or more levels). |
?(p), *(p), +(p), @(p), !(p) |
With extglob: zero or one, zero or more, one or more, exactly one, anything except. p is one or more patterns separated by |. |
shopt option |
Effect |
|---|---|
nullglob |
A pattern with no match expands to nothing. |
failglob |
A pattern with no match is an error and the command does not run. |
dotglob |
Leading dots are matched by wildcards. |
nocaseglob |
Case-insensitive matching. |
globstar |
Enables **. |
extglob |
Enables the extended operators. |
Returns: a sorted list of matching names (sorted according to the locale). Errors: with failglob, a pattern that matches nothing aborts the command.
Usage notes
- No match means a literal pattern by default, so
rm *.tmpwith no.tmpfiles tries to delete a file literally named*.tmp. Usenullglobin scripts, or check with a loop. - Quote patterns meant for other programs.
find . -name '*.c'must reachfindunexpanded; unquoted, the shell may expand it first. - Hidden files are excluded by
*. To include them safely use.[!.]* ..?*ordotglob; avoid.*, which also matches.and..in some situations. - Globs expand to separate words even if names contain spaces, so
for f in *.txtis safe whilefor f in $(ls)is not. Always quote"$f"afterwards. - Sorting is locale dependent. Use
LC_ALL=Cfor byte order, with upper case before lower case. !(pattern)is greedy about what it matches, including directories; combine with other tests when deleting.- Very long expansions can exceed the argument limit (
Argument list too long). Usefindwith-exec ... +orxargs. - Glob vs regex:
*in a glob means "any characters", while in a regex it repeats the previous item. For matching text, usegrepor[[ =~ ]]. - Inside
[[ ]]andcase, the same pattern syntax matches strings instead of files.