You are here

Changing the charset/encoding of a file in Linux

Submitted by Druss on Tue, 2014-07-29 22:51

An easy avenue in Windows to change the encoding of a file is to open it in Notepad and then use the Save As option which allows you to specify the encoding that the file should be saved using ...

Linux does offer a bunch of solutions too, albeit perhaps relatively less simple:

  • One way is to change the encoding of the file in Vim.
  • The other way (and probably the more general solution) is to use iconv:
    1. First, use file to find out what the encoding of the file is at the moment:
      $ file import.txt
      which gave me,
      import.txt: ISO-8859 English text, with very long lines, with CRLF line terminators
    2. Now, use iconv to change the encoding (in my case, to UTF-8):
      $ iconv -f ISO-8859-1 -t UTF-8 import.txt > import2.txt

      The -f and -t switched denote the from file and the to file. Also,
      note that I'm using ISO-8859-1 as the encoding value rather than simply ISO-8859 (which is what file output). iconv does not recognise ISO-8859 as a legitimate value.

    Now, import2.txt contains a UTF-8 version of import.txt.