The war of the character encodings for text files is over and UTF-8 has won. Occasionally, I need to check if a text file is UTF-8 or some other encoding like ASCII or UTF-16.
filecommand for this! It is not reliable and might detect a UTF-8 file as ASCII:
$ file foobar.json foobar.json: ASCII text
$ isutf8 foobar.json $ isutf8 /bin/ls /bin/ls: line 1, char 1, byte offset 25: invalid UTF-8 code
Notice how it is a quiet tool that returns exit code 0 if the file is UTF-8 and prints nothing. If the file is not UTF-8, then it prints some details.
$ sudo apt install moreutils