Home  >  Article  >  Operation and Maintenance  >  How to convert file encoding in linux

How to convert file encoding in linux

PHPz
PHPzforward
2023-05-11 16:52:122741browse
  1. View the encoding of the file\

file

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

  1. Encoding 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 encoding character sets

iconv
  1. Example of file encoding conversion

iconv

Convert the file from ISO-8859-1 encoding to UTF-8 encoding.

If the //IGNORE string is added after the output encoding, characters that cannot be converted will not be converted, and after conversion, the program will display an error message.

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 the form translation 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 deciphered, it will be replaced by a question mark? in the output file.

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

#!/bin/bash
### 将 values_here 替换为输入编码
FROM_ENCODING="value_here"
### 输出编码 (UTF-8)
TO_ENCODING="UTF-8"
### 转换命令
CONVERT=" iconv  -f   $FROM_ENCODING  -t   $TO_ENCODING"
### 使用循环转换多个文件
for  file  in  *.txt; do
$CONVERT   "$file"   -o  "${file%.txt}.utf8.converted"
done
exit

The above is the detailed content of How to convert file encoding in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete