Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I think the ideal solution for me is a Nautilus Script that performs encoding conversion on selected files.

The following script reads the encoding of a selected file and performs utf8 conversion if it's not utf8, but I couldn't figure out how to make it work on multiple files:

CHARSET="$(file -bi "$1"|awk -F "=" '{print $2}')"
if [ "$CHARSET" != utf-8 ]; then
iconv -f "$CHARSET" -t utf8 "$1" -c -o "$1.utf8"
share|improve this question
up vote 5 down vote accepted

Let's say your script is named Here's how you would make it work across multiple files:

for filename in file1 file2 file3 ; do ./ "$filename" ; done

You could incorporate that for-loop in the script itself, like this:

for filename in "$@"; do
    CHARSET="$(file -bi "$filename"|awk -F "=" '{print $2}')"
    if [ "$CHARSET" != utf-8 ]; then
        iconv -f "$CHARSET" -t utf8 "$filename" -c -o "$filename.utf8"

You could then run the script with multiple filenames like this:

./ file1 file2 file3
share|improve this answer
Thank you very much for teaching me this for-loop thing, it's magic! The revised code in the middle does exactly what I want! – Sadi Feb 5 '13 at 13:01
But on second thought is it possible to keep file extension intact? – Sadi Feb 5 '13 at 13:40
Thanks, but I tried it already and it seems inconv loses the input file (replaced by output file) before the operation is completed on longish files; and I get only half the file in the end. – Sadi Feb 5 '13 at 15:06
@Sadi: you're right, here's how to fix that:… – Flimm Feb 5 '13 at 15:36
Thank you so much! I've decided to show the result as a separate answer so that it might be useful to others as well. – Sadi Feb 5 '13 at 16:03

Here's a nice and simple Nautilus Script that I've managed to write thanks to Flimm as seen in the answer above:

#Nautilus Script to determine encoding of selected file(s) and convert to utf8 if necessary
for filename in $@; do
    CHARSET="$(file -bi "$filename"|awk -F "=" '{print $2}')"
    if [ "$CHARSET" != utf-8 ]; then
        iconv -f "$CHARSET" -t utf8 "$filename" -c -o "$filename.utf8" &&
        mv -f "$filename.utf8" "$filename"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.