In Linux how to make a change to the character encoding of a file

Source: Internet
Author: User

questions : in my Linux system there is a subtitle file encoded as iso-8859-1, some of which do not display properly, I want to change the text to UTF8 encoding. In Linux, is there a good tool to convert the character encoding of a text file?

As we know, the computer can only handle low-level binary values and cannot handle characters directly. When a text file is stored, each character in the file is mapped to a binary value, and it is these "binary values" that are actually stored on the hard disk. Then, when the program opens the text file, all binary values are read in and mapped back to the original readable characters. This "Save and open" process works well only if all programs that need access to the file are able to "understand" its encoding, that is, binary value-to-character mapping, which ensures a round-trip process of understandable data.

If different programs use different encodings to process the same file, special characters in the source file will not display properly. The special characters here refer to characters that are non-English letters, such as accented characters (such as Ñ,á,ü).

And then the question is: 1 How do we determine what character encoding is used for a certain text file? 2) How do we convert the file to the selected character encoding?

Step One

To determine the character encoding of the file, we use a command-line tool called "File". Because the file command is a standard UNIX program, we can find it in all modern Linux distributions.

Run the following command:

Step Two

The next step is to see what kind of file encoding your Linux system supports. To do this, we use the tool named Iconv and the "-l" option (lowercase for L) to list all currently supported encodings.

The Iconv tool is part of the GNU libc library, so it is out of the box in all Linux distributions.

Step Three

After we have selected the target encoding in the encoding supported by our Linux system, run the following command to complete the encoding transformation:

$ iconv-f old_encoding-t new_encoding filename

For example, convert iso-8859-1 encoding to UTF-8 encoding:

$ iconv-f iso-8859-1 -t utf-8

In Linux how to make a change to the character encoding of a file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.