Methods of viewing and converting file encoding under Linux system

Source: Internet
Author: User

If you need to operate a file under Windows in Linux, you may often experience file encoding conversion problems. The default file format in Windows is GBK (gb2312), and Linux is generally UTF-8. Here's how to view the encoding of a file in Linux and how to encode a file.

View File Encoding
There are several ways to view file encoding in Linux:
(1). You can view the file encoding directly in VIM

The code is as follows:

: Set fileencoding
The file encoding format can be displayed.
If you just want to see other coded format files or want to solve the problem of using Vim to see the files garbled, you can
Add the following in the ~/.VIMRC file:

The code is as follows:

Set Encoding=utf-8 fileencodings=ucs-bom,utf-8,cp936
This allows vim to automatically recognize the file encoding (which can automatically identify UTF-8 or GBK encoded files), in fact, in accordance with the code list provided by Fileencodings, and if the appropriate encoding is not found, open with the latin-1 (ASCII) encoding.
(2). eNCA (if this command is not installed in your system, you can use sudo yum install-y eNCA installation) to view the file encoding

The code is as follows:

$ enca filename
Filename:universal Transformation Format 8 bits; UTF-8
CRLF Line Terminators
It is important to note that eNCA some GBK encoded files are not well recognized and appear when identified:
Unrecognized encoding

File Encoding Conversion
Because now with Linux, the original files in Windows are GBK encoded. So copy to Linux is garbled, file content can use Iconv to convert but a lot of Chinese file name or garbled, find a can convert file name Encoding command, is CONVMV.
CONVMV command detail parameters such as

The code is as follows:

Convmv-f gbk-t UTF-8 *.mp3
But this command does not convert directly, you can see the contrast before and after the conversion. If you want a straight conversion to add a parameter--notest

The code is as follows:

Convmv-f gbk-t UTF-8--notest *.mp3
The-f argument indicates the encoding before conversion, T is the converted encoding. Don't make a mistake about this. Otherwise, it may be garbled. There is also a parameter that is useful. This is---------this represents recursively converting all subdirectories under the current directory.
* Need to install convmv-1.10-1.el5.noarch.rpm
Better fool-type command-line tool eNCA, which not only intelligently identifies file encodings, but also supports batch conversions.
(1). Installation

The code is as follows:

$sudo Apt-get Install eNCA
(2). View Current file encoding

The code is as follows:

Enca-l ZH_CN Ip.txt
Simplified Chinese national Standard; GB2312 surrounded by/intermixed with non-text data
(3). Conversion command format is as follows
$enca-L Current language-x target encoded file name
For example, to turn all files in the current directory into Utf-8

The code is as follows:

Enca-l zh_cn-x Utf-8 *
Check the encoding of the file

The code is as follows:

Enca-l zh_cn File
Convert file encoding to "UTF-8" encoding

The code is as follows:

Enca-l zh_cn-x UTF-8 File
You can do this if you don't want to overwrite the original file

The code is as follows:

Enca-l zh_cn-x UTF-8 < file1 > File2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.