View File Encoding
There are several ways to view file encodings in Linux:
1. The file encoding can be viewed directly in Vim
: 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 identify the file encoding (which automatically identifies the UTF-8 or GBK encoded file), in fact, in accordance with the fileencodings provided by the encoding list to try, if not found the appropriate encoding, the latin-1 (ASCII) encoding opened.
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, the ICONV command format is as follows:
iconv-f encoding-t encoding inputfile
such as converting a UTF-8 encoded file into a GBK encoding.
Iconv-f UTF-8-T GBK file1-o file2
To view the file Encoding files command
file ip.txt ip.txt:utf-8 Unicode text, with escape sequences
One, encode and convert file content encoding with iconv command Iconv command is used to convert the encoding of the specified file, the default output to the standard output device, or the output file can be specified. 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 enumerates all known character Set output controls:-C ignores invalid words from output Character-O,--output=file output file-S,--silent close warning--verbose print progress information-?,--help gives the system a list of requests--usage give a brief usage information-V,--version print path Sequence 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 Na tional standard; GB2312 surrounded by/intermixed with non-text data
Pasting
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 file encoding enca-l zh_cn-x UTF-8 file if you do not want to overwrite the original
View file encoding and modification code under Linux