简介

file命令用来查看文件类型,也可用来辨别一些文件的编码格式。它是通过查看文件的头部信息来获取文件类型,而不是像Windows通过扩展名来确定文件类型的。

命令格式

file [-bcLvz] [-f <名称文件>] [-m <魔法数字文件>…] [文件或目录…]

常用参数

-b 列出辨识结果时,不显示文件名称 (简要模式)
-i 显示MIME类别
-L 直接显示符号连接所指向的文件类别
-F separator 使用指定的字符串作为文件名和文件结果之间的分隔符返回。默认为“:”。

MIME类别

text/plain 普通文本
text/html HTML文本
application/pdf PDF文档
application/msword Word文档
image/png PNG图片
image/jpeg JPEG图片
application/x-tar TAR文件
application/x-gzip GZIP文件

示例

  1. [root@localhost ~]# echo '我爱中国' > install.log
  2. [root@localhost ~]# file install.log
  3. install.log: UTF-8 Unicode text
  4. [root@localhost ~]# file -b install.log
  5. UTF-8 Unicode text
  6. [root@localhost ~]# file -i install.log
  7. install.log: text/plain; charset=utf-8
  8. [root@localhost ~]# file -i -b install.log
  9. text/plain; charset=utf-8
  10. [root@localhost ~]# mkdir a
  11. [root@localhost ~]# ln -s a b
  12. [root@localhost ~]# ls -l b
  13. lrwxrwxrwx. 1 root root 1 Apr 20 16:39 b -> a
  14. [root@localhost ~]# file b
  15. b: symbolic link to `a'
  16. [root@localhost ~]# file -L b
  17. b: directory
  18. [root@localhost ~]# file install.log
  19. install.log: UTF-8 Unicode text
  20. [root@localhost ~]# file -F ' => ' install.log
  21. install.log => UTF-8 Unicode text
  22. [root@localhost ~]# which --skip-alias ls
  23. /usr/bin/ls
  24. [root@localhost ~]# file /usr/bin/ls
  25. /usr/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=0x7dc964034aa8ec7327b3992e3239a0f50789a3dd, stripped