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?

2
  • see stackoverflow.com/questions/4544669/… Feb 16, 2012 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?
    – user401565
    Feb 16, 2012 at 11:41

4 Answers 4

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

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 {}\;
2
  • Why would you not run you terminal in utf8 all the time anyway? Let it be the lingua franca of exchange.
    – tchrist
    Feb 16, 2012 at 12:53
  • I do run my terminal in utf8 encoding
    – 2r2w
    Feb 16, 2012 at 13:22
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 {} \;
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" {}'

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.