Nips
Nips

Reputation: 13860

How to change encoding in many files?

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?

Upvotes: 6

Views: 11234

Answers (4)

Tiky
Tiky

Reputation: 160

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" {}'

Upvotes: 1

Sebastien
Sebastien

Reputation: 2679

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 {} \;

Upvotes: 1

wobmene
wobmene

Reputation: 1138

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

Upvotes: 14

2r2w
2r2w

Reputation: 1509

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 {}\;

Upvotes: 1

Related Questions