Wildcards and Globbing¶
Wildcards and globbing allow you to work with multiple files using simple patterns instead of typing each filename individually. They are essential for Linux administration, shell scripting, DevOps automation, and production operations.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand what wildcards are
- Learn how Bash performs globbing
- Use
*,?, and[] - Match files using character ranges
- Use brace expansion
- Combine wildcard patterns
- Use wildcards safely in production
Prerequisites¶
Before starting this lesson, complete:
- Module 1 – Linux Fundamentals
- Module 2 Lessons 1–6
Why Learn Wildcards?¶
Imagine you have thousands of files.
Instead of typing:
You simply type:
One command.
Thousands of files.
This is the power of wildcards.
What is Globbing?¶
Globbing is the process performed by the Bash shell to expand wildcard patterns into matching filenames.
Example:
You type:
Bash converts it to:
before executing the command.
Notice:
The shell expands the pattern, not the ls command.
Common Wildcards¶
| Wildcard | Meaning |
|---|---|
* | Zero or more characters |
? | Exactly one character |
[] | Match one character from a set |
{} | Brace expansion |
[a-z] | Character range |
The * Wildcard¶
Matches zero or more characters.
Example files:
Display all text files:
Output:
Display all log files.
Display every file.
The ? Wildcard¶
Matches exactly one character.
Example files:
Command:
Matches:
Does NOT match:
Character Sets¶
Use square brackets.
Example:
Command:
Output:
Character Ranges¶
Example:
Matches:
Alphabet range:
Matches:
Negation¶
Exclude characters.
Matches files not starting with numbers.
Brace Expansion¶
Brace expansion is not globbing.
It generates multiple strings.
Example:
Creates:
Create files:
Creates:
Alphabet expansion:
Creates:
Combining Wildcards¶
Example:
Matches:
Another example:
Matches:
Does NOT match:
Hidden Files¶
Remember:
does not match hidden files.
Hidden files begin with:
Display hidden files.
Match hidden files.
Using Wildcards with Commands¶
Copy all YAML files.
Delete all log files.
Move all shell scripts.
Count all text files.
Preview Before Deleting¶
Never run:
without checking.
Preview first:
Then:
This simple habit prevents accidental deletions.
Wildcards vs Regular Expressions¶
Many beginners confuse them.
| Wildcards | Regular Expressions |
|---|---|
| Used by Shell | Used by Programs |
| Expand filenames | Match text patterns |
*.txt | .*\.txt |
| Simple | More Powerful |
We'll cover Regular Expressions later in the course.
Real Production Example¶
Backup Kubernetes YAML files.
Delete old logs.
Archive reports.
Move certificates.
Production Perspective¶
DevOps engineers frequently use wildcards.
Examples:
Docker:
Git:
Terraform:
Kubernetes:
Automation becomes much easier with wildcard patterns.
Hands-on Lab¶
Task 1¶
Create a practice directory.
Task 2¶
Create files.
touch app.py
touch app.js
touch app.java
touch notes.txt
touch report.txt
touch server1.log
touch server2.log
touch config.yaml
Task 3¶
List text files.
Task 4¶
List log files.
Task 5¶
Match single character.
Task 6¶
Create multiple files.
Task 7¶
List SQL files.
Task 8¶
Create alphabet files.
Task 9¶
Display only demo files.
Command Deep Dive¶
| Pattern | Description | Example |
|---|---|---|
* | Any number of characters | *.txt |
? | One character | file?.txt |
[] | Character set | file[12].txt |
[a-z] | Character range | [a-f]* |
[!a-z] | Negation | [!0-9]* |
{} | Brace expansion | file{1..5}.txt |
Production Troubleshooting Scenario¶
Scenario
A server contains:
Tasks:
- Display all YAML files.
- Display only application logs.
- Delete only backup files.
- Copy all YAML files to a backup directory.
- Display files beginning with "config".
Possible solutions:
Mini Challenge¶
Create the following files.
project1.txt
project2.txt
project3.txt
server1.log
server2.log
server3.log
config.yaml
docker-compose.yml
deploy.sh
Now perform the following:
- List all
.txtfiles. - List all
.logfiles. - Display only
project1.txttoproject3.txt. - List files beginning with
server. - Create
backup1tobackup5directories using a single command. - Copy all YAML files into a new directory named
configs.
Best Practices¶
- Preview wildcard matches with
lsbefore using destructive commands. - Use quotes when you want to prevent shell expansion.
- Prefer specific patterns over broad ones.
- Combine wildcards with other commands for automation.
- Test wildcard commands in a lab before running them in production.
Common Mistakes¶
❌ Running:
✅ Use:
without checking the current directory.
Always verify first:
❌ Assuming * matches hidden files.
✅ It does not.
Use:
❌ Confusing brace expansion with globbing.
✅ Brace expansion creates filenames.
Globbing matches existing filenames.
Interview Questions¶
Beginner¶
- What is a wildcard?
- What does
*match? - What does
?match? - What is brace expansion?
Intermediate¶
- Explain globbing.
- Why is brace expansion different from globbing?
- How do you match files beginning with "config"?
- How do you match files ending with
.yaml?
Architect Level¶
- How can wildcard misuse cause production incidents?
- Why should engineers preview wildcard matches before deleting files?
- How do wildcards simplify DevOps automation?
Summary¶
In this lesson, you learned:
- Wildcards
- Globbing
*?- Character sets
- Character ranges
- Brace expansion
- Production-safe wildcard usage
Wildcards are one of the most powerful features of the Linux shell. Mastering them will help you work faster, write cleaner scripts, and automate repetitive tasks with confidence.
Key Takeaways¶
- Bash performs wildcard expansion before executing commands.
*matches zero or more characters.?matches exactly one character.[]matches characters from a specified set or range.- Brace expansion generates multiple filenames.
- Always preview wildcard matches before performing destructive operations.
What's Next?¶
In the next lesson, you'll learn:
- Standard Input (stdin)
- Standard Output (stdout)
- Standard Error (stderr)
>>><|tee- Redirecting command output
- Building powerful command pipelines