Bash Globbing
Globbing matches file names with wildcard patterns. One short pattern can stand for many files at once.
Match with *
The pattern *.txt matches every name that ends in .txt. The .log file is not matched.
touch a.txt b.txt c.log
echo *.txtA different ending
Changing the ending changes which files match. Here *.log picks only the two log files.
touch a.txt b.log c.log
echo *.logMatch one character with ?
The ? wildcard stands for exactly one character. It matches file1 and file2, but not file10.
touch file1.txt file2.txt file10.txt
echo file?.txtExample
touch a.txt b.txt c.log d.log readme
echo "Text files:"
echo *.txt
echo "Log files:"
echo *.log
echo "Markdown files:"
echo *.mdKey points
- A glob pattern uses wildcards to match file names.
- The * wildcard matches any number of characters.
- The ? wildcard matches exactly one character.
- Bash expands a glob into a sorted list of matching names.
- A pattern with no matching file stays as literal text.
- Globs are expanded by Bash before the command runs.
In this example, each glob expands to the files that match its ending. No file ends in .md, so that pattern stays literal. Bash prints *.md exactly as written.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Which pattern matches every file name ending in .txt?