Linux & OSX: Get file encoding

Sometime you need to know what’s your file encoding? Is it UTF-8, ISO 8859-1, ASCII or Windows 1252? You can find this out by using the file¬†Unix command.

Linux: file -i <filename>

Mac OSX: file -I <filename>

Example usage:

username@server ~$ file -i somefile.php
somefile.php: text/x-php; charset=us-ascii
username@server ~$ file -i myutf8file.txt
myutf8file.txt: text/plain; charset=utf-8
username@server ~$ file -i username.tar.bz2
username.tar.bz2: application/x-bzip2; charset=binary
username@server ~$

If you want only the encoding. You can do file -i filename.txt | sed "s/.*charset=\(.*\)/\1/" E.g.

username@server ~$ file -i myutf8file.txt | sed "s/.*charset=\(.*\)/\1/"

Kevin Kivi

I am a professional software developer. My software interests include PHP, Perl, React Native, Node.js and tinkering with anything Linux related. I also have front-end knowledge.

Among other things, on my spare time I do ultra-running, swimming, software developing for fun and watch Mr. Robot.