Skip to main content
In this lesson you’ll learn how to select files in a Unix-like shell using absolute and relative paths, the special directory references . and .., and filename globbing (wildcards). Filename globbing lets the shell expand simple patterns into matching filenames so you don’t need to memorize exact names. Key terms: globbing, wildcards, shell patterns, Bash, filename expansion.
This guide covers the most common glob patterns supported by the shell: exact matches, wildcards (*, ?), character sets ([]), POSIX character classes, and brace expansion. Remember: by default, glob patterns do not match names that begin with a dot (.) unless the pattern itself begins with a dot.

Pattern reference

Exact match

To list a specific file by name:

Wildcard: *

  • * matches zero or more characters.
  • Examples:
    • ls * — lists all non-hidden files in the current directory.
    • ls *.txt — lists files ending with .txt.
    • ls log* — lists files starting with log.
    • ls *X* — lists files containing the letter X anywhere in the name.

Wildcard: ?

  • ? matches exactly one character.
  • Examples:
    • ls file? — matches names starting with file followed by one character (e.g., file1).
    • ls ???? — matches any filename exactly four characters long.

Character sets: []

Square brackets match any single character listed inside (OR relationship).
  • ls file[12] — matches file1 or file2.
  • ls file[1-4] — matches file1..file4.
  • ls [abc]* — matches filenames beginning with a, b, or c.
Negation inside brackets: put ! (or ^ in some shells) as the first character to invert the set.
  • ls [!a]* — matches files that do not start with a. Note: dotfiles are excluded unless the pattern begins with a dot.
  • Ranges also work for digits and letters: [0-9], [a-z], etc.

POSIX character classes inside bracket expressions

These are useful for matching character categories portably across locales:
  • [[:digit:]] — any digit (0–9).
  • [[:upper:]] — any uppercase letter.
  • [[:lower:]] — any lowercase letter.
Negate a class using ! inside the outer brackets:
  • ls [![:upper:]]* — files that do not start with an uppercase letter (dotfiles still excluded unless the pattern begins with a dot).

Brace expansion

Brace expansion is performed by the shell before globbing. It lets you generate multiple patterns concisely:
  • ls *.{log,txt} — expands to ls *.log *.txt and lists files ending with .log or .txt.

Hidden files

Files that begin with a dot (.) are hidden by default.
  • ls -a — shows all files including . and ...
  • ls -A — shows all entries except . and ...
  • ls .* — matches names beginning with a dot, but may expand to . and ...
    • To list only dotfiles and avoid . and .., use a safer pattern such as:
      • ls -d .[!.]* ..?*

Hidden file commands quick reference

Example session

Below is a consolidated example that demonstrates the patterns and their outputs in a sample directory:
Hidden files begin with a dot (.). Use ls -a to show all files including hidden ones. If you want to list only dotfiles without seeing . and .., use ls -d .[!.]* ..?* or ls -A.

Summary

  • Use *, ?, [], POSIX character classes, and brace expansion to specify filename patterns.
  • Globbing allows you to describe sets of files concisely without remembering exact filenames.
  • Remember: dotfiles are matched only when the pattern begins with a ..

Watch Video