Skip to main content

Wildcards and Classes in Linux The Art of Pattern Matching

In Linux and Unix-based operating systems, one of the most powerful tools at your disposal is the ability to manipulate files and directories through the command line interface (CLI). One essential feature that makes this possible is the use of "wildcard characters" and "character classes" for pattern matching.

What is a Wildcard Character?

In computing, a wildcard character is a kind of placeholder represented by a single character, like *, ?, or others. In Linux, wildcard characters can replace zero or more characters in a string.

Basic Wildcard Characters

Here are the most commonly used wildcard characters:

PatternMatches
*Any number of characters, including none.
?A single arbitrary character.
[]A range or class of characters.

Examples

  • *: Matches zero or more characters, so *.txt would select all files that have .txt extension.
  • ?: Matches a single character, so ?.txt would match a.txt but not aa.txt.
  • [abc]: Matches a single character that is either a, b, or c.

What is a Character Class?

Character classes in Linux allow you to specify a set or range of characters for pattern matching. Character classes are usually enclosed in square brackets [].

Basic Character Classes

PatternMatches
[abc]Any single a, b, or c character
[a-z]Any single lowercase letter from a to z
[A-Z]Any single uppercase letter from A to Z
[0-9]Any single digit from 0 to 9
[!abc]Any character that is not a, b, or c

Examples

  • [aeiou]: Matches any single vowel.
  • [!aeiou]: Matches any single non-vowel.
  • [0-9]: Matches any single digit.

Combining Wildcards and Character Classes

You can combine wildcard characters and character classes to form more complex patterns. Here are some examples:

PatternMatches
*.txtAll .txt files
file?.txtfile1.txt, filea.txt but not fileaa.txt
[a-c]*.jpgAll .jpg files starting with a, b, or c
*[!0-9].pngAll .png files not ending with a digit
[A-Z]*[0-9]Any file starting with an uppercase letter and ending with a digit

Examples with Commands

Finding Files

To find all .txt files in the current directory:

ls *.txt

Removing Files

To remove all .log files that start with either a, b, or c:

rm [a-c]*.log

Copying Files

To copy all .jpg files that do not end with a digit to another directory:

cp *[!0-9].jpg /some/other/directory

Special Note: Escaping Wildcard Characters

Sometimes you might want to literally search for a file or directory with a name that includes a wildcard character. In such cases, you should escape the wildcard character with a backslash (\).

Example:

ls \*.txt

This will look for a file literally named *.txt rather than all files with a .txt extension.

Conclusion

The use of wildcards and character classes in Linux is a potent feature that can make your life easier, whether you're a system administrator or an end-user. Learning to use these effectively can save you time and effort in many tasks related to file and directory management.

TypeScript Course Instructor Image
TypeScript Course Instructor Image

Time To Transition From JavaScript To TypeScript

Level Up Your TypeScript And Object Oriented Programming Skills. The only complete TypeScript course on the marketplace you building TypeScript apps like a PRO.

SEE COURSE DETAILS

What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for ssoftware developers.

YouTube @cloudaffle