5

I try this:

find . -exec iconv -f iso8859-2 -t utf-8 {} \;

but output goes to the screen, not to the same file. How to do it?

Nips
  • 13,162
  • 23
  • 65
  • 103
  • see http://stackoverflow.com/questions/4544669/batch-convert-latin-1-files-to-utf-8-using-iconv – Kai Sternad Feb 16 '12 at 11:40
  • You can also see the file with "cat" I think your files problem is not encoding! maybe your file can include some color codes, so you can't see the normal file.. please try to "cat" command and write again.. see normally or not? –  Feb 16 '12 at 11:41

4 Answers4

13

Try this:

find . -type f -print -exec iconv -f iso8859-2 -t utf-8 -o {}.converted {} \; -exec mv {}.converted {} \;

It will use temp file with '.converted' suffix (extension) and then will move it to original name, so be careful if you have files with '.converted' suffixes (I don't think you have).

Also this script is not safe for filenames containing spaces, so for more safety you should double-quote: "{}" instead of {} and "{}.converted" instead of {}.converted

wobmene
  • 1,108
  • 9
  • 14
1

I found this method worked well for me, especially where I had multiple file encodings and multiple file extensions.

Create a vim script called script.vim:

set bomb
set fileencoding=utf-8
wq

Then run the script on the file extensions you wish to target:

find . -type f \( -iname "*.html" -o -iname "*.htm" -o -iname "*.php" -o -iname "*.css" -o -iname "*.less" -o -iname "*.js" \) -exec vim -S script.vim {} \;
Sebastien
  • 2,607
  • 9
  • 30
  • 40
1

No one proposed a way to automatically detect encoding and recode.

Here is an example to recode to UTF-8 all HTM/HTML files from master branch of a GIT.

git ls-tree master -r --name-only | grep htm | xargs -n1 -I{} bash -c 'recode "$(file -b --mime-encoding {})..utf-8" {}'

TiCPU
  • 160
  • 4
1

read about enconv.
If you need to convert to your current terminal encoding you can do it like that:

find . -exec enconv -L czech {}\;

Or exactly what you wanted:

find . -exec enconv -L czech -x utf8 {}\;
2r2w
  • 1,384
  • 1
  • 12
  • 29