Which Way to Recursively Remove Accented Characters from File Names

Asked

Viewed 1,166 times

2

I tried other scripts I found online (*os I found), but to no avail. So I want to know some of you(s) how to do this automated/recursive task.

Shell Script Taken from: https://tsgii.blogspot.com/

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 didn’t work! Maybe I don’t have Mr. Bash as CLI command interpreter

Shell Script Taken from: http://www.alexandrepinheiro.com/2011/05/shell-script-para-retirar-os-acentos.html

#!/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 think that could not even the interpreter Bash, only Ash linked to Bash.

Shell Script Taken from: https://www.vivaolinux.com.br/topico/Shell-Script/Remover-caracteres-esquisitos/

#!/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 amendment makes a somewhat strange exchange, even changes characters without accent also [where there is no].

These are the ones I found.

1 answer

4


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

Contents 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)

Explanation of the Command:

 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

Complete 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
  • To identify your shell, utilize echo $SHELL

  • As I wrote before editing, please check. Anything I can remove and edit later.

  • @Diegohenrique I added an example with the /bin/sh

  • I tested but when running the command which iconv discovered that I do not have and I did not find in the repository of the system Slitaz 5. A pity, since it could work. But for what it’s worth!

  • 1

    Valdeirpsr can suggest @Diegohenrique to compile libiconv himself, first download at https://ftp.gnu.org/pub/gnu/libiconv/ --- extract the chosen .tar.gz, then navigate to the folder with the extracted content and execute: ./configure --prefix=/usr/local, make and make install . PS: I did not test

  • @Guilhermenascimento I saw your suggestion, but it would be interesting something retro-compatibility [something you already have in virtually every operating system] /bin/sh

  • @Guilhermenascimento As I tried -> $ ls -w1 *.txt | while read line; do mv "$line" "$(echo $line | tr 'ÁÉÍÓÚÑ' 'AEIOUN')"; done

  • @Guilhermenascimento To rename several files you can use one loop, to enter the subfolders you would have to change the command ls for find. The command rename uses the same syntax regex of sed, but you can manage without it if you need to... mv does not work at all because of spaces in names, errors. So it is necessary to remove spaces -> $ for i in *.txt; do mv "$i" "echo $i | tr ' ' '_'"; done

  • @Diego and Valdeir, I assure you one thing, iconv (or libs equivalent) do not exist by chance, want to do in hand no ÁÉÍÓÚÑ => AEIOUN can work if you are sure of the CODEC, understand that the Á utf-8 is not the same thing as Á latin1 (window-1252, iso-8859-1 and equivalent/approximate), iconv detects coding, can even try to create something in hand, but it will be much more complex than a single line, probably will have to enumerate all possible variations of codecs, create something to remove permanently if codec is not supported [...]

  • [...] , when it reaches a level of complexity so maybe the best is to use what already exists, to embark a software for this, to create something in [tag:c] or [tag:c++] own and so solve once and for all with something guaranteed to avoid codec problems.

  • I agree with @Guilhermenascimento. I downloaded the operating system (OS) and saw that it is quite simple (less than 50MB the live-cd version). In some tests with sed, awk, tr etc. You even have a simple result, but it is not what you expect. If you want something simpler, you can install the perl and make a script basic. If you plan to install this ONLY on several devices, you can maintain a version with docker.

  • @Guilhermenascimento Grateful for your beautiful explanation, I also agree with what you say. I also noticed this, the varied "codification". For now I’m using iconv in the distro Cdlinux-0.9.7.1 the same brings this tool with it, besides being a distro Live-CD and USB, can be installed side-by-side of Windows OS. In my case I had a way to manually install in the hard drive. For now, this serving me very well, aliases have been using it for a long time. From it, I use iconv in it and I will think later on to elaborate a universal shell script, to give account task where there is no iconv

Show 7 more comments

Browser other questions tagged

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