Question:
- On a for-loop how do you search for both a specific string or a filename extension?
I have files that have no extension but have the string “-DT-” in the filename, and other files that have the extension .fff. I want to process both types.
local dir imfiles
for imfiles in "$dir"**/*-DT-!(.jpg); do
printf "File processed: ${imfiles}\n"
done
- How can I make the for-loop to not only look for files that have the string -DT- but also files that have an extension .fff?
The result would be the for-loop processing both types of files. I have tried using an array, but it didn’t work. I have seen a lot of examples of for-loop but none approaching this specific scenario.
Answer:
Tryshopt -s dotglob extglob nullglob globstar
for imfiles in "$dir"**/@(*-DT-!(*.jpg)|*.fff); do
printf "File processed: ${imfiles}\n"
done
- See the extglob section in glob – Greg’s Wiki for an explanation of
!(*.jpg)
and@(*-DT-!(*.jpg)|*.fff)
.
If you have better answer, please add a comment about this, thank you!