用 file -I 检测文件类型和文本编码
一行命令弄清「神秘文件」的真实类型,文本文件还能直接显示字符集。
运行 file -I notes.txt,输出类似 notes.txt: text/plain; charset=iso-8859-1。
01命令
命令
$ file -I notes.txt02作用说明
file 通过内容魔数而非扩展名判断类型。-I(mime)形式会为文本文件附带 charset,这正是修复乱码前需要的信息:按报告的字符集用 iconv 转成 utf-8 即可。它还能立刻揭穿「改了扩展名的 zip/图片」这类文件。
03风险
!注意: 编码检测是启发式的——内容单一的短文件可能判断不准,转换后请再次核对。
04撤销
✓无需撤销——该命令不会改变持久状态。
05来源
- file(1) man page依据: 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依据: Documents the -I (--mime) option which prints MIME type and charset, e.g. text/plain; charset=utf-8, instead of the human-readable description.
最后验证: 2026-09-19 · 分类: 文件与文件夹