1

I've looked at this question, and while it works there a few issues:

  1. All converted images are placed in the directory from which the command is called.
  2. The original images are kept.

I need to convert around 70,000 .tif and .jpgs to .pngs. It's really important the the files when converted are placed in their normal directories.

I can do a batch rm of .tff/.jpg's at the end if that's the best way to handle removing the old files.

2 Answers 2

3
for f in $(find . -iname "*.jpg" -type f) ;
    do
    convert $f $(dirname $f)/$(basename -s .jpg $f).png ;
    done
  • find . -iname "*.jpg" -type f : search for file ending with ".jpg" case insensitive
  • $(dirname $f) : relative path to the folder containing file $f
  • $(basename -s .jpg $f) : filename without the suffix ".jpg"

In old Ubuntu basename may need to be run with different way:

basename $f .jpg

Convert is a part of imagemagick (As reference see Batch Processing tif images: Converting .tif to .jpeg) , to install

sudo apt-get install imagemagick
11
  • Thanks, I get: basename: invalid option -- 's' Try `basename --help' for more information. A lot of times... Jan 16, 2014 at 17:01
  • @user2757729 basename has a -s option in Ubuntu. Are you sure you are on Ubuntu?
    – gertvdijk
    Jan 16, 2014 at 17:02
  • 1
    Try with basename --suffix=.jpg $f or basename $f .jpg
    – user.dz
    Jan 16, 2014 at 17:05
  • 1
    looks like basename $f .jpg was the way to go Jan 16, 2014 at 17:11
  • 2
    The find -iname finds foo.JPG too - but the basename $f .jpg does not handle that. Sep 25, 2014 at 11:32
0

Works fine for file names with spaces also.

To remove originals:

SAVEIFS=$IFS; IFS=$'\n'
for f in $(find . -iname "*.jpg" -type f); do
    convert "$f" "${f%.*}.png"; rm "$f"
done
IFS=$SAVEIFS

To keep originals: remove the < ; rm "$f" > part

SAVEIFS=$IFS; IFS=$'\n'
for f in $(find . -iname "*.jpg" -type f); do
    convert "$f" "${f%.*}.png"
done
IFS=$SAVEIFS
1
  • Since @user2757729 wanted to remove the originals
    – Vijay
    Sep 1, 2018 at 11:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .