. and .., and filename globbing (wildcards). Filename globbing lets the shell expand simple patterns into matching filenames so you don’t need to memorize exact names.
Key terms: globbing, wildcards, shell patterns, Bash, filename expansion.
This guide covers the most common glob patterns supported by the shell: exact matches, wildcards (
*, ?), character sets ([]), POSIX character classes, and brace expansion. Remember: by default, glob patterns do not match names that begin with a dot (.) unless the pattern itself begins with a dot.Pattern reference
Exact match
To list a specific file by name:Wildcard: *
*matches zero or more characters.- Examples:
ls *— lists all non-hidden files in the current directory.ls *.txt— lists files ending with.txt.ls log*— lists files starting withlog.ls *X*— lists files containing the letterXanywhere in the name.
Wildcard: ?
?matches exactly one character.- Examples:
ls file?— matches names starting withfilefollowed by one character (e.g.,file1).ls ????— matches any filename exactly four characters long.
Character sets: []
Square brackets match any single character listed inside (OR relationship).
ls file[12]— matchesfile1orfile2.ls file[1-4]— matchesfile1..file4.ls [abc]*— matches filenames beginning witha,b, orc.
! (or ^ in some shells) as the first character to invert the set.
ls [!a]*— matches files that do not start witha. Note: dotfiles are excluded unless the pattern begins with a dot.- Ranges also work for digits and letters:
[0-9],[a-z], etc.
POSIX character classes inside bracket expressions
These are useful for matching character categories portably across locales:[[:digit:]]— any digit (0–9).[[:upper:]]— any uppercase letter.[[:lower:]]— any lowercase letter.
! inside the outer brackets:
ls [![:upper:]]*— files that do not start with an uppercase letter (dotfiles still excluded unless the pattern begins with a dot).
Brace expansion
Brace expansion is performed by the shell before globbing. It lets you generate multiple patterns concisely:ls *.{log,txt}— expands tols *.log *.txtand lists files ending with.logor.txt.
Hidden files
Files that begin with a dot (.) are hidden by default.
ls -a— shows all files including.and...ls -A— shows all entries except.and...ls .*— matches names beginning with a dot, but may expand to.and...- To list only dotfiles and avoid
.and.., use a safer pattern such as:ls -d .[!.]* ..?*
- To list only dotfiles and avoid
Hidden file commands quick reference
Example session
Below is a consolidated example that demonstrates the patterns and their outputs in a sample directory:Hidden files begin with a dot (
.). Use ls -a to show all files including hidden ones. If you want to list only dotfiles without seeing . and .., use ls -d .[!.]* ..?* or ls -A.Summary
- Use
*,?,[], POSIX character classes, and brace expansion to specify filename patterns. - Globbing allows you to describe sets of files concisely without remembering exact filenames.
- Remember: dotfiles are matched only when the pattern begins with a
..