Skip to content

Instantly share code, notes, and snippets.

@nandalopes
Last active June 26, 2020 23:31
Show Gist options
  • Save nandalopes/26dfc9b6fa17102ce4ffb0b611a6e98e to your computer and use it in GitHub Desktop.
Save nandalopes/26dfc9b6fa17102ce4ffb0b611a6e98e to your computer and use it in GitHub Desktop.
Find accented filenames

Find accented filenames and rename

find help about regex flavors

find -regextype -h: Tipo desconhecido de expressão regular ‘-h’; tipos válidos são:

‘findutils-default’,
‘ed’,
‘emacs’,
‘gnu-awk’,
‘grep’,
‘posix-awk’,
‘awk’,
‘posix-basic’,
‘posix-egrep’,
‘egrep’,
‘posix-extended’,
‘posix-minimal-basic’,
‘sed’.
find -regextype awk -not -iregex "[^áàãâéêíóõôúçñ]+"
find -depth -regextype awk -not -iregex "[0-9a-z \-\_\,\./]+"
find -depth -regextype posix-awk -not -iregex "[0-9a-z \-\_\,\./]+"

Comando que busca apenas arquivos com a última parte acentuada

find -depth -regextype posix-awk -iregex ".*[áàãâéêíóõôúçñ]+[^/]+"

Comando que busca apenas arquivos com qualquer parte acentuada

find -depth -regextype awk -not -iregex "[0-9a-z/ \-\_\,\.\(\)]+"

Comando que encontra arquivos com final fora da norma, mas inclui .

find -depth -regextype posix-awk -not -iregex ".*/[0-9a-z/ \-\_\,\.\(\)]+" |\
while read infile;
do
  if [ $infile != '.' ];
  then
    bnamefile="$(basename "$infile")";
    outfile="$(echo "$bnamefile" | iconv -f UTF8 -t ASCII//TRANSLIT)";
    bnamedir="$(dirname "$infile")";
    echo $bnamefile $outfile;
    mv -vi "$infile" "$bnamedir/$outfile";
  fi
done

Comando para remover acentos dos nomes de arquivos:

find -regextype awk -not -regex "[^áÁàÀãÃâÂéÉêÊíÍóÓõÕôÔúÚçÇñÑ]+" \
| awk '{print length, $0}' \
| sort -nr \
| sed 's/^[[:digit:]]* //' \
| while read dirfile; \
do
  outfile="$(echo "$(basename "$dirfile")" | unaccent UTF-8)"; \
  mv -iv "$dirfile" "$(dirname "$dirfile")/$outfile"; \
done

Usando 'iconv' e 'mv'

find -depth -regextype posix-awk -iregex ".*[áàãâéêíóõôúçñ]+[^/]+" |\
while read infile;
do
  bnamefile="$(basename "$infile")";
  outfile="$(echo "$bnamefile" | iconv -f UTF8 -t ASCII//TRANSLIT)";

  bnamedir="$(dirname "$infile")";
  echo $bnamefile $outfile;
  mv -vi "$infile" "$bnamedir/$outfile";
done

Usando 'unaccent' e 'rename'

find -depth -regextype posix-awk -iregex ".*[áàãâéêíóõôúçñ]+[^/]+" |\
while read infile;
do
  bnamefile="$(basename "$infile")";
  outfile="$(unaccent UTF-8 $bnamefile)";

  bnamedir="$(dirname "$infile")";
  echo $bnamefile $outfile;
  rename -n "s/$bnamefile/$outfile/" "$infile";
done

Estratégias para renomear

Nome original

bnamefile="$(basename "$infile")";

Com ferramenta 'iconv'

outfile="$(echo "$bnamefile" | iconv -f UTF8 -t ASCII//TRANSLIT)";

Com ferramenta 'unaccent'

outfile="$(unaccent UTF-8 $bnamefile)";

Rename folders

find -depth -name _Others -exec mv -i {} {}/../_Other \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment