Q1What does the wildcard * match?
Finding Files — find and Wildcards
This article is part of the Linux Course, where you master practical Linux skills from scratch, from basic commands through to shell scripting.
Learn how to specify many filenames at once with the * ? [] wildcards, and how find searches by name with -name and by type with -type — hands-on in a browser terminal.
Wildcards — * ? []
Wildcards are symbols for specifying part of a filename in bulk.
* matches a run of zero or more characters, ? matches exactly one character, and [] matches any one of the characters inside the brackets.
Combine them with ls, like ls *.txt, to list only the names that match.
* matches any run, ? matches one character, and [] matches any one character inside the brackets.| Symbol | What it matches | Example | Matching files |
|---|---|---|---|
* | A run of zero or more characters | *.txt | a.txt note.txt |
? | Exactly one character | log?.txt | log1.txt (log10.txt doesn't match) |
[...] | Any one character inside the brackets | log[12].txt | log1.txt log2.txt |
[a-c] | Any one character in the range | f[a-c].txt | fa.txt fb.txt fc.txt |
touch a.txt b.txt note.log # create sample files
ls *.txt # a.txt b.txt
ls *.log # note.log
ls ?.txt # a.txt b.txt (one char + .txt)
One character or a choice — ? and []
? always matches exactly one character, so use it when you want to fix the number of characters.
[] matches any one of the characters written inside the brackets, so [12] is 1 or 2, and you can write a range like [a-c].
They narrow targets more finely than *.
touch log1.txt log2.txt log9.txt logA.txt # create sample files
ls log?.txt # the 4 with one trailing char
ls log[12].txt # only log1.txt log2.txt
Searching by Condition — find -name / -type
find walks recursively below a starting directory and searches for things that match a condition.
With find start -name 'pattern' you condition on the name, and with -type f (file) or -type d (directory) you condition on the type.
Use it to find files by name when they're buried deep in the hierarchy.
| Condition | Meaning | Example |
|---|---|---|
-name 'pattern' | Name matches the pattern | find . -name '*.log' |
-type f | Regular files only | find . -type f |
-type d | Directories only | find . -type d |
-name filters by name pattern, -type f by file, and -type d by directory.mkdir -p tree/sub # create the sample tree
touch tree/a.txt tree/sub/b.txt # place files in the hierarchy
find tree -name '*.txt' # search .txt under tree recursively
find tree -type d # directories under tree only
Quote the find pattern
Wrap the '*.txt' in find . -name '*.txt' in single quotes.
Without quotes, the shell expands * first, which can target something other than you intended.
The basic rule is to quote the -name pattern and pass it to find itself.
Knowledge Check
Answer each question one by one.
Q2Which files does ls log[12].txt match?
Q3What is shown when you run find tree -type d?