When you download a massive CSV data export from an old legacy mainframe, or receive a text document from a colleague using an outdated Windows system, you will often run into character encoding errors. Instead of proper quotation marks or accented characters, the file might display strings of question marks or strange garbled glyphs (known as mojibake). This happens when a file encoded in an older standard like ISO-8859-1 is forced to display in a modern UTF-8 terminal. To seamlessly convert files from one encoding standard to another, you should use the Linux iconv command.
How the iconv Command Works
The iconv (Internationalization Conversion) utility is a standard GNU tool designed specifically to transcode text files. It reads text in one encoding, instantly converts every single character, and writes the freshly encoded text to the standard output. Because it supports hundreds of different character sets, it is the ultimate tool for fixing corrupted text files.
Listing Available Encodings
Before you convert a file, you need to know the exact internal name of the encoding standards you are dealing with. You can force iconv to print a massive list of every single character encoding it supports by running the command with the -l (list) flag.
iconv -l
You will see hundreds of aliases like UTF-8, WINDOWS-1252, ASCII, and SHIFT_JIS. You must use these exact strings when formatting your conversion command.
Converting a File to UTF-8
The most common use case for iconv is modernizing an old file into the universal UTF-8 standard. To do this, you must specify the “from” encoding using the -f flag, and the “to” encoding using the -t flag, followed by the original file name.
iconv -f ISO-8859-1 -t UTF-8 legacy_data.csv
By default, iconv will print the newly converted text directly to your terminal screen. This is helpful for a quick visual inspection to ensure the garbled characters are fixed, but it does not actually save the data.
To permanently save the fixed file, you must use standard bash redirection (>) to write the output into a brand new file.
iconv -f ISO-8859-1 -t UTF-8 legacy_data.csv > modernized_data.csv
Crucial Warning: Do not attempt to redirect the output back into the exact same original file (e.g., > legacy_data.csv). Doing so will instantly overwrite the original file before the conversion completes, resulting in total data loss. Always write to a new file name.