If the file encoding is not recognized, it may be garbled in the file, this time need to remove garbled
View File Encoding
There are several ways to view file encodings in Linux:
1. InVimYou can view the file encoding directly in the
: Set fileencoding
You can display the file encoding format.
If you just want to see other encoded files or if you want to solve the problem of viewing files garbled with Vim, you can
Add the following to the ~/.VIMRC file:
Set Encoding=utf-8 fileencodings=ucs-bom,utf-8,cp936
This allows vim to automatically recognize the file encoding (which automatically recognizes UTF-8 or GBK encoded files), in fact, according toFileencodings provides an attempt to encode a list, and if no appropriate encoding is found, it is opened with latin-1 (ASCII) encoding.
2. eNCA (if you do not have this command installed on your system, you can use sudo yum install-y eNCA installation) to view the file encoding
$ enca filename
Filename:universal Transformation Format 8 bits; UTF-8
CRLF Line Terminators
It is important to note that eNCA is not very good at identifying certain GBK encoded files and will appear when identified:
Unrecognized encoding
File Encoding Conversion
1. Convert file encoding directly into Vim, such as converting a file to Utf-8 format
: Set Fileencoding=utf-8
2. Enconv conversion file encoding, such as 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 as follows:
iconv-f ENCODING-T encoding Inputfile
iconv-f< Span style= "font-family: Microsoft Jacob Black; font-size:16px; " >utf-8 -t
To view the file Encoding files command
File Ip.txt ip.txt:utf-8 Unicode text, with escape sequences
One, the use of iconv command encoding conversion file content encoding conversion iconv command is used to convert the encoding of the specified file, the default output to the standard output device, you can also specify the output file. usage: iconv [Options ...] [File ...] The following options are available: input/output format specification: -F,--from-code= name raw text encoding -T,--to-code= name output encoding information: -L,--list Enumerate all known character set output controls:- C ignores invalid characters from output- o,--output=file output file -S,--silent close warning --verbose Print progress information -?,--Help gives the system a list of--usage to give a brief usage information- V,--version Print program version number example: iconv-f utf-8-T gb2312 Aaa.txt >bbb.txt This command reads the Aaa.txt file, converts from Utf-8 encoding to gb2312 encoding, and its output is directed to the Bbb.txt file.
Second, file name encoding conversion
Because now with Linux, the original files in Windows are encoded with GBK. So copy to Linux is garbled, file content can be used iconv to convert but a lot of Chinese filename or garbled, find a can convert the file name Encoding command, is CONVMV.
CONVMV Command Detail Parameters For example
Convmv-f gbk-t UTF-8 *.mp3
However, this command does not convert directly, you can see the contrast before and after the conversion. If you want the conversion to be straight, add the parameter--notest
Convmv-f gbk-t UTF-8--notest *.mp3
The-f parameter indicates the encoding before the conversion, and-T is the converted encoding. Don't make a mistake about it. Otherwise, it may be garbled. There is one more parameter that is useful. IS-r This indicates that all subdirectories under the current directory are converted recursively.
* Need to install convmv-1.10-1.el5.noarch.rpm
Third,a better eNCA command-line tool, which not only intelligently identifies the encoding of files, but also supports batch conversions. 1. Installing the $sudo apt-get install eNCA 2. View current file encoding enca-l ZH_CN ip.txt Simplified Chinese national standard; GB2312 surrounded by/intermixed with Non-text data 3. The conversion command format is as follows $enca-L current language-x target encoding file name for example, to convert all files in the current directory to Utf-8 enca-l zh_cn-x utf-8 * Check the encoding of the file enca-l ZH_CN file convert the encoding to "UTF-8" encoding enca-l zh_cn-x UTF-8 File If you do not want to overwrite the original files can be enca-l zh_cn-x UTF-8 < file1 > file2
[Turn] Linux to view file encoding and modification code