0

I want to do a batch convert and resize of some images in a folder on the Linux (Mint 17) command line, so I am using:

for $file in *.jpg; do convert -resize 25% $file $file.png; done

but that leaves me with a bunch of files like:

image1.jpg.png
photo4.jpg.png
picture7.jpg.png
...

is there a way that I can easily clip the file extension from the file name so that it only has the .png instead of the preceding .jpg as well?

or, better yet, can I include a counter so that I can run the command with something like this:

for $file in *.jpg using $count; do convert -resize 25% $file image$count.png; done

so that I end up with something like:

image1.png
image2.png
image3.png
image4.png
...

I'd rather not have to worry about creating a batch script, so if that is too hard and there is a simple way of just removing the .jpg inline, then I'm happy with that..

thanks!

EDIT:
I think this question is okay to be a question in it's own right because it also has a counter element.

guskenny83
  • 1,321
  • 14
  • 27
  • Possible duplicate of [How can remove the extension of a filename in a shell script?](http://stackoverflow.com/questions/12152626/how-can-remove-the-extension-of-a-filename-in-a-shell-script) – tripleee Oct 26 '16 at 02:40
  • See also http://stackoverflow.com/questions/28725333/looping-over-pairs-of-values-in-bash – tripleee Oct 26 '16 at 02:41

4 Answers4

2
 for file in *.jpg; do convert -resize 25% $file ${file%.*}.png; done 
UltrasoundJelly
  • 1,845
  • 2
  • 18
  • 32
1

Something like this?

i=image1.jpg
echo ${i/.jpg/.png}
image1.png
tink
  • 14,342
  • 4
  • 46
  • 50
1
$ ls *.jpg
DSCa_.jpg*  DSCb_.jpg*  DSCc_.jpg*
$ count=0; for file in *.jpg; do (( count++ )); convert -resize 25% "$file" "${file%\.*}$count.png"; done
$ ls *.png
DSCa_1.png  DSCb_2.png  DSCc_3.png

${file%\.*} uses parameter substitution to remove the shortest matching string from the right of $file. You can read more about parameter substitution here

riteshtch
  • 8,629
  • 4
  • 25
  • 38
1

Also, you can use the command basename (strip suffixe from filename) :

for file in *.jpg; do base=$( basename  ${file} .jpg); convert -resize 25% $file ${base}.png; done
V. Michel
  • 1,599
  • 12
  • 14