Skip to content

Instantly share code, notes, and snippets.

@timotheemoulin
Last active August 13, 2018 09:32
Show Gist options
  • Save timotheemoulin/61c55d15eac78afb29a4916bc66b8d0c to your computer and use it in GitHub Desktop.
Save timotheemoulin/61c55d15eac78afb29a4916bc66b8d0c to your computer and use it in GitHub Desktop.
convert files from iso-8859-1 to utf8
#!/usr/bin/env bash
# Timothée Moulin
#
# Install : copy this script anywhere (you can place it in the (/usr/local/bin) directory
# and give it the execution permission : chmod +x convert-8859-utf8.sh
#
# You can use this script either by calling it directly to convert a single file
# or you can give it to the "find -exec" command to convert a batch of files.
# e.g.
# find . -type f -name "*.php" -exec convert-8859-utf8.sh {} \;
# convert-8859-utf8.sh your-wrongly-encoded-file.txt
# detect source encoding
encoding=`file -i $1 | cut -f 2 -d";" | cut -f 2 -d=`
# convert only iso-5589-1 (and unkown)
case $encoding in
iso-8859-1)
iconv -c -f iso8859-1 -t utf-8 $1 -o $1.utf8
# we create a temporary file to avoid Bus error
mv $1.utf8 $1
;;
unknown-8bit)
iconv -c -f iso8859-1 -t utf-8 $1 -o $1.utf8
mv $1.utf8 $1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment