Convert input.txt from UTF-8 to ISO-8859-1 and write output.txt.
All input characters are representable in the target encoding.
Which command is appropriate?
iconv uses -f for the input encoding and -t for the output encoding.
Detailed explanation
iconv -f UTF-8 -t ISO-8859-1 input.txt > output.txtCorrect. It converts from UTF-8 to ISO-8859-1.
iconv -f ISO-8859-1 -t UTF-8 input.txt > output.txtIncorrect. It reverses the source and target encodings.
iconv -t UTF-8 -f UTF-8 input.txt > output.txtIncorrect. Both sides are UTF-8, so it does not convert to ISO-8859-1.
locale -f UTF-8 -t ISO-8859-1 input.txtIncorrect. locale does not convert file contents.
Try it yourself
An example you can run in a temporary verification environment.
printf 'Cafe
' | iconv -f UTF-8 -t ISO-8859-1 | od -An -tx1Expected result
43 61 66 65 0aKey points
- -f means from
- -t means to
- Unrepresentable characters can fail
Notes
- Environment: GNU libiconvまたはglibc iconv / ASCII範囲の標準入力のみ
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Locale category
A setting that controls language, numeric formatting, collation, or date display.
Character encoding
A mapping between characters and the bytes used to represent them.
Timezone
A rule set that converts timestamps for a region or display context.