Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash loop over file mask

What's the proper way to do a for loop over a file mask?

E.g. if the mask doesn't expand to any file, then the loop should not run; else, it should run over all the files that it expands to.

The problem with naive approach is that if the * doesn't expand to anything, then the whole loop will run once as if the * part is an actual part of a filename (of a file that doesn't actually exist).

like image 204
cnst Avatar asked Sep 03 '26 04:09

cnst


2 Answers

One way to do this is:

for f in abc*; do if [[ -f "$f" ]]; then
  # Do something with "$f"
fi; done

That will also filter directories out of the list, which might or might not be what you want.

If you want to keep directories, use -e instead of -f.

Another way to do it is to set the shell option nullglob (may not be available on all bash versions). With nullglob set, patterns which don't match any filename will result in nothing instead of being unaltered.

shopt -s nullglob
for f in abc*; do
  # do something with $f
done
shopt -u nullglob

(That leaves nullglob set for the duration of the for loop. You can unset it inside the loop instead of waiting for the end, at the smallish cost of executing the shopt -u on every loop.)

like image 186
rici Avatar answered Sep 04 '26 22:09

rici


Use the nullglob shell option to make a wildcard that doesn't match anything expand into nothing instead of returning the wildcard itself:

shopt -s nullglob
for file in abc*
do
    ...
done
like image 43
Barmar Avatar answered Sep 04 '26 22:09

Barmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!