Home>Article>Operation and Maintenance> What to do if files are garbled under Linux?
What should I do if the file is garbled under Linux?
We often encounter garbled characters when operating files under Linux. I searched for some solutions on the Internet, hoping to be helpful to everyone.
If you need to operate files under windows in Linux, you may often encounter file encoding conversion problems. The default file format in Windows is GBK (gb2312), while Linux is generally UTF-8.
The following introduces how to check the encoding of files and how to convert files in Linux.
Recommended: "Linux Tutorial"
View file encoding
View files in Linux Encoding can be done in the following ways:
1. You can directly view the file encoding in Vim
:set fileencoding
to display the file encoding format.
If you just want to view files in other encoding formats or want to solve the problem of using Vim to view garbled files, then you can add the following content to the
~/.vimrc file:
set encoding=utf-8 fileencodings=ucs-bom,utf-8,cp936
In this way, vim can automatically identify the file encoding (it can automatically identify UTF-8 or GBK encoded files). In fact, it is to try according to the encoding list provided by fileencodings. If no suitable encoding is found, use latin-1 (ASCII) encoding turned on.
File encoding conversion
1. Convert the file encoding directly in Vim, such as converting a file into utf-8 format
:set fileencoding=utf-8
iconv conversion, the command format of iconv is as follows:
iconv -f encoding -t encoding inputfile
For example, convert a UTF-8 encoded file Into GBK encoding
iconv -f GBK -t UTF-8 file1 -o file2
iconv -f gbk -t utf8 linux common commands.txt > linux common commands.txt.utf8
File name encoding conversion:
When copying files from Linux to Windows or from Windows to Linux, sometimes the Chinese file name will be garbled, and this problem will occur. The reason is that the default Chinese encoding of file names in Windows is GBK, while the default file name encoding in Linux is UTF8. Due to inconsistent encoding, the file name is garbled. To solve this problem, the file name needs to be transcoded.
In Linux, a tool convmv is specially provided to convert the file name encoding. It can convert the file name from GBK to UTF-8 encoding, or from UTF-8 to GBK.
First check whether convmv is installed on your system. If not, use:
yum -y install convmv
Install.
Let’s take a look at the specific usage of convmv:
convmv -f source encoding -t new encoding [option] file name
Commonly used Parameters:
-r Recursively process subfolders
-notest Actual operation, please note that by default, the actual operation of the file is not performed, but only a test.
–list displays all supported encodings
–unescap You can do some escaping, such as turning it into a space
For example, we have a utf8 encoded file name and convert it to GBK encoding, the command is as follows:
convmv -f UTF-8 -t GBK –notest utf8 encoded file name
After this conversion, the "utf8 encoded file name" will be converted into GBK encoding (Only the file name encoding is converted, the file content will not change)
The above is the detailed content of What to do if files are garbled under Linux?. For more information, please follow other related articles on the PHP Chinese website!