How to convert file encoding in linux

07-04-2023

The content of this article mainly explains how to convert the file encoding in linux, interested friends may wish to take a look. The method described in this article is simple, quick and practical. Let the editor take you to learn how to convert the file encoding in linux!


  1. View the file Encoding\

file

Use the file command and add the -i or –mime parameter to view a file character encoding.

  1. Coding conversion tool iconv

iconv options -f from-encoding -t to-encoding inputfile(s)

-f or –from-code indicates the input encoding, while -t or –to-encoding specifies output encoding.

  1. List all supported coded character sets

iconv

  1. Example of file encoding conversion

iconv< /p>

Converts a file from ISO-8859-1 encoding to UTF-8 encoding.

If the output code is appended with the //IGNORE string, those characters that cannot be converted will not be converted, and after conversion, the program will display an error information.

If the string //TRANSLIT is added after the output encoding in the above example (UTF-8//TRANSLIT), the characters to be converted will try to use literal translation in principle. That is, if a character cannot be represented in the output encoding scheme, it will be replaced by a character with a similar shape.

If a character is not in the output encoding,And cannot be transliterated, it will be replaced by a question mark ? in the output file.

  1. Convert multiple files to UTF-8 encoding
    You can use shell script

#/ Bin/bash
###Add values_ Replace here with input encoding
From_ ENCODING="value_here"
###Output encoding (UTF-8)
TO_ ENCODING="UTF-8"
###Conversion command
CONVERT="iconv - f $FROM-ENCODING - t $TO-ENCODING"
###Convert multiple files using a loop
For file in *. txt; Do
$CONVERT "$file" - o "${file%. txt}. utf8. converted"
Done
Exit


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us