Hello 🙂 I want to recursively optimize many of jpg files and made such a oneliner to do so.
for i in $(find . -iname '*.jpg' ) ; do file=$(echo $i|sed 's/.jpg//g') ; echo file to $file ; cjpeg -quality 95 -outfile "$file-o.jPg" -verbose "$file.jpg" ; wynik="$?" ; if [ $wynik -eq 0 ]; then echo FILE OPTIMIZED RM SOURCE ; rm "$file.jpg" ; mv -v "$file-o.jPg" "$file.jPg" ; else echo OPTIMIZE FAILED RM DEST ; rm "$file-o.jPg" ; fi ; done
Cjpeg doesn’t support overwriting original file, so I create new with “-o” suffix, and, if process went ok, it removes original file. After that I want to remove that “-o” suffix and leave file with “.jPg” extension (to filter it out from next optimizations – files to optimize needs to have lowercase extension). That works, however I’ve issues with my little script:
First, and the biggest issue, is that it will omit every file or directory with spaces.
Second is that using “sed ‘s/.jpg//g’ within logic works well for everything else, however it will definitively miss files with additional “.jpg” within name like “example_of_.jpg_file.jpg” or “file.jpg.jpg”. Same thing for “.jpg” in directory name. However I thing it would be needed to change whole logic to use awk for it since ?