Linux and Command-Line Basics
A directory contains six files: a1.txt, a2.txt, a10.txt, b1.txt, c1.txt and a1.csv. Which of the following is the number of files shown when "ls [ab]?.txt" is run in this directory?
- A2
- B3
- C4
Show answer and explanation
Answer: B. 3
Before running a command, the shell expands names containing wildcards into file names (globbing). [ab] matches one character that is either a or b, ? matches exactly one character, and * matches any string of zero or more characters. [ab]?.txt means "a or b, then any one character, then .txt", so it matches the three files a1.txt, a2.txt and b1.txt. a10.txt has two characters after a, c1.txt starts with c, and a1.csv has a different extension, so they do not match.
- A. [ab] matches one character that is a or b, so b1.txt is included too.
- B. Correct. The three files are a1.txt, a2.txt and b1.txt.
- C. ? matches exactly one character, so a10.txt is not included. Reading ? as zero or more characters, like *, would include a10.txt and give 4.