Filename Expansion
Understanding Filename Expansion with Wildcards
Filename expansion, also known as file globbing, is a powerful feature in command-line shells that allows you to use wildcard characters to match multiple filenames or directory names with a single pattern. This significantly simplifies operations like listing, copying, or deleting files.
Key Concepts in Filename Expansion
- A wildcard character is a special character used to represent one or more characters in a filename or foldername.
- File globbing is the operation that recognizes these wildcard characters and performs the expansion, replacing the pattern with a list of matching files.
Common Wildcard Characters and Their Usage
| Wildcard | Description | Example | Matches | Does Not Match |
|---|---|---|---|---|
* |
Matches 0 or more characters. | ls to* |
to, tom, ton, tow, tommy, tommie |
tata, tea |
? |
Matches exactly 1 character. | ls to? |
tom, tow, ton |
to, tommy, tommie, tata, tea |
[abc] |
Matches any single character within the brackets. | ls [bc]at |
bat, cat |
Bat, Cat, rat |
[a-z] |
Matches any single character within the specified range (case-sensitive). | ls day[1-9] |
day1, day2, ..., day9 |
day11, day |
[!abc] |
Matches any single character that is NOT within the brackets. | ls [!r]at |
bat, cat, Bat, Cat, Rat |
rat |
[!a-z] |
Matches any single character that is NOT within the specified range. | ls day[!1-9] |
day0, days |
day1, ..., day9 |
Benefits of Using Filename Expansion
Leveraging filename expansion can save you a significant amount of typing and reduce the potential for errors when working with numerous files. It's a fundamental skill for efficient command-line usage.
Further Reading
- MDN Web Docs: Regular Expressions (While not identical, shares similar pattern matching concepts)
- Bash Manual: Filename Expansion