Menu
Coddy logo textTech

Bracket Wildcards

Part of the Fundamentals section of Coddy's Terminal journey — lesson 27 of 82.

The bracket wildcard [] matches exactly one character from a specific set or range of characters that you define inside the brackets.

For example, if you have these files:

file1.txt
file2.txt
file3.txt
file4.txt
file5.txt

Match only files with 1, 2 or 3:

ls file[123].txt

Output:

file1.txt
file2.txt
file3.txt

Using ranges inside brackets:

Instead of listing every character, you can use a range with a hyphen -:

ls file[1-3].txt    # same as [123]
ls file[a-z].txt    # matches any lowercase letter
ls file[A-Z].txt    # matches any uppercase letter
ls file[0-9].txt    # matches any single digit

Negating a bracket pattern:

Use ! or ^ at the start to match any character not in the set:

ls file[!45].txt    # matches file1, file2, file3 but NOT file4 or file5
ls file[^45].txt    # same result

Combining character sets:

ls file[1-3a-c].txt   # matches file1, file2, file3, filea, fileb, filec
challenge icon

Challenge

Beginner

Use a bracket wildcard with a range to list only file1.txt, file2.txt and file3.txt — excluding file4.txt and file5.txt.

Hint: Use the range syntax [1-3] inside the brackets. This is more elegant than listing each character separately as [123].

Cheat sheet

The bracket wildcard [] matches exactly one character from a specific set or range of characters defined inside the brackets.

Match specific characters:

ls file[123].txt    # matches file1.txt, file2.txt, file3.txt

Using ranges:

ls file[1-3].txt    # matches digits 1 through 3
ls file[a-z].txt    # matches any lowercase letter
ls file[A-Z].txt    # matches any uppercase letter
ls file[0-9].txt    # matches any single digit

Negating a pattern:

Use ! or ^ at the start to match any character not in the set:

ls file[!45].txt    # excludes file4.txt and file5.txt
ls file[^45].txt    # same result

Combining character sets:

ls file[1-3a-c].txt    # matches file1, file2, file3, filea, fileb, filec

Try it yourself

Terminal
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals