Bash by Example: Glob Patterns
Matching filenames using standard wildcards and glob patterns including asterisk (*) for any characters, question mark (?) for single characters, and bracket expressions ([]) for character sets, understanding that globs expand before command execution, using glob patterns for file filtering, and distinguishing glob patterns from regular expressions.
Code
#!/bin/bash
mkdir -p globs
touch globs/file1.txt globs/file2.txt globs/fileA.log globs/data.csv
echo "--- Match Everything (*) ---"
# Matches all files in 'globs' directory
echo globs/*
echo -e "\n--- Match Single Character (?) ---"
# Matches file1.txt, file2.txt, etc.
echo globs/file?.txt
echo -e "\n--- Match Character Set ([...]) ---"
# Matches file1.txt or file2.txt, but not fileA.txt if it existed
echo globs/file[12].txt
echo -e "\n--- Match Range ([a-z]) ---"
# Matches fileA.log (if case sensitive depending on locale)
echo globs/file[A-Z].log
echo -e "\n--- Negation ([!...]) ---"
# Matches anything EXCEPT 1 or 2
touch globs/file3.txt
echo globs/file[!12].txt
rm -rf globsExplanation
Glob patterns (or "globbing") are the shell's mechanism for pattern matching filenames. Unlike Regular Expressions (Regex), which are used for text processing inside files, globs are used to expand filenames on the command line. When you type *.txt, the shell itself expands this into a list of matching filenames before passing them to the command. This means the command (like ls or echo) never sees the asterisk; it sees the actual list of files.
The most common glob is the asterisk *, which matches any string of characters, including an empty string. The question mark ? matches exactly one character. This is useful when you know the length of the filename but not the specific character at a position (e.g., image_???.jpg for image_001.jpg).
Square brackets [...] allow for more precise matching. You can specify a set of characters (e.g., [abc] matches 'a', 'b', or 'c') or a range (e.g., [0-9] matches any digit). You can also negate the set using ! or ^ as the first character inside the brackets, matching any character except those listed.
Code Breakdown
* wildcard expands to all files in the directory. If no files match, the shell usually leaves the string as globs/* literal (unless nullglob is set).? is strict about length. file?.txt matches file1.txt but would NOT match file10.txt because '10' is two characters.[!12] matches any single character that is NOT '1' or '2'. So it matches '3' in file3.txt.
