Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Bash script for recursive file convertion windows-1251 --> utf-8
#!/bin/bash
# Recursive file convertion windows-1251 --> utf-8
# Place this file in the root of your site, add execute permission and run
# Converts *.php, *.html, *.css, *.js files.
# To add file type by extension, e.g. *.cgi, add '-o -name "*.cgi"' to the find command
find ./ -name "*.php" -o -name "*.html" -o -name "*.css" -o -name "*.js" -type f |
while read file
do
echo " $file"
mv $file $file.icv
iconv -f WINDOWS-1251 -t UTF-8 $file.icv > $file
rm -f $file.icv
done

Batname commented Apr 15, 2014

Thanks

Great script. Works perfectly, even on cygwin (which I have to use at work). Thanks a lot.

save my day

ranold commented Apr 7, 2016

thank you!

nymo commented Jan 20, 2017

Thanks! Really good script.

obojdi commented Feb 17, 2017

Confirmed working on cygwin, many thanks @akost!

That script is bad. since iconv doesn't detect if file is already UTF-8. So it will ruin your files if run on directory with files in mixed encodings. Running iconv more than once is guaranteed to screw your files too.

What you actually should use for this operation is enca, since it will correctly detect input encoding and act accordingly.

After installing enca, just run this one-liner & your files will be UTF-8 in no time:
find ./ -name "*.php" -o -name "*.html" -o -name "*.css" -o -name "*.js" -type f | while read file; do enca -x UTF-8 $file; done;

worked like a charm
Thank you so much!!!!

ayzakh commented Aug 29, 2017

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment