用 file 命令识别文件的真实类型

不看扩展名,直接读取文件头魔数判断真实格式,改名伪装一秒现形。

Linux文件魔数检查类型低风险
运行 file suspicious-download.dat 即可输出真实类型,例如 'PE32 executable' 或 'gzip compressed data';加 -b 输出更简洁,加 -i 显示 MIME 类型。

01命令

命令
$ file suspicious-download.dat

02作用说明

file 命令读取文件开头字节并与 /usr/share/misc/magic 魔数库比对,所以改名过的压缩包、伪装成图片的脚本、下载到一半的残缺文件都能立刻识别出来。对可执行文件它还会报告架构和链接方式,对文本文件报告字符集。检查来路不明的下载文件时,这是标准的第一步。

03风险

风险低风险
使用条件file package (preinstalled on most distros)
!注意: 知道文件类型不等于文件安全——被识别为可执行文件的照样可能是恶意程序,来历不明的二进制不要运行。

04撤销

✓无需撤销——该命令不会改变持久状态。

05来源

  • file(1) — determine file type
    Ubuntu manpages · community · 2026-09-17
    依据: Man page documents magic tests and options -b (brief), -i (mime), -L; confirms 'file', '-b' and 'magic' appear in the text.
  • file(1) — Debian man page
    Debian manpages · community · 2026-09-17
    依据: Man page header states 'file — determine file type' and describes MAGIC (the magic tests based on /usr/share/misc/magic); confirms MAGIC and file-identification behavior.

最后验证: 2026-09-19 · 分类: 文件与文件夹