Detect a File's Type and Text Encoding with file -I
Identify what a mystery file really is, and for text files which character set it uses, in one line.
Run file -I notes.txt — it prints something like notes.txt: text/plain; charset=iso-8859-1.
01Command
Command
$ file -I notes.txt02What it does
file inspects content magic bytes rather than trusting the extension. The -I (mime) form adds the charset for text files, which is exactly what you need before running iconv to fix mojibake: convert from the charset file reports to utf-8. It also instantly exposes files that are actually zip archives or images with the wrong extension.
03Risk
!Warning: Encoding detection is heuristic — a short file of uniform text may be guessed imprecisely, so cross-check after converting.
04Undo
✓No undo needed — this command does not change persistent state.
05Sources
- file(1) man pageEvidence: The file man page documents -I / --mime-magic style output: file prints a MIME type string including the character set for text files.
- file - Determine File TypeEvidence: Documents the -I (--mime) option which prints MIME type and charset, e.g. text/plain; charset=utf-8, instead of the human-readable description.
Last verified: 2026-09-19 · Category: Files & Folders