Frequently Asked Question

How does shell globbing work, and what do *, ? and [abc] mean?

Globbing is filename expansion done by the shell before a command is run. When you type ls *.md, bash looks at the current directory, finds every entry whose name matches *.md, and replaces the pattern with the matching list of names. By the time ls is invoked it sees the expanded arguments and has no idea a wildcard was ever involved. This is why a pattern that matches nothing usually fails noisily ("no such file") and why quoting changes things: ls "*.md" passes the literal string and ls itself errors out.

The patterns are simple. * matches any string of characters (but not the slash that separates path components). ? matches exactly one character. [abc] matches one of the characters in the brackets, [a-z] a range, and [!abc] anything not in the set. Bash adds brace expansion ({one,two,three}), which is not strictly globbing because it does not look at the filesystem, and an opt-in ** for recursive matching with shopt -s globstar.

Further reading and video