How to convert file encoding in linux

PHPz
Release: 2023-05-11 16:52:12
forward
2811 people have browsed it
  1. View the encoding of the file\

file
Copy after login

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)
Copy after login

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

  1. List all supported encoding character sets

iconv
Copy after login
Copy after login
  1. Example of file encoding conversion

iconv
Copy after login
Copy after login

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
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template