A brief introduction to Linux's coding and coding conversion methods

  • 2020-06-23 02:36:38
  • OfStack

If you need to work in Linux with files under windows, you may often run into problems with file encoding conversions. The default file format in Windows is GBK(gb2312), while Linux1 is UTF-8 in general. Here's how to view the encoding of a file and how to encode a file in Linux.

View file encoding

You can view file encoding in Linux in several ways:

1. You can view the file code directly in Vim

:set fileencoding

The file encoding format can be displayed.

If you just want to view files in other encoding formats or want to solve the problem of using Vim to view scrambled files, you can do so in the

Add the following to the ~/.vimrc file:


set encoding=utf-8 fileencodings=ucs-bom,utf-8,cp936

In this way, you can ask vim to automatically identify file codes (es32EN-8 or GBK files can be automatically identified). In fact, you can follow the list of codes provided by fileencodings to try. If you do not find a suitable code, you can open it with latin-1(ASCII).

File encoding conversion

1. Directly convert file encoding in Vim, such as converting one file to utf-8 format


:set fileencoding=utf-8

2. iconv conversion, iconv command format is as follows:


iconv -f encoding -t encoding inputfile

For example, convert an UTF-8 encoded file to an GBK encoded file


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

iconv-f ES58en-ES59en utf8 linux common command > linux common command. txt.utf8

Filename encoding conversion:

From Linux windows copy files or from windows Linux copy files, can appear sometimes Chinese filenames gibberish, appear this kind of problem because, the default for GBK windows file name in Chinese code, and the default file name in Linux coding for UTF8, due to no 1 encoding, so caused the filename garbled question, solve the problem need to transcoding file name.

convmv provides a tool to convert filename encoding from GBK to ES84en-8 or from ES85en-8 to GBK.

First check if convmv is installed on your system. If not, use:


yum -y install convmv

The installation.

The following is the specific usage of convmv:

convmv-f source code -t new code [Options] filename

Common parameters:

-ES105en recursively processes subfolders

notest to do the actual work, please note that the default is not to do the actual work on the file, but only to experiment.

--list displays all supported encodings

unescap can do 1 escape, such as change %20 to a space

For example, we have a file name encoded by utf8, which is converted to GBK. The command is as follows:

convmv-f ES123en-8-ES124en GBK --notest utf8 encoded file name

After this conversion, "utf8 encoded file name "will be converted to GBK (only the conversion of file name encoding, the file content will not change)


Related articles: