Wildcards: pick many files at once
Instead of naming every toy one by one, you can say "all the red ones". A wildcard is a special character that stands for "anything", so one short command can work on lots of files.
- Before this course: Command Line Basics (course 7.11). There you typed one file name at a time. Wildcards let you match many names with one pattern.
- On Mac and Linux the common wildcards are
*(any number of characters, including none),?(exactly one character) and[ab]or[0-9](one character from a set or range). - On Mac and Linux it is the shell, not the program, that expands the pattern, per the Bash manual (it calls this filename or pathname expansion). So
ls *.txtbecomesls a1.txt a2.txt notes.txtbeforelseven runs. - In PowerShell, wildcard matching is done by the cmdlet receiving the pattern, per Microsoft's wildcard help.
Get-ChildItem *.txtworks the same way for you as a user. - Quotes matter. A file name with a space, such as
my file.txt, must be quoted:ls "my file.txt". Without quotes the shell sees two names. - A wildcard cannot see inside files; it only matches names. Hidden files (names starting with a dot) are normally not matched by
*in Bash and zsh. - Safety: a wildcard multiplies a command.
rm .txtdeletes every match. Always runls .txtfirst to see what the pattern matches, then changelsto the real command, and preferrm -ior-WhatIf(see course 7.11).
| Pattern | Matches | Example files |
|---|---|---|
*.txt | any name ending in .txt | a1.txt, notes.txt |
a?.txt | a, one character, .txt | a1.txt, a2.txt (not ab1.txt) |
[ab]1.txt | a1.txt or b1.txt | a1.txt, b1.txt |
photo[0-9].jpg | photo plus one digit | photo1.jpg, photo2.jpg |
- Open your terminal and
cdinto yoursandboxpractice folder. Runpwdto be sure. - Type
ls *.txtand press Enter. Only the .txt files are listed. - Type
ls a?.txt. Only names with exactly one character between a and .txt appear. - Type
ls [ab]1.txtand thenls photo[0-9].jpg. Compare with the table above. - If you have a file with a space in its name, type
ls "my file.txt"and then try it without quotes to see the error. - PowerShell: use
Get-ChildItem .txt(orls .txt), andGet-ChildItem a?.txt. In PowerShell[ab]1.txtalso works as a set. - Check: before using any wildcard with
rmorRemove-Item, you ran the same pattern withlsorGet-ChildItemfirst and the list was exactly what you expected.
Videos by other creators on YouTube, for reference. They are not made by or affiliated with The Future Market.
Read the video script
[Host] "A wildcard is a stand-in character. One pattern can match many files."
[Text on screen: * ? [ab]] "Star means anything, question mark means exactly one character, brackets mean one from a set."
[Screen: ls *.txt listing three files] "ls star dot txt lists every file ending in dot txt."
[Text on screen: Quote names with spaces] "If a name has a space, put it in quotes."
[Host] "Always preview a wildcard with ls before you use it with a delete command."
In your sandbox, create a1.txt, a2.txt, b1.txt, photo1.jpg and photo2.jpg. Write the pattern that matches (1) only the photos, (2) only a1 and b1, (3) only a1 and a2. Test each with ls and write down the output.
ClaudeStuck? Copy a ready-made prompt for Claude
Copy this, open Claude, paste it, and Claude will walk you through the lesson step by step.
For a grown-up: Claude is for adults (18+). Copy this, paste it into Claude, and use the answer to explain the lesson to your child.
I'm learning "Command Line 2: files, search, pipes and scripts" on The Future Market. Lesson 1: "Wildcards: pick many files at once". What the lesson teaches: - Before this course: Command Line Basics (course 7.11). There you typed one file name at a time. Wildcards let you match many names with one pattern. - On Mac and Linux the common wildcards are (any number of characters, including none), ? (exactly one character) and [ab] or [0-9] (one character from a set or range). - On Mac and Linux it is the shell, not the program, that expands the pattern, per the Bash manual (it calls this filename or pathname expansion). So ls .txt becomes ls a1.txt a2.txt notes.txt before ls even runs. - In PowerShell, wildcard matching is done by the cmdlet receiving the pattern, per Microsoft's wildcard help. Get-ChildItem .txt works the same way for you as a user. - Quotes matter. A file name with a space, such as my file.txt, must be quoted: ls "my file.txt". Without quotes the shell sees two names. - A wildcard cannot see inside files; it only matches names. Hidden files (names starting with a dot) are normally not matched by in Bash and zsh. - Safety: a wildcard multiplies a command. rm .txt deletes every match. Always run ls .txt first to see what the pattern matches, then change ls to the real command, and prefer rm -i or -WhatIf (see course 7.11). The exercise I'm working on: In your sandbox, create a1.txt, a2.txt, b1.txt, photo1.jpg and photo2.jpg. Write the pattern that matches (1) only the photos, (2) only a1 and b1, (3) only a1 and a2. Test each with ls and write down the output. I'm stuck. Please: 1. Explain the main idea in plain, simple words with one everyday example. 2. Walk me through the exercise one small step at a time. Ask me what I've done so far before giving the next step. 3. Don't just give me the final answer. Help me do it myself, and check my work when I share it.
I'm a parent helping my child (about 8 years old) with a lesson called "Wildcards: pick many files at once" from the course "Command Line 2: files, search, pipes and scripts". Here's what the lesson says: Instead of naming every toy one by one, you can say "all the red ones". A wildcard is a special character that stands for "anything", so one short command can work on lots of files. The activity: In your sandbox, create a1.txt, a2.txt, b1.txt, photo1.jpg and photo2.jpg. Write the pattern that matches (1) only the photos, (2) only a1 and b1, (3) only a1 and a2. Test each with ls and write down the output. Please explain this so an 8-year-old understands: short sentences, a fun everyday example, and no hard words. Then give me 3 simple questions I can ask my child to check they understood, and a way to do the activity together, one tiny step at a time.
Sources
- GNU Bash manual, Filename Expansion: www.gnu.org/software/bash/manual/bash.html
- Microsoft Learn, about_Wildcards: learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/
Finding files with find and Get-ChildItem
This lesson is part of full access: every course and every level for one payment.
Searching inside files with grep and Select-String
This lesson is part of full access: every course and every level for one payment.
Pipes: chaining small commands together
This lesson is part of full access: every course and every level for one payment.
Redirection: saving output and hiding noise
This lesson is part of full access: every course and every level for one payment.
Environment variables and PATH
This lesson is part of full access: every course and every level for one payment.
Your first shell script, safely
This lesson is part of full access: every course and every level for one payment.
Final project
This lesson is part of full access: every course and every level for one payment.
Quiz
What does
*.txtmatch?Every name that ends in .txt (in the folder you are in).
Who expands a wildcard like
*.txton Mac and Linux?The shell, before the command runs.
What is the difference between
findandgrep?findlooks for files by name or type;greplooks for text inside files.Why quote
".txt"infind . -name ".txt"?So
findreceives the pattern instead of the shell expanding it first.What does
grep -v ERROR app.logshow?Lines that do not contain ERROR.
What is the PowerShell tool that works like grep?
Select-String.What does a pipe
|do?It sends the output of one command into the next command as its input.
What is the difference between
>and>>?>overwrites the file;>>adds to the end.What does
2> /dev/nulldo?It throws away error messages (PowerShell:
2> $null).What is PATH?
A list of folders searched for programs when you type a command.
What does
chmod +x script.shdo?It makes the script file executable so you can run it with
./script.sh.Why should a script that changes files have a preview mode?
So you can check what it would do before it really does it.
Final exam
This lesson is part of full access: every course and every level for one payment.
Certificate
This lesson is part of full access: every course and every level for one payment.