iconv

text processingLinux/Unix
The iconv command is one of the most frequently used commands in Linux/Unix-like operating systems. iconv Convert text from one character encoding to another

Quick Reference

Command Name:

iconv

Category:

text processing

Platform:

Linux/Unix

Basic Usage:

iconv [options] [arguments]

Common Use Cases

    Syntax

    iconv [options] [-f from-encoding] [-t to-encoding] [input-file]...

    Options

    Option Description
    -f, --from-code=NAME Encoding of original text
    -t, --to-code=NAME Encoding for output
    -l, --list List all known coded character sets
    -c Omit invalid characters from output
    -o, --output=FILE Output file
    -s, --silent Suppress warnings
    --verbose Print progress information
    -?, --help Give this help list
    --usage Give a short usage message
    --version Print program version

    Examples

    How to Use These Examples

    The examples below show common ways to use the iconv command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    iconv -f ISO-8859-1 -t UTF-8 input.txt
    Convert text from ISO-8859-1 to UTF-8 encoding.
    iconv -f ISO-8859-1 -t UTF-8 input.txt > output.txt
    Convert text and save to a new file.
    iconv -l
    List all known character encodings. # Advanced Examples Advanced iconv -f ISO-8859-1 -t UTF-8 -o output.txt input.txt Convert text and write to output.txt. echo "café" | iconv -f UTF-8 -t ASCII//TRANSLIT Convert UTF-8 text to ASCII with transliteration. iconv -c -f UTF-8 -t ASCII input.txt Convert with skipping characters that cannot be converted.
    cat input.txt | iconv -f ISO-8859-1 -t UTF-8
    Use iconv in a pipeline.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The iconv utility converts text from one character encoding to another and is essential for handling text in different languages and systems. It's particularly useful in multilingual environments or when dealing with files from different operating systems. Key features of iconv: 1. Encoding Conversion: iconv's primary function is to convert text between different character encodings, such as UTF-8, ISO-8859-1, ASCII, and many others. 2. Wide Range of Encodings: iconv supports a vast array of character encodings, which can be listed using the -l option. This includes international standards, legacy encodings, and platform-specific encodings. 3. Flexible Input/Output: iconv can read from files specified as arguments or from standard input, making it suitable for use in pipelines. Output can be directed to a file or to standard output. 4. Error Handling: The -c option allows iconv to skip characters that cannot be converted in the target encoding, which is useful when dealing with text that may contain characters not representable in the destination encoding. 5. Transliteration: Some implementations support the //TRANSLIT suffix for the target encoding, which attempts to represent characters that don't exist in the target encoding with similar-looking characters (e.g., converting 'é' to 'e' when converting to ASCII). 6. Byte Order Mark Handling: iconv can handle and generate Byte Order Marks (BOMs) for encodings like UTF-16 and UTF-32, which indicate the endianness of the text data. 7. Integration with System Libraries: iconv leverages the system's character conversion libraries, ensuring it has access to the most up-to-date encoding definitions. iconv is particularly valuable in scenarios such as: - Preparing text files for cross-platform use - Processing files from legacy systems - Ensuring proper display of international text in applications - Converting web content between different encodings - Preparing text for systems with specific encoding requirements The command is available on most Unix-like systems, including Linux and macOS, and similar functionality is available on Windows through tools like GNU iconv for Windows or PowerShell's encoding parameters.

    Related Commands

    These commands are frequently used alongside iconv or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the iconv command works in different scenarios.

    $ iconv
    View All Commands