Skip to content
linux command-line

Glob

glob wildcard shell bash command-line
Plain English

A glob is a shortcut for matching many file names at once. Typing *.jpg means “every file ending in .jpg.” The shell swaps the pattern for the real list of names before the command runs, so you can copy, move, or delete groups of files in one line. It is simpler than a regular expression and used constantly at the command line.

Technical Definition

Globbing (pathname expansion) is done by the shell, not the command. ls *.log becomes ls app.log error.log before ls runs.

Patterns:

  • *: any string, including empty (does not match a leading .)
  • ?: exactly one character
  • [abc]: one of the listed characters; [0-9] a range; [!abc] or [^abc] none of them
  • {a,b}: brace expansion (not strictly globbing, but used alongside it): file.{txt,md}
  • **: recursive match with shopt -s globstar in Bash

Behavior to know:

  • Hidden files (starting with .) are skipped unless the pattern starts with . or dotglob is set
  • No match: Bash leaves the pattern as literal text by default, so rm *.tmp with no matches tries to remove a file named *.tmp. shopt -s nullglob changes this.
  • Quoting disables globbing: find . -name "*.log" passes the pattern to find instead of letting the shell expand it

Glob vs regex: in a glob * means “anything”; in a regex * means “repeat the previous item.” Do not mix them.

Safety: before running rm with a glob, run ls or echo with the same pattern to see what it expands to.

Source: glob(7) Linux manual page

Using globs safely

$ ls
app.log  error.log  notes.md  photo1.jpg  photo2.jpg  photo10.jpg

$ ls *.log
app.log  error.log
$ ls photo?.jpg
photo1.jpg  photo2.jpg
$ ls photo[0-9]*.jpg
photo1.jpg  photo10.jpg  photo2.jpg

# Preview what a glob expands to before deleting
$ echo rm *.jpg
rm photo1.jpg photo10.jpg photo2.jpg

# Let find do the matching (quoted), recursively
$ find /var/log -name "*.gz" -mtime +30
In the Wild

Globs make bulk file work fast and also make it dangerous. rm -rf * .bak (note the space) deletes everything in the folder, not just backups. The habit that prevents disasters: echo or ls the pattern first, then run the real command. When you need recursion or conditions like age and size, switch to find with a quoted pattern instead of stacking globs.