Bash: multiple pattern matches

I use this script to convert all .png files into a directory into .jpg files. If I want to convert not only png files, but also tif, gif and bmp files to jpg, how can I change this script?

  #!/bin/bash for f in *.png ; do convert "$f" -resize 50% "${f%.*}.jpg" done 
+4
source share
2 answers

Just add the extensions you want to handle; eg:

 for f in *.png *.tif *.gif; do 

or simply:

 for f in *.{png,tif,gif}; do 

another approach may be as follows: find each image file in a directory or folder tree and convert them to jpg, except that the image is already a jpg file; for example (not verified):

 find . -exec bash -c 'file "$1" | grep "image data" | grep -iv JPEG && convert "$1" -resize 50% "${1%.*}.jpg"' {} {} \; 
+10
source

for f in *.{png,tif,gif,bmp}; do

+1
source

All Articles