• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: for-loop for filename that have a specific string or a specific extension

Resolved: for-loop for filename that have a specific string or a specific extension

0
By Isaac Tonny on 16/06/2022 Issue
Share
Facebook Twitter LinkedIn

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:

Try
shopt -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!

bash for-loop
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Pandas Groupby Get Values from Previous Group

24/03/2023

Resolved: Can’t access static methods from outside until JavaScript’s static block is over?

24/03/2023

Resolved: Count number on values (which are measure) in a table

24/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.