Recursive UTF-8 conversion (BASH)

Good Morning everyone.

Yesterday we had a Problem with non UTF-8 Files in the Project (happened in accident).

So I wrote a small bash script that should do the trick. It recursively converts all .php files to UTF-8 encoding

Requirements:

  • Linux system

  • iconv

Script:


for i in $(find -name '*.php');do encoding=$(file -bi $i | sed -e 's/.*[ ]charset=//'); iconv -f $encoding -t UTF-8 -o $i $i; done

Greetings

rho

Sorry, but it didn’t work for me. All the files ended with 0 byte after running the command. Fortunately I still have the backup.

We have ready to use commands for it in Yii repo:

Whoa, dude … find has got this nifty -exec param. At least you could’ve used xargs :o

@DASourcerer :P was really qnd and late at night…

@Samdark… wow didn’t know that, thanks for the hint