5

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

2 Answers 2

11

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"' {} {} \; 
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to suppress f being '*png' if there are no png files to be found? for i in *jpg *png; do echo $i; done in a directory with all jpegs produces a *png in addition to the expected output.
Found it: unix.stackexchange.com/questions/56051/… for i in *jpg *png; do [ -e "$i" ] || [ -L "$i" ] || continue; echo $i; done
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.