Skip to content

File encoding

check a file’s encoding

! https://www.shellhacks.com/linux-check-change-file-encoding { #_IN_ZOTERO }

file command

file -bi [filename]
opm , veel kritiek op dit commando , want het is niet 100 % :
utf-8 has no header. Pure ASCII (7-bit) is indistinguishable from utf-8
That is the point, a header will cause all sorts of problems).

if you have a file that is ASCII for the first MB , then has a single UTF-8 character,
then you will not know, unless you read the whole file.

The ‘file’ command reads only part of the file and uses magic numbers to take a best guess.
On occasion ‘file’ can and will give you the incorrect answer.

==iconv command==

accepted as a good solution : the ‘iconv’ command

iconv -f utf8 <filename> -t utf8 -o /dev/null

Replace {filename} with actual full filename
A return code of nothing = utf8 , error = not utf8
or :

if recode utf8/..UCS < "$FILE" >/dev/null 2>&1; then
    echo "Valid utf8 : $FILE"
else
    echo "NOT valid utf8: $FILE"
fi

isutf8 command

use  isutf8 command from the moreutils package.