Linux iconv command
iconv is a command-line tool in Linux systems used to convert the content of text files between different character encodings. It can handle various common character encoding formats such as UTF-8, GB2312, ISO-8859, etc., solving the problem of incompatible text encodings between different systems.
Why character encoding conversion is needed
Character encoding problems often lead to the following situations:
- Text files copied from Windows to Linux appear as garbled characters
- Web page content displays abnormally in different browsers
- Encoding issues in source code files developed across platforms
- Compatibility issues when handling internationalized multilingual text
The iconv command is designed precisely to solve these problems.
Basic Syntax
iconv [选项] -f 原编码 -t 目标编码 [输入文件]
Commonly used options
| Option | Description |
|---|---|
-f |
Specify the character encoding of the original file (from) |
-t |
Specify the target character encoding to convert to (to) |
-o |
Specify the output file (by default, output to standard output) |
-l |
List all supported encoding formats |
-c |
Silently ignore characters that cannot be converted (by default, an error is reported) |
--verbose |
Display detailed information during the conversion process |
Supported encoding formats
To view all encoding formats supported by the system, you can run:
iconv -l
Common encoding formats include:
- UTF-8
- GB2312
- GBK
- GB18030
- BIG5
- ISO-8859-1 (Latin-1)
- ASCII
- EUC-JP (Japanese)
- SHIFT_JIS (Japanese)
Practical Application Examples
Example 1: Basic encoding conversion
Convert a GB2312-encoded file to UTF-8:
iconv -f GB2312 -t UTF-8 input.txt -o output.txt
Example 2: Handling standard input and output
Converting text through a pipe:
cat gb2312_file.txt | iconv -f GB2312 -t UTF-8
Example 3: Ignoring characters that cannot be converted
iconv -f GBK -t UTF-8//IGNORE input.txt -o output.txt
Example 4: Batch converting all files in a directory
Example
for
file
in
.txt;
do
iconv
-f
GB2312
-t
UTF-
8
"$file"
-o
"utf8_${file}"
done
Common Problem Solutions
Problem 1: Incorrect encoding detection
If you don't know a file's original encoding, try common encodings first:
Example
Try GB2312
iconv
-f
GB2312
-t
UTF-
8
input.txt
If that fails, try GBK
iconv
-f
GBK
-t
UTF-
8
input.txt
Problem 2: Garbled text remains after conversion
The encoding may have been specified incorrectly; try:
Example
Use //TRANSLIT to handle special characters
iconv
-f
GBK
-t
UTF-
8
//
TRANSLIT input.txt
Or use //IGNORE to skip characters that cannot be converted
iconv
-f
GBK
-t
UTF-
8
//
IGNORE input.txt
Problem 3: Insufficient memory when converting large files
For large files, you can use split-stream processing:
Example
split
-l
10000
bigfile.txt part_
for
part
in
part_
;
do
iconv
-f
GB2312
-t
UTF-
8
"$part"
-o
"utf8_${part}"
done
cat
utf8_part_
&
gt; bigfile_utf8.txt
Best Practice Recommendations
- Back up the original file: Back up before conversion to prevent data loss
- Test the conversion: First test the conversion on a small file
- Standardize the encoding: Use a unified encoding standard in your project (UTF-8 recommended)
- Check the result: After conversion, use the
filecommand to check the file encoding - Automate the process: Write commonly used conversion commands into scripts for reuse
Combining with other tools
Batch conversion with the find command
find . -name "*.txt" -exec bash -c 'iconv -f GB2312 -t UTF-8 "{}" > "{}.utf8"' ;
Checking encoding with vim
vim -c "set fileencoding" filename.txt
Detecting encoding with the file command
file -i filename.txt
Advanced Tips
Converting filename encodings
Example
Convert GBK-encoded filenames to UTF-8
convmv
-f
GBK
-t
UTF-
8
--notest
.txt
Handling HTML/XML files
Example
Preserve encoding declarations in files
iconv
-f
GB2312
-t
UTF-
8
input.html
|
sed
's/charset=gb2312/charset=utf-8/i'
&
gt; output.html
Create encoding conversion aliases
in ~/.bashrc add the following:
Example
alias
gb2utf8
=
'iconv -f GB2312 -t UTF-8'
alias
big52utf8
=
'iconv -f BIG5 -t UTF-8'
Then run source ~/.bashrc Make the aliases take effect.
Summary
iconv is a powerful tool for handling text encoding issues on Linux. After studying this article, you should be able to:
- Understand the basic concepts of character encoding conversion
- Master the basic syntax and common options of the iconv command
- Solve encoding conversion problems in your daily work
- Apply advanced techniques to handle complex scenarios
Remember to always back up before working with important files and verify the results after conversion. As a universal encoding standard, UTF-8 is the recommended choice for most modern applications.
Linux Command Reference