The iconv
command is a utility that converts text from one character encoding to another. It is particularly useful when dealing with files that may not be in the desired encoding format, allowing users to ensure compatibility across different systems and applications.
The basic syntax of the iconv
command is as follows:
iconv [options] [arguments]
-f, --from-code=CODE
: Specifies the encoding of the input text.-t, --to-code=CODE
: Specifies the encoding for the output text.-o, --output=FILE
: Redirects the output to a specified file instead of standard output.-l, --list
: Lists all available encodings.Here are some practical examples of using the iconv
command:
Convert a file from UTF-8 to ISO-8859-1:
iconv -f UTF-8 -t ISO-8859-1 input.txt -o output.txt
Convert a file and display the output in the terminal:
iconv -f UTF-8 -t UTF-16 input.txt
List all available encodings:
iconv -l
Convert a string from Windows-1252 to UTF-8:
echo "Hello, World!" | iconv -f WINDOWS-1252 -t UTF-8
-o
option to save the converted output to a file, especially for large files.-l
option to list available encodings and find the correct one.