How-to: Wildcards

Wildcards allow pattern matching within both Regular Expressions and in Globbing.

Bash performs filename expansion on unquoted command-line arguments.

Any command that uses quotes e.g. echo "hello" or echo '*' will echo the text.
Without quotes echo * will perform globbing and echo a list of files in the current directory.

Any character that appears in a pattern, other than the special pattern characters described below, matches itself.
The NUL character can not occur in a pattern. The special pattern characters must be quoted if they are to be matched literally.

The special pattern characters have the following meanings:

* Matches any string, including the null string.

? Matches any single character.

[...] Matches any one of the enclosed characters.

All uppercase alphabets are defined by the range as, [:upper:] or [A-Z].
All lowercase alphabets are defined by the range as, [:lower:] or [a-z].
All numeric digits are defined by the range as, [:digit:] or [0-9].
All uppercase and lower alphabets are defined by the range as, [:alpha:] or [a-zA-z].
All uppercase alphabets, lowercase alphabet and digits are defined by the range as, [:alnum:] or [a-zA-Z0-9]

A-C A pair of characters separated by a minus sign denotes a range; any character lexically between those two characters, inclusive, is matched.

{ pattern,pattern } Used to match filenames with more than one globbing pattern. Each pattern is separated by a comma.
so ls {*.doc,*.docx} is equivalent to ls *.doc, followed by ls *.docx

example mv photos/{IMG,img}001.jpg will evaluate as mv photos/IMG001.jpg img001.jpg

To prevent any file named with dashes (-rf) being interpreted as a commnd option, it is a good idea to use /some/path/* (or ./* for the current directory) rather than just a bare *

If the first character following the '[' is a '!' or a '^' then any character not enclosed is matched. A '-' can be matched by including it as the first or last character in the set. A ']' can be matched by including it as the first character in the set. Within '[' and ']', character classes can be specified using the syntax [:class:], where class is one of the following classes defined in the POSIX 1003.2 standard:

alnum   alpha   ascii   blank   cntrl   digit   graph   lower
print   punct   space   upper   xdigit

A character class matches any character belonging to that class. Within '[' and ']', an equivalence class can be specified using the syntax [=c=], which matches all characters with the same collation weight (as defined by the current locale) as the character c. Within '[' and ']', the syntax [.symbol.] matches the collating symbol symbol.

If the extglob shell option is enabled using the shopt builtin, several extended pattern matching operators are recognized. In the following description, a pattern-list is a list of one or more patterns separated by a '|'. Composite patterns can be formed using one or more of the following sub-patterns:

?(pattern-list) Matches zero or one occurrence of the given patterns.

*(pattern-list) Matches zero or more occurrences of the given patterns.

+(pattern-list) Matches one or more occurrences of the given patterns.

@(pattern-list) Matches exactly one of the given patterns.

!(pattern-list) Matches anything except one of the given patterns.

Related Linux commands

Bash shell variables
Unix Wildcards Gone Wild - Simple 'exploits' using carefuly named files to trigger options in a bash command when called via a wildcard.
Windows equivalent commands: Wildcards.


 
Copyright © 1999-2024 SS64.com
Some rights reserved