Linux: figured out which file encoding was used for a file

for format in `iconv -l | grep / | cut -d / -f1` 

do

        iconv -f $format -t ASCII file >/dev/null 2>&1

        if [ $? = 0 ]

        then 

                echo "Found $format"

                iconv -f $format -t ASCII file

                exit

        fi

done