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.txtMatch only files with 1, 2 or 3:
ls file[123].txtOutput:
file1.txt
file2.txt
file3.txtUsing 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 digitNegating 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 resultCombining character sets:
ls file[1-3a-c].txt # matches file1, file2, file3, filea, fileb, filecChallenge
BeginnerUse 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.txtUsing 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 digitNegating 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 resultCombining character sets:
ls file[1-3a-c].txt # matches file1, file2, file3, filea, fileb, filecTry it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Directories
Create A DirectoryCopy A DirectoryMove And Rename A DirectoryDelete A DirectoryRecap - Directory Operations7File Content
Head And TailWord CountSort CommandUnique CommandGrep BasicsGrep With FlagsRecap - Text Detective2Navigation
Print Working DirectoryList FilesChange DirectoryAbsolute vs Relative PathsHome And Root DirectoryRecap - Find Your Way8Redirection
Standard OutputOverwrite To A FileAppend To A FileStandard InputStandard ErrorRecap - Log Builder6Wildcards And Patterns
The Star WildcardThe Question Mark WildcardBracket WildcardsCombining WildcardsRecap - Selective Operations9Piping
What Is A PipeChaining Two CommandsChaining Multiple CommandsPipe With GrepRecap - Data Pipeline