Linux and Unix-like shells like bash allow for the use of globs to approximate filenames and make file searching easier. I'm aware of the wildcard glob (*). What other globs exist in bash that I can make use of with grep?
- 13,835
1 Answers
There is a difference between the bash builtin parameter expansion called glob, and what grep can understand as a search input.
Typical glob use is the *, to expand to any string in it's place that matches the rest of the string. For example:
ls
# 1.txt 2.txt 3.txt
grep "search string" *.txt
# expands the star to match anything ending in .txt, so in this case is the same as:
grep "search string" 1.txt 2.txt 3.txt
This is all bash, and also works with echo for example (but since it's bash, it doesn't work inside quotes).
To your actual question, here is the glob manpage (or man glob.7 from the shell) which also describes several other matchers you can use as a glob. To summarize (using ls for the sake of simplicity):
ls ?.txt # matches any single character
ls [0-9].txt # matches a single character from 0 to 9
ls [[:digit:]].txt # same as above
ls [0-9A-Za-z].txt # matches any alphanumeric character
See the glob manpage above for a complete list plus some notable special cases like matching a . for example.
For the grep search string, there are several forms of (semi)standard regex strings you can use, see for example -e, -E or -P in the grep man page and general regex help online for the supported syntaxes that are way to numerous to list here.
- 189