Home > Hosting > Server

How to convert file encoding in linux

2023-07-04 15:03:25

<p style="text-align: left;">The content of this article mainly explains how to convert the file encoding in linux, interested friends may wish to take a look. The method described in this article is simple, quick and practical. Let the editor take you to learn how to convert the file encoding in linux!</p><p><br/></p><ol data-id="o9d5df12-FNYDLV6B" class=" list-paddingleft-2"><li><p style="text-align: left;">View the file Encoding</p></li></ol><p style="text-align: left;">file</p><p data-id="p838747a-HgM2c7Mh" style="text-align: left;">Use the file command and add the -i or –mime parameter to view a file character encoding.</p><ol start="2" data-id="oa950d4e-73gB9Ebe" class=" list-paddingleft-2"><li><p style="text-align: left;">Coding conversion tool iconv</p></li></ol><p style="text-align: left;">iconv options -f from-encoding -t to-encoding inputfile(s)</p><p data-id="p838747a-oL7dZGGR" style="text-align: left;">-f or –from-code indicates the input encoding, while -t or –to-encoding specifies output encoding.</p><ol start="3" data-id="o2efa9a6-R53mVlRl" class=" list-paddingleft-2"><li><p style="text-align: left;">List all supported coded character sets</p></li></ol><p style="text-align: left;">iconv</p><ol start="4" data-id="ofc672e1-l8POISYN" class=" list-paddingleft-2"><li><p style="text-align: left;">Example of file encoding conversion</p></li></ol><p style="text-align: left;">iconv&lt; /p&gt;</p><p data-id="p838747a-jhC48f99" style="text-align: left;">Converts a file from ISO-8859-1 encoding to UTF-8 encoding.</p><p data-id="p838747a-pb82I3Ni" style="text-align: left;">If the output code is appended with the //IGNORE string, those characters that cannot be converted will not be converted, and after conversion, the program will display an error information.</p><p data-id="p838747a-867YMkbb" style="text-align: left;">If the string //TRANSLIT is added after the output encoding in the above example (UTF-8//TRANSLIT), the characters to be converted will try to use literal translation in principle. That is, if a character cannot be represented in the output encoding scheme, it will be replaced by a character with a similar shape.</p><p data-id="p838747a-sBT3Va7B" style="text-align: left;">If a character is not in the output encoding,And cannot be transliterated, it will be replaced by a question mark ? in the output file.</p><ol start="5" data-id="oc5f4d4e-gEbmCk65" class=" list-paddingleft-2"><li><p style="text-align: left;">Convert multiple files to UTF-8 encoding<br/>You can use shell script</p></li></ol><pre class="hljs bash" style="margin-top: 8px; margin-bottom: 8px; padding: 15px; box-sizing: border-box; overflow-x: auto; background-color: rgb(240, 242, 245); color: rgb(45, 48, 55); font-size: 14px; border: 1px solid rgb(219, 225, 232); border-radius: 4px; white-space: pre-wrap; overflow-wrap: break-word; word-break: break-all; text-align: left;">#/&nbsp;Bin/bash ###Add&nbsp;values_&nbsp;Replace&nbsp;here&nbsp;with&nbsp;input&nbsp;encoding From_&nbsp;ENCODING=&quot;value_here&quot; ###Output&nbsp;encoding&nbsp;(UTF-8) TO_&nbsp;ENCODING=&quot;UTF-8&quot; ###Conversion&nbsp;command CONVERT=&quot;iconv&nbsp;-&nbsp;f&nbsp;$FROM-ENCODING&nbsp;-&nbsp;t&nbsp;$TO-ENCODING&quot; ###Convert&nbsp;multiple&nbsp;files&nbsp;using&nbsp;a&nbsp;loop For&nbsp;file&nbsp;in&nbsp;*.&nbsp;txt;&nbsp;Do $CONVERT&nbsp;&quot;$file&quot;&nbsp;-&nbsp;o&nbsp;&quot;${file%.&nbsp;txt}.&nbsp;utf8.&nbsp;converted&quot; Done Exit</pre><p><br/></p>


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us