processing multiple extensions

I have done a script to convert recursive .jpg files to another size:

echo $re
mkdir "$re"_tmp
for a in *.jpg ; do convert "$a" -resize $re "$re""_tmp/${a%.*} ["$re"].jpg" ; done

I’d like integrate a multi extension support: png, bmp, etc. better with:

FILEFORMAT="jpg, JPG, png, PNG, bmp, BMP"

any idea to build it?

PS: variable re is the new size 1024×768 (or 800×600, etc)

Asked By: Pol Hallen


If I understand right, you want to process files with other extension, instead of only jpg. So you can try:

for a in *.{jpg,JPG,png,PNG,bmp,BMP}; do
  printf '%sn' "$a"
  # do your stuff here

{...} is bash feature called brace expansion.

Answered By: cuonglm

You may use multiple GLOB expressions separated by space:

for a in *.[jJ][pP][gG] 
    echo "$a" "$re/$a"

# or inline
for a in *.[jJ][pP][gG] *.[jJ][pP][eE][gG] *.[pP][nN][gG] *.[bB][mM][pP] ; do echo "$a" "$re/$a"; done

Not sure if it is POSIX compatible, but I believe it is.

Answered By: Valdemar_Rudolfovich

Here is a POSIX complaint variant:

for a in *.jp* *.JP*; do
  [ -f "$a" ] || continue
  echo "Processing $a"
  1. In POSIX you may put multiple GLOBs separated by space
  2. In real life extensions are ether in lower case jpg or in upper JPG but not with mixed case.
  3. If no any JPG files then the glob wont be expanded and the var a will have *.JP* value. You must filter out it by checking that hte file is exists with the [ -f "$a" ]
Answered By: Sergey Ponomarev
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.