ファイルのエンコーヂングを一括変換する需要があったので、車輪の再発明かもしれませんが自分の勉強がてら書いてみました。
カレントディレクトリ以下にある全てのencoding Shift-JISのファイルをUTF-8に変換し、全ての¥
を\
に書き換えます。
encoding_converter.sh
#!/bin/sh
dir="`pwd`"
# $dir以下の全てのファイルに対して行う。
for file in `find $dir -type f`;
do
echo $file
# encoding_converter.sh自身だった場合にスキップする。
if expr $file : ".*$0" > /dev/null; then
echo "$0 itself. skip!"
continue
fi
# すでにutf-8になっていた場合はtmpfに移すだけ。
if expr "`file -bI $file`" : ".*utf-8" > /dev/null; then
echo 'already utf-8'
mv $file tmpf
# そうでなければshift-jisからutf-8に変換する。
else
echo 'change encoding'
iconv -f shift-jis -t utf-8 $file > tmpf
fi
# 変換されたものの¥を\に書き換える。
echo 'replace ¥ with \\'
tr '¥' '\\' < tmpf > $file
done
rm tmpf
exit