Wildcards in Linux commands
Wildcards provides special characters that makes Linux commands so powerful. Since the shell uses filenames so much, it provides special characters to help user rapidly specify groups of filenames.
Wildcards is also known as globbing which allow you to select filenames based on patterns of characters. Wildcards can be used with any command that accepts filenames as arguments The table below lists the wildcards and what they select:
Wildcard | Meaning |
---|---|
* |
Matches any characters |
? |
Matches any single character |
[characters] |
Matches any characters that is a member of the set characters |
[!characters] |
Matches any characters that is not a member of the set characters |
[[:class:]] |
Matches any characters that is a member of the specified class |
Below is the commonly used character classes:
Characters Class | Meaning |
---|---|
[:alnum:] |
Matches any alphanumeric character |
[:alpha:] |
Matches any alphabetic character |
[:digit:] |
Matches any numeral |
[:lower:] |
Matches any lowercase letter |
[:upper:] |
Matches any uppercase letter |
Examples
Using wildcards makes it possible to construct very sophisticated selection criteria for filenames. Here are some examples of patterns and what they match:
Pattern | Matches |
---|---|
* |
All files |
g* |
Any file beginning with "g" |
b*.txt |
Any file beginning with "g" and ending with ".txt" |
Data??? |
Any file beginning with "Data" followed by exactly three characters |
[abc]* |
All files beginning with either an "a", a "b" or a "c" |
BACKUP.[0-9][0-9][0-9] |
Any file beginning with "BACKUP." followed by exactly three numerals |
[[:upper:]]* |
Any file beginning with an uppercase letter |
[![:digit:]]* |
Any file not beginning with a numeral |
*[[:lower:]123] |
Any file ending with a lowercase letter or the numerals “1”, “2”, or “3” |
Characters Ranges
If you are coming from another Unix-like environment or have been reading some other books on this subject, you may have encountered the [A-Z]
or the [a-z]
character range notations. These are traditional Unix notations and worked in older versions of Linux as well. They can still work, but you have to be very careful with them because they will not produce the expected results unless properly configured.
Latest Post
- Dependency injection
- Directives and Pipes
- Data binding
- HTTP Get vs. Post
- Node.js is everywhere
- MongoDB root user
- Combine JavaScript and CSS
- Inline Small JavaScript and CSS
- Minify JavaScript and CSS
- Defer Parsing of JavaScript
- Prefer Async Script Loading
- Components, Bootstrap and DOM
- What is HEAD in git?
- Show the changes in Git.
- What is AngularJS 2?
- Confidence Interval for a Population Mean
- Accuracy vs. Precision
- Sampling Distribution
- Working with the Normal Distribution
- Standardized score - Z score
- Percentile
- Evaluating the Normal Distribution
- What is Nodejs? Advantages and disadvantage?
- How do I debug Nodejs applications?
- Sync directory search using fs.readdirSync