The find command and wildcards
Updated · Every command on this page ran in our practice shell
Short answer
find . -name "*.txt" lists every file ending in .txt below the current folder. Add -type f for files only or -type d for folders only. Quote the pattern so the shell passes the * to find instead of expanding it first.
grep searches inside files; find searches for the files themselves. Wildcards like * and ? let you name many files at once. Here's how both work, with examples you can run below each section.
Find files by name
find where -name pattern walks through where and every folder below it, printing paths that match. Start from . to search from the current folder.
learner@shellstreak:~$ find . -name "*.log" ./logs/access.log ./logs/error.log ./logs/old/access-feb.log
Ignore case with -iname
-name is case-sensitive. -iname matches IDEAS.md, ideas.md and Ideas.MD alike.
learner@shellstreak:~$ find notes -iname "IDEAS.md" notes/ideas.md
Only files or only folders with -type
-type f keeps regular files and -type d keeps directories. Combine it with -name to narrow things down further.
learner@shellstreak:~$ find logs -type d logs logs/old
The * wildcard
* matches any number of characters (including none). The shell expands it before the command runs, so ls logs/*.log really runs ls logs/access.log logs/error.log. It doesn't look inside subfolders.
learner@shellstreak:~$ ls logs/*.log logs/access.log logs/error.log
The ? wildcard
? matches exactly one character. beach?.jpg matches beach1.jpg and beach2.jpg, but not beach10.jpg.
learner@shellstreak:~/photos$ ls beach?.jpg beach1.jpg beach2.jpg
Why you quote the pattern in find
Write find . -name "*.log", not find . -name *.log. Without quotes, the shell expands *.log first. If the current folder has one .log file, find only searches for that exact name; if it has two, find errors out.
Count what find finds
find prints one path per line, so piping into wc -l counts the matches.
learner@shellstreak:~$ find logs -name "*.log" | wc -l 3
Check yourself
One real exercise from the “Searching” unit. No account needed.Starting from here (.), find every Markdown file: names ending in .md, anywhere below your home folder.
What's checked: the output of your last command (any order).
Common mistakes
Leaving the pattern unquoted, so the shell expands it before find sees it.
Expecting
ls *.txtto search subfolders. Wildcards only match names in one folder; usefindto go deeper.Putting the folder after the tests: it's
find logs -name "*.log", with the starting point first.Running
rm *.logwithout checking what matches. Runls *.logfirst to see exactly what would go.
Questions
What's the difference between find and grep?+
find matches file **names** and types; grep matches text **inside** files. To find files that contain a word, use `grep -rl word folder`.
What does find . mean?+
The `.` is the current folder: find starts there and works through everything below it.
Do wildcards match hidden files?+
No. In bash, `*` skips names that start with a dot, so `rm dir/*` leaves hidden files behind. `ls -a` shows them. Never run `rm -rf .*`: in older shells `.*` also matches `..`, the parent folder.
Reading is step one. Now make it stick.
Practise this in a five-minute lesson with instant feedback. No account needed.