Wildcards

Published

2025-06-25

Caution

This section is under development. Thank you for your patience.

Wildcards (or glob patterns) can be used with commands to match filenames, paths, or filter text (ls, cp, mv, rm, etc.). The shell expands arguments and wildcards into a list of files or directories that match the pattern.

Asterisk: *

* is a wildcard for matching zero or more characters.

Example

List all files in the data/ directory that end with .md:

ls data/*.md
# data/README.md

Example

List all top-level files starting with w and ending in .tsv in data/

ls data/w*.tsv
# data/wu_tang.tsv

Example

Print the top ten lines of the pwrds .csv file in data/raw/ via *:

head data/raw/p*.csv
# password,rank,strength,online_crack
# password,1,8,6.91 years
# 123456,2,4,18.52 minutes
# 12345678,3,4,1.29 days
# 1234,4,4,11.11 seconds
# qwerty,5,8,3.72 days
# 12345,6,4,1.85 minutes
# dragon,7,8,3.72 days
# baseball,8,4,6.91 years
# football,9,7,6.91 years

Question Mark: ?

? is the wildcard for matching exactly one character.

Example

ls myfile?.txt lists files like myfile2.txt, but not myfile.txt and my file 3.txt:

ls myfile?.txt
# myfile2.txt

Example

List five-character names indata/raw/ ending with .csv:

ls data/raw/?????.csv
# data/raw/pwrds.csv
# data/raw/trees.csv

Example

List any four-character names indata/raw/ ending with s.csv:

ls data/raw/????s.csv
# data/raw/pwrds.csv
# data/raw/trees.csv

Square brackets: []

[abc]: Matches any one character listed (a, b, or c).

Example

[a-z]: match the top-level TSV files starting with m, p, or t in data/:

ls data/[mpt]*.tsv
# data/music_vids.tsv
# data/pwrds.tsv
# data/trees.tsv

Example

Matches files beginning with p or t and ending in .tsv in data/:

ls data/[pt]*.tsv
# data/pwrds.tsv
# data/trees.tsv

Example

Matches ajperlis_epigrams.txt (but excludesroxanne.txt) in data/raw/:

ls data/raw/[!r]*.txt
# data/raw/ajperlis_epigrams.txt

Example

Match any one character in range (a to p).

ls data/[a-p]*
# data/music_vids.tsv
# data/pwrds.tsv

In the next section we’ll cover how to use special characters to enhance matching and searching text with tools like grep, cat, head, awk. etc.

See a typo, error, or something missing?

Please open an issue on GitHub.