Linux_View file encoding and modify encoding

If you need to manipulate files under Windows in Linux, then you may often encounter the problem of file encoding conversion. The default file format in Windows is GBK (gb2312), while Linux is generally UTF-8. Let's introduce how to view the encoding of the file and how to convert the encoding of the file in Linux.

· View file encoding

There are several ways to view file encoding in Linux:

  1. You can directly view the file encoding in Vim

  :set fileencoding displays the file encoding format.

 If you just want to view files in other encoding formats or to solve the problem of garbled files with Vim, you can add the following to the ~/.vimrc file:

  set encoding=utf-8 fileencodings=ucs-bom,utf-8,cp936 In this way, vim can automatically recognize the file encoding (it can automatically recognize UTF-8 or GBK encoded files), in fact, it is to try according to the encoding list provided by fileencodings , if no suitable encoding is found, open it with latin-1 (ASCII) encoding.

  2. enca (if this command is not installed in your system, you can install it with sudo yum install -y enca) to view the file encoding

  $ enca filename filename: Universal transformation format 8 bits; UTF-8 CRLF line terminators It should be noted that enca does not recognize some GBK encoded files very well, and the recognition will appear: Unrecognized encoding 

·File encoding conversion

  1. Convert file encoding directly in Vim, such as converting a file to utf-8 format

  :set fileencoding=utf-8 2. enconv converts the file encoding, for example, to convert a GBK-encoded file into UTF-8 encoding, the operation is as follows

  enconv -L zh_CN -x UTF-8 filename 3. iconv conversion, iconv command format is as follows: iconv -f encoding -t encoding inputfile For example, convert a UTF-8 encoded file to GBK encoding

  iconv -f GBK -t UTF-8 file1 -o file2

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326993053&siteId=291194637