72

Possible Duplicate:
How can I convert multiple files to UTF-8 encoding using *nix command line tools?

Okay, now that I can detect the encoding, I know that my encoding is using charset=iso-8859-1 instead of utf. How can I convert this?

Casebash
  • 7,477

1 Answers1

122

Use iconv:

iconv -f iso-8859-1 -t utf-8 < file > file.new
Donald Duck
  • 2,569
u1686_grawity
  • 452,512
  • Ok, that might make sense indeed (though such straightforward answers are found in the other questions too). No need for the < by the way. – Arjan Jun 13 '10 at 12:44
  • 7
    @Arjan Note that Google points to this answer first.. – tomdemuyt Jun 18 '11 at 19:18
  • 2
    I did this.. now that file was completly emptied....... and I don't have any backup of it.. great.......... – OZZIE Nov 03 '18 at 14:33
  • 1
    @OZZIE then I am afraid you did it wrong – Matt Sephton Feb 11 '19 at 01:06
  • To clarify: should that be <file> (no spaces) as in "substitute your filename here", or; are you redirecting the output with > and the preceding < is an error? There is a suggested edit (as well as the first comment) which suggests removing the <. I guess this is due to the spaces around file inside the <...> making it look confusing and as if the < is a typo. What increases the confusion is that the second file.new does not have <...>. You might want to fix that. I'd suggest that iconv -f iso-8859-1 -t utf-8 <filename> <filename>.new is better... at least it's consistant – Greenonline Jul 01 '22 at 06:46
  • 2
    @Greenonline: Both < and > are intentional – they redirect input from file and output into file.new. The input redirection is optional since iconv could read its input directly, but I wanted it to be present for consistency. – u1686_grawity Jul 01 '22 at 07:50
  • You should add your comment to the answer, as it may be interesting to the uninitiated about the input redirection being optional - maybe show both forms of the command line, up to you. Nevertheless, the command line looks better now... :-) – Greenonline Jul 01 '22 at 09:11
  • 1
    take note that file.new MUST NOT BE the same as file, otherwise the result will indeed be an empty file – René Roth Sep 18 '23 at 13:49