Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Concepts

Globs vs regular expressions

Shell globs and regular expressions look alike but are different languages: compare their syntax, who interprets them, and where each is used on Linux.

Linux has two pattern languages that share some characters and almost nothing else. A glob (wildcard pattern) matches file names and is expanded by the shell: *.txt. A regular expression (regex) matches text and is interpreted by a program such as grep: ^[a-z]+\.txt$.

Mixing them up is one of the most common beginner mistakes: grep *.txt does not mean "search in txt files for...", and ls .* does not mean "everything starting with a dot".

Terminal
ls *.txt             # glob: the shell expands it
grep 'error.*' log   # regex: grep interprets it, so quote it

Try it

Bash Same characters, different meaning

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.

Bash Quoting decides who expands

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.

Comparison

Meaning Glob Regex (ERE)
Any single character ? .
Any run of characters * .*
One of a set [abc], [a-z] [abc], [a-z]
Not in a set [!abc] (also [^abc] in Bash) [^abc]
Alternatives {a,b} (Bash brace expansion, not a glob) or @(a|b) with extglob a|b
Zero or more of the previous item *(x) with extglob x*
One or more +(x) with extglob x+
Optional ?(x) with extglob x?
Anchors implicit: whole name must match ^, $ (otherwise matches anywhere)
Literal dot . is literal \.
Hidden files * skips a leading dot no such rule
Path separator * and ? never match / . matches /

The deepest difference is anchoring. A glob always matches the whole name, so *.txt needs the name to end in .txt. A regex finds a match anywhere in the text unless you add ^ and $.

Where each one is used

  • Globs: shell command lines, case patterns, [[ x == pattern ]], find -name, -path, ls, rsync --include, .gitignore, ${var#pattern} and ${var//pattern/x} trimming and replacement.
  • Regexes: grep, sed, awk, [[ x =~ re ]], find -regex, less and vim searches, rename (Perl), pgrep.
  • Hybrid: git pathspecs and tar --exclude accept globs; ls -I takes a glob; egrep takes a regex.

Usage notes

  • The shell expands unquoted globs first. If the pattern matches files, the program sees the file list, not the pattern. If nothing matches, Bash passes the pattern through unchanged (unless nullglob or failglob is set).
  • Quote regexes so the shell does not touch them: grep '^a.*z$' file. A pattern with $ or * unquoted is a bug waiting to happen.
  • Dotfiles: * never matches names starting with . unless shopt -s dotglob is set; .* also matches . and .. in some shells.
  • No recursion in plain globs. ** matches across directories only with shopt -s globstar.
  • Case: both are case-sensitive by default. shopt -s nocaseglob and grep -i change it.
  • Extended globs (shopt -s extglob) add ?(...), *(...), +(...), @(...), !(...), which mimic a few regex operators but remain glob rules.
  • Performance: globbing happens before the command runs and is limited by the argument-length limit (Argument list too long); find and xargs avoid that.