Bash script to rename images according to their size?

I uploaded a bunch of images using wget, and unfortunately there was a huge flaw ...

Uploaded images have the same name! So the script is automatically added .1, .2, ... etc. in the end:

Accept-Male-User-icon.png Accept-Male-User-icon.png.1 Accept-Male-User-icon.png.2 ... 

So, I'm looking for a script that will take these files and rename them according to their size, given that their size can be one of the following:

(256x256, 128x128, 64x64, 48x48, 32x32, 16x16)

So, I get something like this:

 Accept-Male-User-icon256.png Accept-Male-User-icon128.png Accept-Male-User-icon64.png ... 

Thanks!!

+4
source share
2 answers

If you have ImageMagick installed, you can try:

 for a in *.png*; do mv -i $a ${a%\.png*}`identify -format '%w' $a`.png; done 

Check it out, and if it works, remove the -i switch after mv .

+10
source
  • Get dimensions using something like pngcheck , pnginfo or imageinfo --width --height
    You probably need to install it, for example. using apt-get or yum
  • Separate the suffix with newname=${filename%.*}
  • Rename with mv
+3
source

Source: https://habr.com/ru/post/1338230/


All Articles