I would like to recursively list all files in a directory which match a given input pattern using ls. (Oh, and this is in Bash).
The only way I can think of to do this using ls is to shopt -s extglob and then use the ls --ignore=pattern option with the extended glob negate pattern !( pattern-list ). Seems kind of roundabout to negate in an ignore but otherwise wildcards don't recurse.
Assuming I already have the directory to look in as dir and the pattern to match against as file, this is my current attempt:
ls -AR --ignore='!('"${file}"')' ${dir}
However, this does not seem to have the desired effect, for example in a test case where the directory structure gives this output for ls -AR:
.:
a a.c b b.c c c.h s
./s:
a a.c b b.c c c.h
My command with file = *.c and dir = s (full command: ls -AR --ignore=!(*.c) s) gives this output:
./s:
a a.c b b.c c c.h
I know I could pipe it into grep quite easily, and do something like ls -AR s | grep -v '.*.c' and get output like:
s:
a
b
c
c.h
but I am looking for a way to do this just through ls.
Sorry if I was unclear at all, this is my first time on a StackExchange site.
Thanks in advance!
