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.

macOSfilemimeencodingLow risk
Run file -I notes.txt — it prints something like notes.txt: text/plain; charset=iso-8859-1.

01Command

Command
$ file -I notes.txt

02What 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

RiskLow risk
!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 page
    manpagez.com · technical · 2026-09-17
    Evidence: 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 Type
    SS64 · technical · 2026-09-17
    Evidence: 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

06Related solutions