 <p>The tool "uchardet" does this well using character frequency distribution models for each charset. Larger files and more "typical" files have more confidence (obviously).</p> <p>On ubuntu, you just <code>apt-get install uchardet</code>. </p> <p>On other systems, get the source, usage &amp; docs here: <a href="https://github.com/BYVoid/uchardet" rel="nofollow">https://github.com/BYVoid/uchardet</a></p>