Convert File Names to Alphabet Letters

Asked

Viewed 186 times

2

Convert File Names to Alphabet Letters

For example, I have several image/photo files in a directory, and I want to pass these long names of the respective images in names designated by letters of the alphabet

Before

IMG01-03082016.jpg

IMG02-03082016.jpg

IMG03-03082016.jpg

etc. ...

Afterward

A.jpg

B.jpg

C.jpg

etc. ...

  • 2

    What if the number of files is greater than the number of letters in the alphabet? How would you name the files that are outside the alphabet range?

  • 1

    ls *.jpg /| head -26 | perl -nlE 'system ("echo mv $_ ". chr(64+$.).".jpg")' and erase the "echo" if it looks good to you...

  • @Dener Carvalho Actually, I do not intend to leave inside the directory more than 26 images that in numbers corresponds to the alphabet. OK!

  • @Diegohenrique, as noted in the commentary, if he is "showing" the intended operation, removes the "echo" ls *.jpg /| head -26 | perl -nlE 'system ("mv $_ ". chr(64+$.).".jpg")'.

2 answers

1

The comment of Jjoao and the response of Lacobus got me to the shell-script:

 #!/bin/sh
 #
 # Por - Diego Henrique
 #
 # Programa - Renomear Arquivos Para Letras Do Alfabeto
 #
 # NOTA - Deve-se ter no máximo 26 Arquivos, alojado neste diretório 
 # Isso se dá ao número no qual corresponde as 26 Letras Alfabéticas 
 #
 n=0; LETRA=(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z);

 ls $HOME/*.png | while read IMG; do mv "$IMG" "${LETRA[n++]}.png"; 

 done

This is what worked perfectly on my Pinguin System - Damn Small Linux


About my own experience (mistakes/successes), I decided to leave this as an absolute answer.

1


Follow solution tested for the problem:

#/bin/bash

n=0

alfabeto=$(echo {A..Z})

find . -maxdepth 1 -name '*.jpg' -type f  | while read imagem; do

    mv "${imagem}" "${alfabeto[n++]}.jpg"

done

exit 0

#fim-de-arquivo#

Browser other questions tagged

You are not signed in. Login or sign up in order to post.