Im almost there with this code:

for PHOTO in /home/dvms/Desktop/projs/others/tests/gulp_test/src/images/*.{png,jpeg,jpg}
       BASE=`basename $PHOTO`
    convert "$PHOTO" -quality 50% "/home/dvms/Desktop/projs/others/tests/gulp_test/src/imagesCompressed/$BASE.jpg"

But the output files are appearing with their old file extension with a ".jpg" appended in the end, example: imageA.png.jpg .

How can solve this?

  • That's because you have this $BASE.jpg it should be ${BASE%%.*}.jpg – George Udosen Aug 25 '18 at 14:51
  • @GeorgeUdosen the answer of Parto worked but its also duplicating the images adding "imgOne-0.jpg", "imgTwo-1.jpg", etc... – K_dev Aug 25 '18 at 14:53
  • I know it did just offering alternative syntax! – George Udosen Aug 25 '18 at 14:55
  • @GeorgeUdosen The alternative syntax is good enough as an answer,too. Please post – Sergiy Kolodyazhnyy Aug 25 '18 at 15:23

Replace the line:

BASE=`basename $PHOTO`

With this one:

BASE=`basename $PHOTO | cut -d. -f1`

Then try again.

  • Its close, but it duplicates also the images adding "imgOne-0.jpg", "imgTwo-1.jpg", etc... – K_dev Aug 25 '18 at 14:45
  • 1
    I tryed now and it didn't duplicate the images, strange.. – K_dev Aug 25 '18 at 15:04
  • Please remember to quote all variables, as BASE="$( basename "$PHOTO" | cut -d. -f1 )" (note two sets of quotation marks). Without them, if any file name contains a space or certain special characters, you run the risk of data corruption. Also, the cut command won't work if a file name contains two or more dots; this needs to be changed; it can also, in certain cases, cause data being overwritten. The answer by @GeorgeUdosen provides a good option. – Paddy Landau Aug 28 '18 at 10:15

Modify you code into this form:

for PHOTO in /home/dvms/Desktop/projs/others/tests/gulp_test/src/images/*.{png,jpeg,jpg}
       BASE=$(basename $PHOTO)
    convert "$PHOTO" -quality 50% "/home/dvms/Desktop/projs/others/tests/gulp_test/src/imagesCompressed/${BASE%.*}.jpg"

Rather than this $BASE.jpg use ${BASE%.*} then add the extension.

  • You might want "${BASE%.*}" to use the shortest matching suffix pattern, so my.long.filename.jpg turns into my.long.filename.jpg, not my.jpg. Also, worth checking that the output doesn't already exist, in case of collisions between different source suffixes. – Peter Cordes Aug 25 '18 at 18:46
  • Also worth checking if the output file is actually smaller than the input, otherwise just copy or link. (Or check if the input jpg quality setting was below some threshold, but just comparing file size after an attempted recompression is easier and always works.) – Peter Cordes Aug 25 '18 at 18:47
  • Please add quotes: BASE="$( basename "$PHOTO" )"(two sets of quotes), in case the file name contains a space or certain other special characters. – Paddy Landau Aug 28 '18 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.