Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert File Encoding format under Linux system

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to convert file encoding format under the Linux system". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First, use iconv command to convert file content coding.

Usage: iconv [option.] [file.]

The following options are available:

Input / output format specification:

-f,-- from-code= name original text encoding

-t,-- to-code= name output code

Information:

-l,-- list lists all known character sets

Output control:

-c ignores invalid characters from the output

-o,-- output=FILE output file

-s,-- silent shutdown warning

-- verbose print progress information

Help gives a list of requests for help from the system.

-- usage gives brief usage information

-V,-- version printer version number

Example:

The code is as follows:

Iconv-f gb2312-t utf-8 aaa.txt > bbb.txt

This command reads the aaa.txt file, converts from gb2312 encoding to utf-8 encoding, and its output is directed to the bbb.txt file.

Note: the txt generated by WordPad under windows is generally gb18030 encoding. If you specify an error, the following error will be reported:

Iconv: illegal input sequence at unknown 6071

II. File name coding conversion

Because now using linux, the original files in windows are encoded in GBK. So copy to linux is garbled, file content can be converted with iconv, but a lot of Chinese file names are still garbled, find a command that can convert file name coding, that is, convmv.

Detailed parameters of convmv command

For example

Convmv-f GBK-t UTF-8 * .mp3

However, this command will not convert directly, you can see the comparison before and after the conversion. If you want a direct conversion, you need to add a parameter-- notest.

Convmv-f GBK-t UTF-8-- notest * .mp3

The-f parameter indicates the pre-conversion code, and-t is the converted code. Make no mistake about this. Otherwise, it may still be garbled. There is another parameter that is useful. This means to recursively convert all subdirectories under the current directory.

* convmv-1.10-1.el5.noarch.rpm needs to be installed

Third, better stupid command line tool enca

It can not only intelligently identify the encoding of files, but also support batch conversion.

1. Installation

The code is as follows:

$sudo apt-get install enca

two。 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

The 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

The code is as follows:

Enca-L zh_CN-x utf-8 *

The code is as follows:

Enca-L zh_CN file

Check the encoding of the file

The code is as follows:

Enca-L zh_CN-x UTF-8 file

Convert file encoding to "UTF-8" encoding

The code is as follows:

Enca-L zh_CN-x UTF-8

< file1 >

File2

If you don't want to overwrite the original file, it's easy.

PS:. How to view the code

Method 1:

The code is as follows:

File filename

Method 2: 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 view files in other encoding formats or want to solve the problem of garbled viewing files with Vim, you can use the

Add the following to the ~ / .vimrc file

The code is as follows:

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

In this way, you can let vim automatically recognize the file encoding (you can automatically identify UTF-8 or GBK-encoded files), which is actually based on the

The encoding list provided by fileencodings tries to open it with latin-1 (ASCII) encoding if a suitable encoding is not found.

This is the end of the content of "how to convert the file encoding format under the Linux system". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report