iconv command in Linux with Examples
Last Updated :
01 Oct, 2024
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.
Similar Reads
hdparm command in Linux with Examples "hdparm" (i.e., hard disk parameter) is one of the command line programs for Linux that is used to handle disk devices and hard disks. With the help of this command, you can get statistics about the hard disk, alter writing intervals, acoustic management, and DMA settings. It can also set parameters
4 min read
Head Command in Linux With Examples Need to quickly view the beginning of a file in Linux? The head command is your best option. This essential command-line tool enables users, developers, and system administrators to preview the start of log files, configuration files, CSV datasets, and other text documents in seconds.The head comman
6 min read
help Command in Linux with examples If youâre new to the Linux operating system and struggling with command-line utilities, the help command is one of the first tools you should learn. As its name suggests, the 'help' command provides detailed information about built-in shell commands, making it an essential resource for beginners and
5 min read
hexdump command in Linux with examples The 'hexdump' command in Linux is a versatile utility used to display file content or data from standard input in a human-readable format. It is invaluable for programmers and system administrators for debugging binary data, analyzing file structures, and verifying data integrity. Here we will get a
5 min read
How to Display Command History in Linux | history Command The history command in Linux is essential for terminal users, enabling them to view, search, and manipulate previously executed commands. Now, mastering this Linux command allows for efficient commands of commands, automation of repetitive tasks, and troubleshooting without the need to retype length
3 min read
host command in Linux with examples host command in Linux system is used for DNS (Domain Name System) lookup operations. In simple words, this command is used to find the IP address of a particular domain name or if you want to find out the domain name of a particular IP address the host command becomes handy. You can also find more s
2 min read
hostid command in Linux with examples hostid is a command in Linux that is used to display the Host's ID in hexadecimal format. It provides a quick and straightforward way to retrieve the host ID, allowing administrators to associate it with software licenses or perform system-specific operations. Syntax of `hostid` command in Linuxhost
1 min read
hostname command in Linux with examples hostname command in Linux is used to obtain the DNS (Domain Name System) name and set the system's hostname or NIS (Network Information System) domain name. A hostname is a name given to a computer and attached to the network. Its main purpose is to uniquely identify over a network. Syntax of the `h
5 min read
hostnamectl command in Linux with Examples hostnamectl command provides a proper API used to control Linux system hostname and change its related settings. The command also helps to change the hostname without actually locating and editing the /etc/hostname file on a given system. Syntax: hostnamectl [OPTIONS...] COMMAND .... Where COMMAND c
2 min read
htop command in Linux with examples htop command in Linux system is a command line utility that allows the user to interactively monitor the systemâs vital resources or serverâs processes in real-time. htop is a newer program compared to top command, and it offers many improvements over top command. htop supports mouse operation, uses
4 min read