Categories
GNU/Linux

Glob Patterns

man 7 glob

Glob Patterns: Globbing Pathnames

Long ago, in Unix V6, the was a program /etc/glob that would expand wildcard patterns.Soon it was built into shell and bash.

Glob Patterns

Glob (Wildcard) Matching

A string is a wildcard pattern if it contains one of the characters:

?  *  [

Globbing is the operation that expands a wildcard pattern into the list of pathnames matching the pattern.

? (not between brackets) matches any single character.
* (not between brackets) matches any string, including empty string.
Glob Patterns Character Classes

[abc]: matches any single character enclosed in the brackets (either a, b or c). The string enclosed cannot be empty hence ‘]’ can be allowed between the brackets.

Glob Patterns Ranges

[a-z]: Two characters separated by a ‘-‘ denote a range.

[A-Fa-f0-9] denote [ABCDEFabcdef0123456789].

Glob Patterns Complementation

[!a-f]: matches a single character that is not matched by removing the first’!’. ie. The negation.

[!]a-]: matches all single characters except ‘]’, ‘a’, ‘-‘

Glob Patterns removing the special meaning

You can remove the special meaning of:

?   *   [    \

By preceding with a backslash, or if it is a shell command enclose in quotes.

Pathnames

Globbing is applied to each part of a path name seperately, the delimiter being ‘/’. The backslash cannot be matched by ‘?’ or ‘*’.

If a filename starts with ‘.’ It needs to be matched explicitly as `rm *` will not remove .profile.

Glob Patterns are Not Regular Expressions

They are similar however

  • Glob patterns match filenames not text
  • Conventions are not the same (regular expressions: ‘*’ means 0 or more of the preceding thing.
  • Regular expressions use ‘^’ as negation, POSIX defines ‘^’ as undefined.

More on Differences

More on Gitignore type:

git help gitignore