Change encoding of all files in folder on Unix, Linux or MacOS

2009-04-05

Here is some simple bash script to bulk change encoding of all files in directory. It uses iconv for change of encoding.

#!/bin/bash

if [ $# -ne 3 ]; then
echo “usage: “$0″ dest_directory original_encoding destination_encoding”
exit
fi;

dir=$1
from=$2
to=$3

if [ ! -d $dir ]; then
mkdir $dir
fi

for f in $( ls . ); do
if [ -f $f ]; then
iconv -f $from -t $to $f > $dir$f
fi
done

To run it, create a file recode.sh and paste above code into it. Then in terminal run

chmod +x recode.sh

adn then just

./recode.sh encoded_dir/ ISO-8859-13 UTF-8

Command mentioned above recodes all files in current directory from  ISO-8859-13 encoding to UTF-8 and copies new files to encoded_dir/ directory.