3

I'm new with shellscript and I'm having some problems trying to do what I need...

I want to 'optimize' all my *.png images and found this so I made the bash as it says with:

#!/bin/bash    
 pngnq -vf -s1 *.png    
 rename -nq8.png .png *-nq8*     
 optipng -o7 *.png

What this do is:

  • pngnq -vf -s1 *.png takes a *png ofr example Image.png and craetes a copy named Image-nq8.png which is the one optimized
  • rename -nq8.png .png *-nq8*makes Image-nq8.png become Image.png so you have the same as before but optimized (so you can have the same in the folders as you had before optimize) <= VERY IMPORTANT!
  • optipng -o7 *.png does another optimization to Images.png without making any copy.

But now I have a problem:

If I write in console every line in order, it works ok on the folder I am but now I need to do it automatically and recursive. I explain my case:

note: I say folder as a directory

I have a folder named ImagesFolder and inside I have other folders with more *.pngs and I want to run this bash in every subfolder of ImagesFolder but I don't know how to do it (as I said I'm new in unix and shellscripts and so on) and what I found on internet and tried didn't work or was a completly mess...

To understand it better what I have is something like:

ImagesFolder
  |-Folder1
  |     |- Folder12
  |     |      |-20 images here
  |     |- Folder13
  |     |      |- 10 images more here
  |     |-  _Folder <- I have some folder names that start with '_'
  |     |- 5 images here 
  |-more images and folders and subfoldrs

This is the structure and I want the script to run from ImagesFolder.

I tried with a for and a find in it, and with a find in console calling the script... tried too appling some examples I found here in stack but nothing... Probably is because as I don't really know how does this work I can't apply it just like that. Hope you can help me with this problem.

Thank you so much!

Edit: To run the script I have to go to ImageFolder, open a console and put chmod u+x name_of_my_script could this be a problem to run it into the subfolders? If yes, can you tell me how to fix it?

4
  • Why u+x not just +x?
    – Tim
    Jun 4, 2015 at 11:16
  • well find on a web that I had to use u+x but now I'm using only +x. I'll have to see which is the difference
    – Megasa3
    Jun 4, 2015 at 11:28
  • As per comment from the OP, this is running on Fedora.
    – Fabby
    Jun 4, 2015 at 17:58
  • Yes, it's a Fedora 12 with a GNU-bash, version 3.0.38(1)
    – Megasa3
    Jun 5, 2015 at 7:02

1 Answer 1

0

A bash script:

Create a new file find_and_rename and add the following lines:

  • version with replace

    #!/bin/bash
    shopt -s globstar
    for f in **/*png; do 
        pngnq -vf -s1 "$f"
    
        # Uncomment the line below for a test run
        # rename -v -n 's/^(.*)\-nq8(.*)/$1$2/' "$f"
    
        # Comment the line below out for a test run
        rename -v 's/^(.*)\-nq8(.*)/$1$2/' "$f"
    
        optipng -o7 "$f"
    done
    
  • version with mv

    #!/bin/bash
    shopt -s globstar
    for f in **/*png; do 
        pngnq -vf -s1 "$f"
    
        # Uncomment the line below for a test run
        # echo "$f  => ${f//-nq8/}"
    
        # Comment the line below out for a test run
        mv "$f" "${f//-nq8/}"
    
        optipng -o7 "$f"
    done
    

Make the file executable with:

chmod +x find_and_rename

Go into your folder ImagesFolder.

Start the script in this folder with.

<path_to_script>/find_and_rename

Example (without pngnq and optipng

$ find . -type f -name "*.png"
./Image-nq8.png
./sub/Image-nq8.png
./Image2-nq8.png

$ ./find_and_rename 
Image2-nq8.png renamed as Image2.png
Image-nq8.png renamed as Image.png
sub/Image-nq8.png renamed as sub/Image.png

$ find . -type f -name "*.png"
./Image2.png
./sub/Image.png
./Image.png
18
  • Hi, how do I call it? with ./Name_of_script when I'm on ImageFolder ? If I do this, it goes into ImageFolder, takes the first image and stops and leave me there the 2 images: Image.png and Image-nq8.png (I edited my question with the permission problems because maybe he stops for that) But the rename did nothing... and I suppose that this is not a permissions problem
    – Megasa3
    Jun 4, 2015 at 10:41
  • Again updated =)
    – A.B.
    Jun 4, 2015 at 11:11
  • Thanks for the update and the help :) I am applying it with a little folder (which has subfolders and images too of course) so I can test quickly if it works Your code enters on every subfolder and tooks the *.pngs and applies the first line (the one that makes the name-nq8.png copies) BUT then it finishes and nothing more happens... so I have the original image and the optimized one with the -nq8 on the name.... so seems that the rename does not work fine....
    – Megasa3
    Jun 4, 2015 at 11:20
  • I tried your new code and tried both uncommenting the first rename and letting only the second one and I have the same: Image.png and Image-nq8.png ... I'm on a fedora12 but I suppose that this is not the problem
    – Megasa3
    Jun 4, 2015 at 11:27
  • I have tested the script without pngnq ... and optipng .... It works in my environment.
    – A.B.
    Jun 4, 2015 at 11:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .