#! /bin/sh ############################################################################## # Copyright 2005 Pablo Chinea (a.k.a. Khertz) (email: khertz@gmail.com) # # # # This program is free software; you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation; either version 2 of the License, or # # (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program; if not, write to the Free Software # # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # ############################################################################## # # Version 1.0 # # This program change recursively the encoding of every file into a directory # to UTF-8 encoding. # # I can't guarantee the good operation of this script, make a backup before # and USE IT UNDER YOUR OWN RISK. # # Check command line parameters if [[ ! -d $1 ]] then echo "Command line error! Usage: $0 "; exit 1 fi # Create the temporal filename tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$ trap "rm -f $tempfile" 0 1 2 5 15 # Locate recursively every files into the directory. find $1 -type f > $tempfile; # Open the temporaly file. exec 3< $tempfile while read -u3 filename do # Check the file type and the file encoding typefile=$(file --mime "$filename"); if [[ -n $(echo $typefile | grep "text/") ]] then encoding=$(echo $typefile | cut -d'=' -f2); if [[ $encoding != "utf-8" ]] then if [[ -n "$(iconv --list | grep -i $encoding)" ]] then # Change the file encoding. tempfile2=`tempfile 2>/dev/null` || tempfile2=/tmp/test$$ trap "rm -f $tempfile2" 0 1 2 5 15 iconv --from-code=$encoding --to-code=utf-8 "$filename" > $tempfile2 mv -f $tempfile2 "$filename" echo "$filename: $encoding -> utf8"; fi fi fi; done; exec 3<&-