Using metacharacters, I need to perform a long listing of all files whose name contains the string foo
followed by two digits, then followed by .txt
. foo**.txt
will not work, obviously. I can't figure out how to do it.
Asked
Active
Viewed 280 times
0

Todd A. Jacobs
- 81,402
- 15
- 141
- 199

user2504710
- 42
- 1
- 8
2 Answers
0
Something like ls foo[0-9][0-9]*.txt
of whatever exactly fits your pattern.

Michael Kohl
- 66,324
- 14
- 138
- 158
-
can you please elaborate a bit what *[!0-9][0-9] does. Thank you for your response. – user2504710 Oct 26 '14 at 16:37
-
Sorry, copy/paste mistake. Look at the current answer, should be obvious. – Michael Kohl Oct 26 '14 at 16:37
-
In case it's not: the string `foo`, followed by a character in the range 0 to 9, then another one, then whatever, then `.txt`. – Michael Kohl Oct 26 '14 at 16:38
0
Use Valid Shell Globbing with Character Class
To find your substring anywhere in a filename like bar-foo12-baz.txt
, you need a wilcard before and after the match. You can also use a character class in your pattern to match a limited range of characters. For example, in Bash:
# Explicit character classes.
ls -l *foo[0-9][0-9]*.txt
# POSIX character classes.
ls -l *foo[[:digit:]][[:digit:]]*.txt
See Also

Todd A. Jacobs
- 81,402
- 15
- 141
- 199