Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: Multiple pattern matching

I use this script to convert all the .png files in a directory to .jpg files. If I want to convert not just png files, but also tif, gif and bmp files into jpg, how this script can be modified?

  #!/bin/bash
    for f in *.png ; do
        convert "$f" -resize 50% "${f%.*}.jpg"
    done
like image 390
nixnotwin Avatar asked May 01 '11 06:05

nixnotwin


2 Answers

Just add the exensions you want to process; for example:

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

or just:

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

another approach could be: find every image file in a directory or a tree of folders and convert them to jpg except if the image is already a jpg file; for example (not tested):

find . -exec bash -c 'file "$1" | grep "image data" | grep -iv JPEG && convert "$1" -resize 50% "${1%.*}.jpg"' {} {} \; 
like image 99
hmontoliu Avatar answered Oct 14 '22 21:10

hmontoliu


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

like image 37
jcomeau_ictx Avatar answered Oct 14 '22 21:10

jcomeau_ictx