I have a directory that contains the following files:
file1a file1ab file12A2 file1Ab file1ab
I want to list all files that start with file1
and followed by two letter at most!
The solution I have proposed is as follows:
ls | grep -i file1 [az] {2}
But it does not work!
I want to know why? and how to list?
Best Answer
You don't need piping,
grep
orls
. Just use shell globbing.In
bash
, usingextglob
pattern (should be enabled by default in interactive sessions, if not doshopt -s extglob
to set it first):?
matched any single character,@(||)
selects any of the patterns separated by|
.If you only meant to match any characters between
a-z
andA-Z
, use use character class[:alpha:]
which denotes all alphabetic characters in the current locale:Example:
zsh
supports this natively:I am answering this portion very reluctantly, upon request from OP.
Any future reader, Don't parse
ls
, use globbing.Using
ls
andgrep
:Example: