Skip to content

Batch convert files to utf-8

by Ofir on October 12th, 2008

I recently had to convert a directory structure of files from Windows created ISO-8859-1 format to standard Unicode UTF-8.

I created a shell script that recursively creates a copy of the directory tree converting to Unicode each PHP file it finds.

find . -name "*.php" -exec iconv -f ISO-8859-1 -t UTF-8 {} -o ../newdir_utf8/{} \;

It was originaly made to be run inside the directory I wanted to convert, but it can be easily changed in order to run from anywhere.

Hope it’s useful to someone having this problem.

Share or bookmark this post:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Twitter

From → linux

3 Comments
  1. # find . -name "test.html" -exec iconv -f CP1251 -t UTF-8 {} -o utf-8/{} \; didn’t work…. =-/

  2. Chavash permalink

    Hi, thanks for this post. It saved me hours of manual converting file for file. This solution were a little bit more complicated:
    http://ubuntuforums.org/showthread.php?t=9369

    And this solution just worked for one file:
    http://mediakey.dk/~cc/howto-convert-text-file-from-utf-8-to-iso-8859-1-encoding/

  3. hansom permalink

    #!/bin/sh

    count=0;
    for proc in `find /work/webserver/pfgrussia/data/bitrix/modules/compression/lang/ru/ -name “*.php”`; do iconv -f CP1251 -t UTF-8 $proc > ${proc//.php/.utf};
    echo “$count: ${proc//.php/.utf}”; ((count++)); done;
    for proc in `find /work/webserver/pfgrussia/data/bitrix/modules/compression/lang/ru/ -name “*.utf”`; do mv -f $proc ${proc//.utf/.php}; echo “$count: ${proc/
    /.utf/.php}”; ((count++)); done;

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS