iconv command in Linux with Examples
The iconv command is used to convert some text in one encoding into another encoding. If no input file is provided then it reads from standard input. Similarly, if no output file is given then it writes to standard output. If no from-encoding or to-encoding is provided then it uses current local's character encoding.
Here, we will explore the syntax, options, and use cases of the iconv command, along with additional examples to ensure you master the tool effectively.
Syntax
iconv [options] [-f from-encoding] [-t to-encoding] [inputfile]...
Commonly Used Options with iconv
Option | Description |
---|---|
-f from-encoding, --from-code=from-encoding | Use from-encoding for input characters. |
-t to-encoding, --to-code=to-encoding | Use to-encoding for output characters. |
-l, --list | List all known character set encodings. |
-c | Silently discard characters that cannot be converted instead of terminating when encountering such characters. |
-o outputfile, --output=outputfile | Use outputfile for output. |
--verbose | Print progress information on standard error when processing multiple files. |
Important Notes on iconv Usage
1. Handling Conversion Errors: If the string //IGNORE is appended to to-encoding, characters that cannot be converted are discarded and an error is printed after conversion.
2. Backup Files Before Conversion: If the string //TRANSLIT is appended to to-encoding, characters that cannot be represented in the target character set, it can be approximated through one or several similar looking characters.
Examples of Using iconv
Let us look at some of the examples of iconv command in Linux to better understand the concept.
Example 1: Converting Text from UTF-8 to ASCII
echo abc ß ? € à?ç | iconv -f UTF-8 -t ASCII//TRANSLIT

In this example, the iconv command converts a string from UTF-8 encoding to ASCII. The //TRANSLIT option ensures that characters like "ß" and "€" are approximated into their closest ASCII equivalents.
Example 2: Listing All Character Set Encodings
iconv -l

This command lists all character encodings supported by the iconv command, allowing you to choose the correct encoding format for conversion.
Example 3: Reading and writing from a file
iconv -f UTF-8 -t ASCII//TRANSLIT -o out.txt in.txt

Conclusion
The iconv command is an indispensable utility for converting text between different encodings in Linux. Understanding its options and how to apply them will help you maintain text file integrity and avoid encoding-related errors. By mastering iconv, you can efficiently handle a variety of encoding conversions, ensuring that your data remains consistent and compatible across different environments.