iconv Command in Linux



The iconv command in Linux converts text from one character encoding to another. Character encoding is a system that pairs each character in a set with a specific numeric value (code point) to represent it in digital form.

The iconv command reads the file of one text encoding and outputs the text into another. If no file is specified with the command or if a dash is given, it will read the standard input. Similarly, if no output file is specified, the output will be written to standard output.

The iconv command is a simple command-line interface that supports various encodings such as UTF-32, ISO-8859-15, UTF-7, US-ASCII, Windows-1252, MacRoman, UTF-16LE, and others.

Table of Contents

Here is a comprehensive guide to the options available with the iconv command −

Syntax of iconv Command

The syntax of the Linux iconv command is as follows −

iconv [options] [file]

The [options] field is used to specify the various command options. The [file] field is used to specify the file that needs to be converted.

Options of iconv Command

The options for the iconv command are listed below −

Flags Options Description
-f from-encoding --from-code= from-encoding To specify the input encoding
-t to-encoding --to-code= to-encoding To specify the output encoding

If //IGNORE is specified with to-encoding, then the characters that cannot be converted will be discarded

If //TRANSLATE is specified with to-encoding, then the characters that cannot be converted will be translated to close resembling characters

-l --list To list known coded character sets
-o output-file --output= output-file To specify the output file
-s --silent To suppress warnings
--verbose To get the detailed output
-? --help To display a brief help related to command its options
--usage To display the short usage message
-V --version To display the command version

Examples of iconv Command in Linux

This section demonstrates the usage of the iconv command in Linux with examples −

Listing the Supported Encodings

To list all supported encodings, use the -l or --list options with the iconv command −

iconv -l
iconv Command in Linux1

Converting Encoding of a File

To convert a file to another encoding, first determine the current encoding of the file. The content of the text file is shown in the following image −

iconv Command in Linux2

To determine the character encoding of the file, use the file command with the -i option. For example, to find the character encoding of the input-file.txt file, use the following command −

file -i input-file.txt
iconv Command in Linux3

The output image shows that the file character ending is UTF-8. To convert the encoding of a file from UTF-8 to UTF-16, use the -f and -t options with input and output encodings −

iconv -f UTF-8 -t UTF-16 input-file.txt
iconv Command in Linux4

The above command will display the conversion in the standard output. Similarly, to convert it to ASCII, use −

iconv -f UTF-8 -t ASCII input-file.txt
iconv Command in Linux5

The output says the characters at position 14 the characters cannot be translated. This indicates that the input file contains a character at position 14 that cannot be represented in ASCII. ASCII only supports a limited set of characters (0-127), while UTF-8 can represent a much wider range, including special characters and symbols.

Converting Encoding and Saving the Output to a File

To convert the character encoding of a file and save the output to a separate file, use the -o or --output options −

iconv -f UTF-8 -t ASCII input-file.txt -o output-file.txt

To verify, determine the encoding type using the file command −

file -i output-file.txt
iconv Command in Linux6

Discarding Non-Convertible Characters

To ignore non-convertible characters, use //IGNORE with the -t option encoding.

iconv -f UTF-8 -t ASCII input-file.txt//IGNORE -o output-file.txt
iconv Command in Linux7

Translating Non-Convertible Characters

Instead of discarding the non-convertible characters, and converting them to closely resembling characters, use the //TRANSLATE with the -t option encoding.

iconv -f UTF-8 -t ASCII input-file.txt//TRANSLATE -o output-file.txt

Converting Standard Input

To convert the standard input, use the iconv command in the following way −

echo "Hello, World! $ € Привет мир! こんにちは世界!" | iconv -f UTF-8 -t ASCII

The input text contains non-ASCII characters.

Getting Verbose Output

To produce verbose output, use the --verbose option −

iconv --verbose -f UTF-8 -t ASCII input-file.txt -o output-file.txt

Displaying Help

To display the command help, use the -? or --help option −

iconv -?

Conclusion

The iconv command is a powerful command line tool in Linux that converts the character encoding of one text to another. In many Linux distributions, this utility is pre-installed.

Converting character encodings is important because it ensures text works properly across different systems, keeps data safe, supports multiple languages, and saves space. It helps ensure that text is easy to read and accessible everywhere.

Advertisements