Wildcards and character classes in Linux explained ↓🐧
🐧 What are wildcards?
Wildcards are characters or groups of characters that enable you to create a pattern for searching or matching text on strings, filenames, or directories. Wildcards are a cool shell feature that makes most Linux commands extremely powerful.
Wildcards are characters or groups of characters that enable you to create a pattern for searching or matching text on strings, filenames, or directories. Wildcards are a cool shell feature that makes most Linux commands extremely powerful.
Using wildcards (also known as globbing) allows you to select filenames or directory names based on character patterns.
🐧 What are character classes?
A character class is an improvement to the wildcard concept. Instead of matching any character at any position, we can list the characters to be matched.
A character class is an improvement to the wildcard concept. Instead of matching any character at any position, we can list the characters to be matched.
The list of characters is surrounded by square bracket metacharacters ([]), each of which can only occupy one position.
Here is the fundamental set of wildcards, characters classes and what they do: ↓
🐧 Wildcards that are frequently used:
* - matches any characters.
? - Matches any single character.
[] - matches a range of characters.
* - matches any characters.
? - Matches any single character.
[] - matches a range of characters.
🐧 Character classes that are frequently used:
[characters] - matches any character that is a member of the set characters.
[!characters]- matches any character that is not a member of the set characters.
[characters] - matches any character that is a member of the set characters.
[!characters]- matches any character that is not a member of the set characters.
[[:class:]] - Matches any character that is a member of the specified class.
[:alnum:] - matches any alphanumeric character.
[:alpha:] - matches any alphabetic character.
[:digit:] - matches any numeral.
[:alnum:] - matches any alphanumeric character.
[:alpha:] - matches any alphabetic character.
[:digit:] - matches any numeral.
[:lower:]- matches any lowercase letter.
[:upper:] - matches any uppercase letter.
Using wildcards allows you to create advanced and powerful filename selection criteria. Here are some examples of patterns and what they match. ↓
[:upper:] - matches any uppercase letter.
Using wildcards allows you to create advanced and powerful filename selection criteria. Here are some examples of patterns and what they match. ↓
* - this will match all the files
b* - this will match any file beginning with b
d*.png - this will match any file beginning with d followed by any characters and ending with .png
log???? - this will match any file beginning with log followed by exactly four characters.
b* - this will match any file beginning with b
d*.png - this will match any file beginning with d followed by any characters and ending with .png
log???? - this will match any file beginning with log followed by exactly four characters.
[abc]* - this will match any file beginning with either an a, b or c.
[[:upper:]]* - this will match any file beginning with an uppercase letter.
[[:lower:]]* - this will match any file beginning with an lowercase letter.
[[:upper:]]* - this will match any file beginning with an uppercase letter.
[[:lower:]]* - this will match any file beginning with an lowercase letter.
[[:digit:]]* - this will match any file beginning with a numeral.
[![:digit:]]* - this will match any file not beginning with a numeral.
[![:digit:]]* - this will match any file not beginning with a numeral.
Wildcards and character classes can be used with any Linux command that takes filenames as arguments, such as ls, grep, and others. So feel free to experiment with various Linux commands.
This information should be sufficient to help you understand the Linux wildcard characters, character classes and how to use them.
That's all! Thank you for getting this far. I hope you find this thread useful.
That's all! Thank you for getting this far. I hope you find this thread useful.
If you found this thread valuable:
1. Toss us a follow for more daily threads on Linux, sysadmin and devops → @linuxopsys
2. Like and RT the first tweet so other Linux folks can find it too.
1. Toss us a follow for more daily threads on Linux, sysadmin and devops → @linuxopsys
2. Like and RT the first tweet so other Linux folks can find it too.
Loading suggestions...