miércoles, 17 de mayo de 2017

renombrar extenciones de FICHEROS MASIVAMENTE RECURSIVA



Renombrar  exenciones de ficheros

en ocaciones las camaras , celulares guardan fotos con extenciones erroneas
su pongamos que sea JPG, pero el app o android las guarda en PNG.

la mayoria de los visualizadores  podran abrir las fotos pero otros se limitan a negarse.


para renombrar de  ".png" a  ".jpg"
find /ruta/de-las/fotos  -type f -exec  rename 's/\.png$/\.jpg/' *  '{}' \;

"en reversa"
para renombrar de  ".jpg"  a  ".png"
find /ruta/de-las/fotos  -type f -exec  rename 's/\.jpg$/\.png/' *  '{}' \;



***********************************************************************

ok, ahora  supongamos que las fotos   PNG  estan entre otras que REALMENTE SI SON "PNG".
y yo solo quiero renombrar los que tiene mal la extencion.
¿como detectar los formatos con extenciones errorneas ?




  find /ruta/de-las/fotos    -type f -name \*.jpg -exec file \{\} \; | grep -i png | cut -d ':' -f 1 | xargs -d '\n' rename  's/\.jpg/\.png/'


explicacion:
busca ficheros con noombre *.jpg =
   find /ruta/de-las/fotos    -type f -name \*.jpg


detecta el tipo de fichero que es = 
   -exec file \{\} \;

entonces busca SOLO los que diga PNG=
| grep -i png

entonces  corta el nombre
 | cut -d ':' -f 1

entonces  Xargs manda  a RENAME   los nombres de  los ficheros 
| xargs -d '\n' rename  's/\.jpg/\.png/'

NOTA: si tiene  una comilla el nombre te dara un error  

 

gracias  a  furrywolf por su ayuda.
tanks to furrywolf  for this help.

that command very uglyly (it's a word now!) chains together several unix commands...  find looks for .jpg files, file identifies them, grep looks for ones where file said they're pngs, cut grabs the part before the ':' find printed, which is the filename, then xargs sends all those filenames to rename.  lol

 it's not done well - it will break if you have filenames containing a colon or a newline.  but I figured it's pretty unlikely you have any of those.  a cleaner version wouldn't break like that, but I didn't want to spend much time thinking about it.  :)

Donarme :->

Donar a AlexLikeRock
muchas gracias por tu ayuda :->

2022

stand by ... PD: "Viva México!"