LoginSignup
1

More than 5 years have passed since last update.

ディレクトリ内ファイルの一括 latin1→ UTF8 文字コード変換

Posted at

linux 初心者なのでもっといいやり方あるかもしれません。いや、あるでしょう。ないわけがない。

latin-1 で書かれていた古い c ソースを保存しようとするとUTF-8 化が走って文字化けが発生してしまうので、先回りして全部 BOM付きUTF-8 化してやるぅ!

というワンライナー。

$ find ./src/ -name "*.c" -or -name "*.h" -exec \
sh -c 'iconv -f WINDOWS-1252 -t UTF-8 {} | \
nkf -W8 -w8 > temp.txt && \
mv temp.txt {}' \;

latin1 から utf8 への変換は 0x80-0xff 領域の変換が nkf じゃうまくいかないので、iconv を使用。

iconv だと BOM をつけてくれないので nkf で付加。

結果を temp.txt に保存して、mv で元のファイルに上書き移動。

以上。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1