Wildcards
What if you want to delete all .log files? Or copy all files starting with test_?
You could list them all manually. Or you could use wildcards.
The * Wildcard
* matches any number of characters (including zero):
Common Patterns
*.txt # All .txt files
test_* # Files starting with "test_"
*.tar.gz # All .tar.gz archives
*config* # Files containing "config"
The ? Wildcard
? matches exactly one character:
* vs ?
* = zero or more characters
? = exactly one character
file* matches file, file1, file123
file? matches only file1, fileA (exactly 5 characters)
Character Classes [ ]
Match specific characters:
Ranges
[0-9] # Any digit
[a-z] # Any lowercase letter
[A-Z] # Any uppercase letter
[a-zA-Z] # Any letter
[a-zA-Z0-9] # Any alphanumeric
Negation
[!0-9] # NOT a digit
[^abc] # NOT a, b, or c
Brace Expansion
This is different from wildcards - it's expansion by the shell:
Wildcards vs Braces
Wildcards match existing files. Braces generate strings (files don't need to exist).
ls *.txt # Lists existing .txt files
touch file{1..5} # Creates file1, file2, file3, file4, file5
Practical Examples
Delete All Logs
Copy All Images
Backup Config Files
Move Old Files
List Specific Extensions
Escaping Wildcards
What if you want a literal * or ??
Quotes or backslash prevent expansion.
Hidden Files and *
By default, * doesn't match hidden files:
What does `ls file[0-9][0-9].txt` match?
Key Takeaways
*matches any number of characters?matches exactly one character[abc]matches any one character in the set[0-9]matches character ranges{a,b,c}expands to multiple strings (not a wildcard)*doesn't match hidden files by default- Quote or escape wildcards for literal characters
Next: the powerful find command for searching the filesystem.