What kind of recursively remove accented characters from file names

1

I tried other scripts I found on the internet (* the ones I found), but without success. So I want to know from some of you how to do this automated / recursive task.

Shell Script Retrieved from: link

set meta-flag on


set output-meta on


set convert-meta off

#!/bin/bash

NOMBRES_FICHEROS=$(ls)

for i in $NOMBRES_FICHEROS
do
        echo '...TRATANDO EL FICHERO: '$i'...'
        cat $i | tr [:upper:] [:lower:] > def_$i
        cat $i | tr "ÁÉÍÓÚÑ" "AEIOUN" > def_$i
        mv def_$i $i
done

No comments, it did not work! Maybe I do not have Mr. Bash as a CLI command interpreter.

Shell Script Retrieved from: link

#!/bin/bash


for file in *;
do
newname='echo "$file" | iconv -t 'ascii//TRANSLIT''


mv "$file" "$newname"


done

This other one I found also did not run on my system, I do not think I could even have the Bash interpreter, just Ash linked to Bash.

Shell Script Retrieved from: link

#!/bin/sh

for i in *
do
    # ^  Quando esse chapeuzinho aparece no início de uma lista de caracteres significa negação;
    j='echo "$i" | sed 's/[^A-Za-z0-9_.]//g''
    # Vai remover todos os acentos das letras indicadas - https://www.vivaolinux.com.br/topico/Shell-Script/Script-para-retirar-acentos
    j='echo "$j" | sed 'y/áÁàÀãÃâÂéÉêÊíÍóÓõÕôÔúÚçÇ/aAaAaAaAeEeEiIoOoOoOuUcC/''
    mv "$i" "$j"
    echo "Acento(s) removido de: $i"
done

This has already worked in parts, I say this, because the change makes a bit weird change, change until characters without accent also [where it does not have].

These were the ones I found.

    
asked by anonymous 23.08.2018 / 23:15

1 answer

4

You can use the iconv command. This function will convert the characters from one encoding to another. The command is simple.

Content of a file:

iconv -f UTF8 -t ASCII//TRANSLIT < input.txt > output.txt

Content of a variable:

$psr_new_value = $(echo $psr_variable | iconv -f UTF8 -t ASCII//TRANSLIT)

Command Explanation:

 iconv  -f UTF8  -t ASCII//TRANSLIT < input.txt > output.txt
└──┬──┘└───┬───┘└───┬───┘  └───┬───┘
   │       │        │          │
   │       │        │          └──── Ele pode ser aproximado por meio de um ou vários caracteres semelhantes.Information Interchange
   │       │        └─────────────── Codificação de destino
   │       └──────────────────────── Codificação de entrada
   └──────────────────────────────── Comando

Full Example:

Below is the structure and code used in the test.

File structure before:

.
├── andré.txt
├── cajá.php
├── joão.txt
├── ñãõ.txt
└── vou_à_praia.txt

0 directories, 5 files

Code:

#/bin/sh

for file in *.txt;
do
    mv $file $(echo $file | iconv -f UTF8 -t ASCII//TRANSLIT)
done

File structure after:

.
├── andre.txt
├── cajá.php
├── joao.txt
├── nao.txt
└── vou_a_praia.txt

0 directories, 5 files
    
23.08.2018 / 23:33