Skip to content

Instantly share code, notes, and snippets.

@ronanguilloux
Last active April 25, 2021 07:17
Show Gist options
  • Save ronanguilloux/8319847 to your computer and use it in GitHub Desktop.
Save ronanguilloux/8319847 to your computer and use it in GitHub Desktop.
batch in bash to replace accents in filenames
#! /bin/bash
#
# slugify.sh by Ronan
#
# Distributed under terms of the MIT license.
#
cd photos
for file in *.png; do
filename=${file%.*}
#file_clean=`echo $filename | tr -cs "[:alnum:]_" _ `
#file_clean=`echo $filename | iconv -c -f utf8 -t ascii`
#file_clean=`echo $filename | iconv -f utf8 -t ascii//TRANSLIT | iconv -c -f utf8 -t ascii`
file_clean=`echo $filename | iconv -f utf8 -t ascii//TRANSLIT`
final="$file_clean.png"
echo "mv \"$file\" \"$final\" "
done
cd ..
@a-fabre
Copy link

a-fabre commented Jul 17, 2018

I generalized your script as follows:

echo $1
find $1 -regextype posix-egrep -regex '.[{ÁÉÍÓÚÀÈÌÒÙÄËÏÖÜ].' | while read filename; do
file_clean=echo $filename | iconv -f utf8 -t ascii//TRANSLIT
mv -n "$filename" "$file_clean"
echo "$file_clean"
done

Hope this helps someone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment