🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

iconv Command

Intermediate Text Processing man(1)

Convert text between character encodings

📅 Updated: Mar 16, 2026
SYNTAX
iconv [OPTIONS] [-f FROM] [-t TO] [FILE...]

What Does iconv Do?

The iconv command converts text from one character encoding to another. It handles conversions between hundreds of encoding formats including UTF-8, UTF-16, ASCII, ISO-8859-1 (Latin-1), Windows-1252, Shift_JIS, EUC-KR, GB2312, and many more.

Character encoding issues are among the most common problems in text processing, especially when working with files from different operating systems, languages, or legacy systems. Files created on Windows often use Windows-1252 encoding, while modern Linux systems default to UTF-8. Database exports, CSV files from Excel, legacy application data, and international text all frequently require encoding conversion.

iconv is part of the GNU C Library (glibc) and is available on virtually every Linux and Unix system. It can process files, streams, and pipes, making it ideal for batch processing. For programmatic use, the iconv library provides C APIs used by many programming languages.

Options & Flags

OptionDescriptionExample
-f ENCODING Source (from) encoding iconv -f Windows-1252 -t UTF-8 file.txt
-t ENCODING Target (to) encoding iconv -f ISO-8859-1 -t UTF-8 file.txt
-o FILE Write output to file instead of stdout iconv -f Windows-1252 -t UTF-8 -o output.txt input.txt
-l List all supported encodings iconv -l
-c Silently discard characters that cannot be converted iconv -f UTF-8 -t ASCII -c file.txt
//TRANSLIT Transliterate characters that cannot be converted iconv -f UTF-8 -t ASCII//TRANSLIT file.txt
//IGNORE Ignore characters that cannot be converted iconv -f UTF-8 -t ASCII//IGNORE file.txt

Practical Examples

#1 Convert Windows to UTF-8

Convert a Windows-encoded CSV file to UTF-8. Common when processing Excel exports on Linux.
$ iconv -f Windows-1252 -t UTF-8 report.csv > report-utf8.csv

#2 Convert Latin-1 to UTF-8

Convert ISO-8859-1 (Latin-1) text to UTF-8. Use -o for in-place-like conversion.
$ iconv -f ISO-8859-1 -t UTF-8 -o output.txt input.txt

#3 Detect encoding first

Detect the encoding before converting. file -bi shows MIME type with charset. chardetect (Python) provides confidence score.
$ file -bi document.txt && chardetect document.txt
Output: text/plain; charset=iso-8859-1

#4 Convert to ASCII with transliteration

Convert accented characters to their ASCII approximations.
$ echo "café résumé naïve" | iconv -f UTF-8 -t ASCII//TRANSLIT
Output: cafe resume naive

#5 Strip non-ASCII characters

Remove all non-ASCII characters. Useful for cleaning data for systems that only support ASCII.
$ iconv -f UTF-8 -t ASCII//IGNORE < input.txt > clean.txt

#6 Batch convert multiple files

Convert all CSV files in a directory from Windows encoding to UTF-8.
$ for f in *.csv; do iconv -f Windows-1252 -t UTF-8 "$f" > "utf8_$f"; done

#7 Convert UTF-8 to UTF-16 for Windows

Convert to UTF-16LE (Little-Endian) for Windows applications that require UTF-16.
$ iconv -f UTF-8 -t UTF-16LE input.txt > output-win.txt

#8 List supported encodings

Show all supported encoding names. There are typically 100+ encodings available.
$ iconv -l | head -30

Tips & Best Practices

Always detect encoding first: Use file -bi file.txt to detect encoding before converting. Converting from the wrong source encoding corrupts the output. Install chardet for better detection.
Cannot convert in-place: iconv cannot read and write the same file. Always write to a new file: iconv -f X -t Y input.txt > output.txt && mv output.txt input.txt
UTF-8 is the standard: UTF-8 is the universal encoding for modern systems. When in doubt, convert everything to UTF-8. It supports all languages and is backwards-compatible with ASCII.
Use //TRANSLIT for lossy conversion: When converting to ASCII, use //TRANSLIT to approximate characters (é→e, ü→u) instead of failing or dropping them.

Frequently Asked Questions

How do I convert a file to UTF-8 in Linux?
First detect current encoding: file -bi file.txt. Then convert: iconv -f CURRENT_ENCODING -t UTF-8 file.txt > file-utf8.txt. Common source encodings: Windows-1252, ISO-8859-1, Shift_JIS.
How do I detect file encoding in Linux?
Use: file -bi file.txt (shows charset), or install chardetect (pip install chardet) for more accurate detection with confidence scores.
What is the difference between UTF-8 and ASCII?
ASCII only supports 128 characters (English letters, digits, punctuation). UTF-8 supports all Unicode characters (every language, emojis, symbols). UTF-8 is backwards-compatible with ASCII — valid ASCII is valid UTF-8.
How do I fix garbled text (mojibake)?
Garbled text means wrong encoding. Detect the real encoding with file or chardet, then convert: iconv -f REAL_ENCODING -t UTF-8 file.txt. Common cause: UTF-8 text interpreted as Latin-1 or vice versa.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →