macOS: How to show a file’s character encoding

Mac/macOS FAQ: How do I show a file’s character encoding?

Solution

On macOS, you show a file’s character encoding using the -I option of the file command:

$ file -I file.txt
file.txt: text/plain; charset=utf-8

Here are a few more examples that show the file command’s output, with and without the -I option:

$ file 00-intro.adoc
00-intro.adoc: UTF-8 Unicode text, with very long lines

$ file -I 00-intro.adoc
00-intro.adoc: text/plain; charset=utf-8


$ file /etc/passwd
/etc/passwd: ASCII text

$ file -I /etc/passwd
/etc/passwd: text/plain; charset=us-ascii


$ file -I /Users/al
/users/al: inode/directory; charset=binary

In summary, if you wanted to learn how to see a the character encoding of a file on a Mac/macOS system, I hope these examples are helpful.