Lesson 7 of 29 ↓
- 1.Rhel 9 Administration Guide
- 2.Ubuntu Boot Process Guide
- 3.Linux Basic Commands Guide
- 4.Comprehensive Unix Shells Guide
- 5.Mastering Vim Deep & Practical Guide
- 6.Magic Numbers in Linux Deep Systems Guide
- 7.Wildcards & Globbing in Linux Deep Cli Guide
- 8.Linux Users & Groups Deep Administration Guide
- 9.Journaling in Linux Filesystems Deep Systems Guide
- 10.Getopt & Getoptlong Deep System-level Option Parsing Guide
- 11.Su Command in Linux Complete Guide with Environment Variables
- 12.Linux File Permissions and Ownership Complete Practical Guide
- 13.Ubuntu Package Management Apt Complete Guide
- 14.Bash Scripting Basics
- 15.Linux File System Essentials
- 16.Process and Thread Management
- 17.Session 10 : Introduction to Networking & the Application Layer
- 18.Linux Networking Cli & Introduction to Yocto for Machine Routing
- 19.Comprehensive Guide to Ssh (secure Shell)
- 20.Logical Volume Management (lvm) Guide
- 21.C Build Process & Libraries Guide
- 22.Archiving and Compression in Linux
- 23.Linux Package Managers: a Detailed Guide
- 24.Misra-c Guidelines Detailed Cheat Sheet
- 25.Mqtt Server Setup (pc as Server)
- 26.Detailed Explanation of Tty and Pts in Linux
- 27.Vim Cheat Sheet
- 28.Difference Between apt and apt-get
- 29.Difference Between apt Upgrade and apt Full-upgrade
3 min read
Wildcards & Globbing in Linux Deep Cli Guide
A deep, practical, and system-level guide to understanding wildcards (globbing) in the Linux Command Line Interface. This guide goes far beyond basic usage to explain how globbing really works, how shells differ, pitfalls, performance, and professional shell workflows.
What Wildcards really Are
Wildcards are pattern-matching characters expanded by the shell itself, not by commands like ls, cp, or rm.
Example:
ls *.txtWhat actually happens:
shell expands *.txt → file1.txt file2.txt
ls file1.txt file2.txt⚠ Commands never see wildcards — they only receive expanded filenames.
Globbing vs Regular Expressions
| Feature | Wildcards (Globs) | Regex |
|---|---|---|
| Expanded by | Shell | Program |
| Syntax | Simple | Complex |
| Recursive | Limited | Yes |
| Context | Filenames | Text content |
Example confusion:
ls .*txt # glob
ls | grep .*txt # regexExpansion Order (very Important)
Shell expansion happens in a defined order:
-
Brace expansion
{} -
Tilde expansion
~ -
Parameter expansion
$VAR -
Command substitution
$( ) -
Pathname expansion (globbing)
-
Quote removal
This affects correctness and safety.
Types of Wildcards (deep Dive)
Asterisk *
-
Matches zero or more characters
-
Does NOT cross directory separators (
/)
*.log
backup*Question Mark ?
- Matches exactly one character
file?.txt # file1.txt ✅ | file10.txt ❌Character Classes [ ]
file[1-3].txt
file[a-z].txtAdvanced:
file[!0-9].txt # NOT digitsBrace Expansion {} (not a Wildcard!)
Brace expansion generates strings before globbing.
echo file{1,2,3}.txtOutput:
file1.txt file2.txt file3.txtRanges:
echo {a..d}
echo {01..10}Advanced Globbing Patterns
Enable extended globbing:
shopt -s extglobPatterns:
*.!(txt) # anything except txt
*@(jpg|png)Requires Bash or Zsh.
Hidden Files & Dotfiles
-
*✅ does NOT match.hidden -
.*✅ matches dotfiles -
.*⚠ also matches.and..
Safe pattern:
ls .[^.]*Recursive Globbing **
Enable:
shopt -s globstarExample:
ls **/*.cEquivalent (conceptually):
find . -name "*.c"Wildcards with Common Commands
cp *.txt /backup/
rm report[0-9].txt
mv *.log old_logs/
chmod +x *.shAlways test first:
echo rm *.logWildcards vs find
| Use Case | Wildcards | find |
|---|---|---|
| Recursive | ❌ | ✅ |
| Size/time filters | ❌ | ✅ |
| Performance (small dirs) | ✅ | ❌ |
Best practice:
-
Use globs for simple cases
-
Use
findfor complex logic
Quoting & Escaping
Disable Expansion Completely:
echo "*.txt"Escape a Single Wildcard:
echo \*.txtWhy important:
rm "$file"Safety Techniques (very Important)
Dry Run Everything:
echo rm *.logFail on No Matches:
shopt -s failglobPrevent Dangerous Deletes:
rm -- -file.txtPerformance Considerations
-
Globbing happens before execution
-
Large directories = memory expansion cost
-
Use
findfor thousands of files
Shell limits:
getconf ARG_MAXShell Differences
| Feature | sh | bash | zsh |
|---|---|---|---|
| Brace expansion | ❌ | ✅ | ✅ |
Recursive ** |
❌ | ✅ | ✅ |
| Extended glob | ❌ | ✅ | ✅ |
| Safer defaults | ❌ | ❌ | ✅ |
Real-world Use Cases
Cleanup Logs:
rm /var/log/*.{1,old}Project Setup:
mkdir -p app/{src,bin,docs,tests}Batch Rename Preview:
echo mv *.jpeg *.jpgCommon Mistakes
❌ Believing commands interpret wildcards ❌ Forgetting hidden files ❌ Mixing regex with glob syntax ❌ Unquoted variables
Exercises
Beginner:
ls *.txtIntermediate:
find . -name "*.log" -mtime -3Advanced:
shopt -s globstar extglob
ls **/*.!(*.tmp)✅ This guide is designed for Linux users, sysadmins, DevOps engineers, and shell scripters.
Understanding globbing gives you precision, safety, and speed at the command line.
Happy hacking 🐧
Comments
Loading the discussion…