masdeseiscaracteres
7/10/2014 - 11:36 AM

Automatically detect the encoding of a file an convert it to UTF-8 (use with care)

Automatically detect the encoding of a file an convert it to UTF-8 (use with care)

#!/bin/bash
if [[ "$#" -ne 1 ]]; then
    echo "Usage: ./toUTF8 <dir>"
	exit 1
fi
OUTPUT_ENC=UTF-8
FOLDER=$1
ICONV="iconv"
# Convert
find $1 -type f -name "*" | while read fn; do
cp ${fn} ${fn}.bak
INPUT_ENC=`file -b --mime-encoding ${fn}`
echo "Converting $fn from " $INPUT_ENC " to " $OUTPUT_ENC " ..."
$ICONV -f $INPUT_ENC -t $OUTPUT_ENC < ${fn}.bak > ${fn}
rm ${fn}.bak
done