Reputation: 363
In a bash script, I'd like to list all files matching a string, including those with a leading dot.
E.g.
ls -A
gives me
.bla.0 .bla.1 bla.2 bla.3
and I'd like to have an expression of the sort
for f in <whatgoeshere?>${pat}* ; do
<something with $f>
done
I have tried to use
shopt -s extglob
and some form of ?(.)bla* and ?(\.)bla*, but to no avail.
I could use
shopt -s dotglob
but I was wondering, if there's a way to specify the pattern without using that.
This would work, but is not very elegant:
for f in `shopt -s dotglob ; ls -A *${pat}*` ; do echo $f; done
Upvotes: 1
Views: 113
Reputation: 4399
You don't need extglobs. Just use something like this:
for f in {.,}${pat}* ; do
[[ $f == "." || $f == ".." ]] && continue
echo $f
done
Update:
If you anticipate files with spaces in the name, go with the find
based solution.
Upvotes: 2
Reputation: 785866
You can replace your ls -A
command with find
with -regex
switch like this:
while read f; do echo $f; done < <(find . -maxdepth 1 -regex ".*${pat}.*")
Upvotes: 2