Convert .png files to .jpg and compress them throughout many subdirectories

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
tzell
Level 3
Level 3
Posts: 126
Joined: Wed Aug 03, 2022 4:56 pm

Convert .png files to .jpg and compress them throughout many subdirectories

Post by tzell »

Hi forum,
I'm pretty new to bash.

I have multiple .png and large .jpg files throughout multiple sub directories

Trying to achieve the following:
1. Convert all .png files to .jpg files throughout all sub directories.
2. Optimize .jpg files for better compression (about 80% quality).
3. Delete all obselete .png files.

I found the following commands to:
1. convert png to jpg (with imagemagick)

Code: Select all

 mogrify -format jpg *.png 
2. Optimize jpg

Code: Select all

 jpegoptim *.jpg -m 80 
Reading online I saw there's ways to do similar file operations with find command, or bash scripting with for loop.
How do I accomplish this? Which way should I pick and how? Couldn't get it to work.

Thanks!

EDIT:
Turns out I had to add "+" at the end of the -exec command following "find".
So it works with the following code, when I run each line seperately:

Code: Select all

find -name *.png -exec mogrify -format jpg {} +

find -name *.jpg -exec jpegoptim -m 80 {} +

find -name *.png -exec rm {} +
Is this the way to do it? Or is there a better more elegant way?
Last edited by LockBot on Tue Mar 07, 2023 11:00 pm, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
1000
Level 6
Level 6
Posts: 1040
Joined: Wed Jul 29, 2020 2:14 am

Re: Convert .png files to .jpg and compress them throughout many subdirectories

Post by 1000 »

1. Put the files into the new folder ( if you can )
2. Test how this example working

Code: Select all

#!/bin/bash

PATH_to_png="/YOUR/PATH/TO/PNG_FILES"

for FILE_name in $(ls "$PATH_to_png") ; do
    echo "FILE_name = $FILE_name"
done 

echo " Finished."
3. Test your command in terminal.
4. Test your command instead "echo" command in example.

EDIT:
You can use "find" command but use more options to specify the files
for example find /your/path -type f
User avatar
tzell
Level 3
Level 3
Posts: 126
Joined: Wed Aug 03, 2022 4:56 pm

Re: Convert .png files to .jpg and compress them throughout many subdirectories

Post by tzell »

Thank you for your reply,
I already ran the command using the 'find' examples above
Yes admittedly I rushed it without testing too much (Before seeing your answer)

Hoping removing the .png file didn't remove more than it needed to using the find command

Code: Select all

find -name *.png -exec rm {} +
I didn't specify the -type
1000
Level 6
Level 6
Posts: 1040
Joined: Wed Jul 29, 2020 2:14 am

Re: Convert .png files to .jpg and compress them throughout many subdirectories

Post by 1000 »

Very often, we can accept a risk if we know that the command will not do anything more than it should.
For example, if we checked / tested first what the command "find" can found.

For "find" command " if no starting-point is specified, `.' ( ./ ) is assumed.
If we make a mistake and we execute the command elsewhere, it will find other files.

When I tested "find" command without quotation marks ( was file *.png and folder *.png )

Code: Select all

$ find  -name *.png
find: paths must precede expression: `jhgjh.png'
find: possible unquoted pattern after predicate `-name'?
So it's a nice security feature.
Locked

Return to “Scripts & Bash”