首页·终端命令

file

文件与目录

用法

file [path]

靠内容而不是文件名判断文件类型;它无视扩展名,所以名为 .jpg 其实是 PDF 的文件会在这里露馅。

常用选项

选项含义
-bPrint only the description, without the file name
--mime-typePrint the MIME type; the short form is -i on GNU, -I on macOS
-LFollow symlinks
-zLook inside compressed files
-sRead block and character devices too
-iOn BSD and macOS this means do not classify contents, not MIME

示例

file screenshot

Says PNG image data even with no extension.

file -b --mime-type report.pdf

Prints application/pdf and nothing else.

file *

Types every entry in this directory.

移动、删除和查找。有些命令不会再问一遍,所以顺手加 -i 是在救自己。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. file 是做什么的?

靠内容而不是文件名判断文件类型;它无视扩展名,所以名为 .jpg 其实是 PDF 的文件会在这里露馅。

Q. 怎么写?

file [path] —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 6 个,完整列表在 man file。这条命令属于文件与目录。

相关命令

man 手册: man file