Monday, May 13, 2024
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 3135  / 3 Years ago, sat, september 4, 2021, 9:04:09

I am using imageOptim to optimize images on my server.


It took me quite some time to learn rudimentary bash scripting to able to optimize all images in all my first level directories. My script looks as below and from functional point of view it does exactly what I want:


for dir in mydir/*/
do
sudo jpegoptim --strip-all -t "$dir"*.jpg
done

The problem is aesthetic. When I run the script it outputs the following:


mydir/dir_1/img_1.jpg axb 24bit N JFIF  [OK] 2234 --> 1861 bytes (16.70%), optimized.
...
mydir/dir_1/img_16.jpg axb 24bit N JFIF [OK] 2234 --> 1861 bytes (16.70%), optimized.
Average compression (16 files): 7.26% (8k)
mydir/dir_2/img_1.jpg axb 24bit N JFIF [OK] 2234 --> 1861 bytes (16.70%), optimized.
...
mydir/dir_2/img_6.jpg axb 24bit N JFIF [OK] 2234 --> 1861 bytes (16.70%), optimized.
Average compression (6 files): 7.26% (8k)

It outputs the line Average compression (6 files): 7.26% (8k) because I asked for a summary with the -t flag. But I do not want to get all information about each individual image. When I try to suppress it with -t -q or -tq or -q -t (I tried them all because I am not sure it it matters) it showed no information at all.


Ideally I just want to see one number - how much size have I saved.


Because this is my first bash script (I struggled for an hour with that loop :-( ), my skills currently are not sufficient to solve my problem.


More From » command-line

 Answers
6

After hours of improving my basic unix/bash skills I found what I think is an amazing way to do this. It is so simple that I am surprised that I wasted so much time. So here is the code:



du -s mydir/
for dir in mydir/*/
do
sudo jpegoptim --strip-all -q "$dir"*.jpg
done
du -s mydir/


That's it the difference between the first and the second output will give me how much space I have saved in KB. du -s mydir/ will tell me the size of the folder before optimization, the same one in the end - after optimization.



If I would be better at bash, I would be able to save the first one in some variable, subtract the second. But even this is good for me.


[#26405] Monday, September 6, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alliulet

Total Points: 46
Total Questions: 109
Total Answers: 97

Location: Svalbard and Jan Mayen
Member since Sat, Oct 10, 2020
4 Years ago
;