Session and utilities
The iconv command
Converts text between character encodings, for example from Latin-1 to UTF-8, so accents and the ñ display correctly on another system. It works on bytes, so it helps to know which encoding the file starts from.
Most useful options
-f <codificación>Source encoding, for example UTF-8, ISO-8859-1 or CP1252-t <codificación>Target encoding. It can take //TRANSLIT or //IGNORE-o <archivo>Writes the result to a file instead of the screen. It overwrites without asking-cSilently omits characters that cannot be converted-lLists the encodings the system supportsCopy-ready examples
$ iconv -f ISO-8859-1 -t UTF-8 viejo.txt > nuevo.txtconverts a Latin-1 file to UTF-8 and saves the result separately.$ printf 'A\xf1o\n' | iconv -f ISO-8859-1 -t UTF-8the same text through a pipe: the ñ in Latin-1 comes out correct in UTF-8.Example output
Año
$ iconv -f UTF-16 -t UTF-8 exportado.txt > exportado-utf8.txtconverts to UTF-8 a file saved as UTF-16, common in some Windows exports.$ file -bi notas.txtbefore converting, check which encoding the file uses.Example output
text/plain; charset=iso-8859-1
$ iconv -c -f UTF-8 -t ASCII informe.txtdrops what does not fit in ASCII, such as the ñ and accents.$ iconv -f UTF-8 -t ASCII//TRANSLIT informe.txtreplaces accents with their base letter (é becomes e), depending on the locale.$ iconv -l | grep -i latinlooks up the Latin-1 variants your system has.You can try them in the browser terminal: it does not touch your computer.
Common errors
| Message | Cause | Fix |
|---|---|---|
| iconv: nofile: No such file or directory | The input file does not exist or is misspelled. | Check the path with ls before converting. |
Typical mistakes
- If you do not know the source encoding, iconv will not guess it: it converts wrongly without warning. Check it first with file -bi.
- With -f UTF-8 on a Latin-1 file, the result comes out wrong or fails, depending on the bytes.
- -o overwrites the destination without asking. Do not point -o at the original file.
Differences from similar commands
- dos2unix: changes only the line endings (CRLF to LF). It does not touch the character encoding.
- file: detects the encoding of a file, but does not change it.
- xxd: shows the bytes in hexadecimal. It helps you see how a specific character is stored.
recode: does something similar with more text formats. It is not installed on every distribution.
FAQ
Why does ñ show up instead of ñ?
Double encoding: the bytes were already UTF-8 and got read as Latin-1, or the other way round. Check the source with file -bi.
What is the difference between //TRANSLIT and -c?
//TRANSLIT tries to replace each character with a similar one; -c removes them without replacing.
How do I find out which encoding names it accepts?
Run iconv -l, which lists them all. Names are not case sensitive, and UTF8 and UTF-8 usually work the same.
Learn to actually use it
The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.
Start the free course