Back to course

27. Advanced Wildcards: Character Sets (`[]`) and Negation

Linux Basics: From Zero to CLI Hero

Finer Control Over Patterns

Character Sets ([...])

Square brackets match any single character listed inside the brackets.

Example 1: Matching specific letters

bash $ ls [abc]file.txt

Matches afile.txt, bfile.txt, or cfile.txt

Example 2: Matching a range of numbers

bash $ rm report[1-9].pdf

Removes reports 1 through 9 (report1.pdf, report9.pdf)

Negation ([^...])

Placing a caret (^) inside the brackets negates the set, matching any character not listed.

bash $ ls *[^0-9].log

Matches log files whose name does NOT end with a digit.

Wildcards are applied by the shell before the command runs. The shell expands the pattern into a list of matching file names, and then passes that list to the command (cp, rm, ls, etc.).