The directory contains log1.txt, logA.txt, and log10.txt.
Pass only names with exactly one character after log and before .txt to ls.
Which filename pattern is appropriate?
In shell globbing, ? matches exactly one character. * matches zero or more characters and is broader.
Detailed explanation
log?.txtCorrect. log?.txt matches log1.txt and logA.txt but not log10.txt.
?は任意の1文字に一致し、log1.txtとlogA.txtへ一致する。
log*.txtIncorrect. * can match zero or more characters, including the two digits in log10.txt.
log[0-9].txtIncorrect. [0-9] limits the character to a digit and excludes logA.txt.
log??.txtIncorrect. ?? requires two characters.
Try it yourself
An example you can run in a temporary verification environment.
tmp=$(mktemp -d); touch "$tmp/log1.txt" "$tmp/logA.txt" "$tmp/log10.txt"; (cd "$tmp" && printf '%s\n' log?.txt); rm -rf "$tmp"Expected result
log1.txt
logA.txtKey points
- ? matches one character
- * matches zero or more
- The shell expands globs
Notes
- Environment: Bash 5.x / 一時ディレクトリ
- Use disposable files and directories for destructive or timestamp-related checks.
- Command details can vary slightly between GNU and non-GNU implementations.
Foundation review
Place the operation
A question mark is narrower than an asterisk because it consumes exactly one character between fixed parts of a name.
Check the side effect
Create one-character and two-character examples in a temporary directory and print the expanded names.