3

I want to convert all jpgs (independent from lower or upper case or jpeg extension) in my current directory to a 95% optimized one. However I do not get the files correctly in my for loop with JPG,jpg and jpeg files :/

#!/bin/bash
PIC=$(ls "$PWD"/*.{jpg,jpeg,JPG})
for i in $PIC
  do
   echo $i
   # convert $i -quality 95 ${i%.*}_resaved.jpg
done
therealmarv
  • 3,692
  • 4
  • 24
  • 42
  • Are you certain there are files of this type in `$PWD`? That `ls` command works for me when I try it in a folder with similar files. – Jonah Bishop Apr 05 '13 at 19:12
  • Yes I'm sure. But I got very strange results with paths which have spaces and directories which do not have .jpeg files. – therealmarv Apr 05 '13 at 19:25
  • Possible duplicate of [Matching files with various extensions using for loop](https://stackoverflow.com/q/6223817/608639), [for loop for multiple extension and do something with each file](https://stackoverflow.com/q/12259331/608639), [Loop over multiple file extensions from bash script](https://stackoverflow.com/q/49103942/608639), [for loop in bash go though files with two specific extensions](https://stackoverflow.com/q/34382072/608639), etc. – jww Aug 19 '18 at 06:45

1 Answers1

5

Try:

find -iname "*.jpg" -o -iname "*.jpeg" | while read f; do
  echo "$f"
  convert "$f" -quality 95 "${f%.*}_resaved.jpg"
done
Joao Morais
  • 1,885
  • 13
  • 20