Learn by reading through in order

Finding Files — find and Wildcards

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.

What each wildcard matches
*.txtfile?.logdata[12].csvall files ending in .txtone character after filedata1.csv or data2.csv
* matches any run, ? matches one character, and [] matches any one character inside the brackets.
SymbolWhat it matchesExampleMatching files
*A run of zero or more characters*.txta.txt note.txt
?Exactly one characterlog?.txtlog1.txt (log10.txt doesn't match)
[...]Any one character inside the bracketslog[12].txtlog1.txt log2.txt
[a-c]Any one character in the rangef[a-c].txtfa.txt fb.txt fc.txt
touch a.txt b.txt note.log         # create material files
ls *.txt                           # a.txt b.txt
ls *.log                           # note.log
ls ?.txt                           # a.txt b.txt (one char + .txt)

① Create three files with different extensions using touch a.txt b.txt note.log.

② Combine a wildcard with ls to list only the files ending in .txt.

③ Then list only the files ending in .log, and confirm you can narrow down by name. (Run it correctly and an explanation will appear.)

Linux console
0 / 3 completed
Loading Linux Terminal...

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 material
ls log?.txt                                  # the 4 with one trailing char
ls log[12].txt                               # only log1.txt log2.txt

① Create four sequentially-named files with touch log1.txt log2.txt log9.txt logA.txt.

② Combine ls with ? to list files where log is followed by one character and ends in .txt.

③ Use [] to narrow down to just log1.txt and log2.txt. Then confirm a range like [1-2] narrows to the same 2 files.

Linux console
0 / 3 completed
Loading Linux Terminal...

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.

ConditionMeaningExample
-name 'pattern'Name matches the patternfind . -name '*.log'
-type fRegular files onlyfind . -type f
-type dDirectories onlyfind . -type d
find conditions and what they match
find . -name '*.txt'find . -type ffind . -type ditems named .txtfiles onlydirectories only
-name filters by name pattern, -type f by file, and -type d by directory.
mkdir -p tree/sub                  # create the material 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.

① Create a two-level directory with mkdir -p tree/sub.

② Place files in both the parent and the subdirectory with touch tree/a.txt tree/sub/b.txt.

③ Pass tree to find as the starting point and recursively search for .txt files by the name condition.

④ Then list only files by the type condition (find tree -type f), also list only directories, and compare the three results.

Linux console
0 / 5 completed
Loading Linux Terminal...
QUIZ

Knowledge Check

Answer each question one by one.

Q1What does the wildcard * match?

Q2Which files does ls log[12].txt match?

Q3What is shown when you run find tree -type d?