Wildcards and globbing are pattern matching features in Linux that allow you to select multiple files and directories using special characters. The shell expands these patterns before passing them to commands, making file operations more efficient and flexible.
Key Concepts
- Wildcard: Special characters that represent one or more characters in filenames
- Globbing: The process of expanding wildcard patterns into matching filenames
- Pattern Matching: Using wildcards to match files based on naming patterns
- Shell Expansion: How the shell converts patterns into actual filenames before running commands
Wildcard Characters
* - Matches zero or more characters
? - Matches exactly one character
[...] - Matches any single character in brackets
[!...] or [^...] - Matches any character NOT in brackets
{...} - Matches any of the comma-separated patterns
Character Classes
[a-z] - Any lowercase letter
[A-Z] - Any uppercase letter
[0-9] - Any digit
[a-zA-Z] - Any letter
[[:alpha:]] - Any alphabetic character
[[:digit:]] - Any numeric character
[[:alnum:]] - Any alphanumeric character
Practical Examples
Example 1: Basic asterisk wildcard
|
|
Matches all files ending in .txt or starting with “file”
Example 2: Question mark wildcard
|
|
Matches exactly one character in the specified position
Example 3: Character ranges
|
|
Matches files with characters in the specified range
Example 4: Character exclusion
|
|
Matches files NOT containing specified characters
Example 5: Brace expansion
|
|
Creates multiple directories or matches multiple patterns
Example 6: Complex patterns
|
|
Combines multiple wildcard types for precise matching
Use Cases
- File listing: Quickly view files matching patterns
- Batch operations: Apply commands to multiple files
- File cleanup: Remove files matching specific patterns
- Backup scripts: Select files based on naming conventions
- Log analysis: Process log files with similar names
Related Commands
find - Search for files using patterns
locate - Find files using database search
grep - Search text using patterns (regex)
rename - Batch rename files using patterns
Tips & Troubleshooting
Escaping Wildcards
|
|
Use backslash or quotes to treat wildcards literally
Hidden Files
|
|
Dot files require explicit wildcard patterns
Case Sensitivity
|
|
Linux filesystems are case-sensitive by default
Empty Matches
|
|
Control behavior when patterns don’t match files
Performance Notes
- Wildcards are expanded by the shell, not commands
- Large directories may slow wildcard expansion
- Use
findfor complex searches across directories - Quote patterns when passing to scripts to prevent premature expansion