What it does
iconv converts text from one character encoding to another. This is critical when moving between systems using different encodings (e.g., UTF-8, ISO-8859-1, Windows-1252, UTF-16).
How it works (mechanical)
-fspecifies the source encoding (from).-tspecifies the target encoding (to).- Reads input file or STDIN and writes converted output.
- Can ignore or transliterate invalid characters.
10 Practical Examples
# 1) Convert ISO-8859-1 to UTF-8 iconv -f ISO-8859-1 -t UTF-8 input.txt -o output.txt
# 2) Convert Windows-1252 to UTF-8 iconv -f WINDOWS-1252 -t UTF-8 file.txt -o newfile.txt
# 3) Convert UTF-8 to UTF-16 iconv -f UTF-8 -t UTF-16 file.txt -o file-utf16.txt
# 4) Ignore invalid characters iconv -f UTF-8 -t ASCII//IGNORE file.txt -o ascii.txt
# 5) Transliterate characters (approximate) iconv -f UTF-8 -t ASCII//TRANSLIT file.txt
# 6) Convert from STDIN cat file.txt | iconv -f UTF-8 -t ISO-8859-1
# 7) Detect supported encodings iconv -l
# 8) Batch convert files for f in *.txt; do iconv -f ISO-8859-1 -t UTF-8 "$f" -o "utf8_$f"; done
# 9) Convert CSV imported from Windows iconv -f WINDOWS-1252 -t UTF-8 data.csv -o clean.csv
# 10) Validate encoding (errors if invalid) iconv -f UTF-8 -t UTF-8 file.txt -o /dev/null
Notes & Gotchas
- Wrong source encoding causes corrupted output.
- //IGNORE silently drops invalid characters.
- //TRANSLIT approximates characters (é → e).
- Always confirm original encoding when importing legacy data.
Historical Context
iconv originates from POSIX and is implemented in glibc and other libc variants. It became critical as UTF-8 replaced older regional encodings.
Modern Equivalent (if applicable)
Many languages (Python, Perl, Go) handle encoding natively, but iconv remains the simplest universal CLI tool for bulk conversion.
Related Commands
- file — detect file type/encoding hints
- recode — advanced encoding conversion
- dos2unix — line ending normalization
- sed, awk — post-processing