Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

There are many plain text files which were encoded in variant charsets.

I want to convert them all to UTF-8, but before running iconv, I need to know its original encoding. Most browsers have an Auto Detect option in encodings, however, I can't check those text files one by one because there are too many.

Only having known the original encoding, I then can convert the texts by iconv -f DETECTED_CHARSET -t utf-8.

Is there any utility to detect the encoding of plain text files? It DOES NOT have to be 100% perfect, I don't mind if there're 100 files misconverted in 1,000,000 files.

share|improve this question
up vote 27 down vote accepted

Try the chardet Python module.

Its original webpage is now dead but still available on

It's also available on PyPi so you can install it in your python environment using

pip install chardet
share|improve this answer
Yes, and it's already packaged as python-chardet in Ubuntu universe repo. – Xiè Jìléi Jun 25 '11 at 6:21
If it wasn't a perfect guess, chardet will still give the most correctly guess, like ./a.txt: GB2312 (confidence: 0.99). Compared to Enca which just failed and report 'Unrecognized encoding'. However, sadly enough, chardet runs very slow. – Xiè Jìléi Jun 25 '11 at 6:48
@谢继雷: Have it run overnight or something like that. Charset detection is a complicated process. You could also try the Java-based jChardet or ... the original chardet is part of Mozilla, but only C++ source is available, no command-line tool. – grawity Jun 25 '11 at 12:13
Exactly what I needed. Thank you. – Ian Hunter Apr 17 '12 at 22:36
Regarding speed: running chardet <(head -c4000 filename.txt) was much faster and equally successful for my use-case. (in case it's not clear this bash syntax will send only the first 4000 bytes to chardet) – ndemou Dec 26 '15 at 19:32

I would use this simple command:

encoding=$(file -bi myfile.txt)

Or if you want just the actual character set (like utf-8):

encoding=$(file -bi myfile.txt | sed -e 's/.*[ ]charset=//')
share|improve this answer
Unfortunately, file only detects encodings with specific properties, such as UTF-8 or UTF-16. The rest -- oldish ISO8859 or their MS-DOS and Windows correspondents -- are listed as "unknown-8bit" or something similar, even for files which chardet detects with 99% confidence. – grawity Oct 28 '11 at 19:09
file showed me iso-8859-1 – cweiske Mar 30 '12 at 7:22
What if the extension is lying? – james.garriss Oct 3 '14 at 13:24
@james.garriss: file extension has nothing to do with its (text) content encoding. – MestreLion Nov 28 '14 at 12:18

On Debian-based Linux, the uchardet package (Debian / Ubuntu) provides a command line tool. See below the package description:

 universal charset detection library - cli utility
 uchardet is a C language binding of the original C++ implementation
 of the universal charset detection library by Mozilla.
 uchardet is a encoding detector library, which takes a sequence of
 bytes in an unknown character encoding without any additional
 information, and attempts to determine the encoding of the text.
 The original code of universalchardet is available at
 Techniques used by universalchardet are described at
share|improve this answer
Thanks! From the project's homepage it wasn't obvious to me that there was a CLI included. It's also available on OS X when installing uchardet via Homebrew. – Stefan Schmidt Jul 6 '13 at 14:47
I was a little confused at first because a ISO 8859-1 document was falsely identified as Windows-1252 but in the printable range Windows-1252 is a superset of ISO 8859-1 so conversion with iconv works fine. – Stefan Schmidt Jul 6 '13 at 14:56

For Linux, there is enca and for Solaris you can use auto_ef.

share|improve this answer
Enca seems too strict for me: enca -d -L zh ./a.txt failed with message ./a.txt: Unrecognized encoding Failure reason: No clear winner. As @grawity mentioned, chardet is more lax, however it's yet too slow. – Xiè Jìléi Jun 25 '11 at 7:06
Enca completely fails the "actually does something" test. – Michael Wolf Mar 1 '12 at 18:59
uchardet failed (detected CP1252 instead of the actual CP1250), but enca worked fine. (single example, hard to generalize...) – Palo Nov 16 '15 at 20:52

Getting back to chardet (python 2.?) this call might be enough:

python -c 'import chardet,sys; print chardet.detect(' < file
{'confidence': 0.98999999999999999, 'encoding': 'utf-8'}

Though it's far from perfect....

echo "öasd" | iconv -t ISO-8859-1 | python -c 'import chardet,sys; print chardet.detect('
{'confidence': 0.5, 'encoding': 'windows-1252'}
share|improve this answer

UTFCast is worth a try. Didn't work for me (maybe because my files are terrible) but it looks good.

share|improve this answer

Mozilla has a nice codebase for auto-detection in web pages:

Detailed description of the algorithm:

share|improve this answer

I tried chardet but it incorrectly guessed ISO8859-2 on an ISO8859-1 test file.

The simplest solution for me was to open the file in vim and type :set fileencoding, which displays the auto-detected encoding. vim correctly detected my test file as latin1 (ISO8859-1).

share|improve this answer

isutf8 (from the moreutils package) did the job

share|improve this answer
How? This answer isn't really helpful. – Moses Oct 28 '15 at 19:02
It's not exactly was asked, but is a useful tool. If the file is valid UTF-8, the exit status is zero. If the file is not valid UTF-8, or there is some error, the exit status is non-zero. – ton Feb 16 at 17:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .