file命令 – 识别文件类型

标签:,
来源: 老季博客
日期: 2022-12-16
作者: 腾讯云/服务器VPS推荐评测/Vultr
阅读数: 48

file命令的功能是用于识别文件的类型,也可以用来辨别一些内容的编码格式。由于Linux系统并不是像Windows系统那样通过扩展名来定义文件类型,因此用户无法直接通过文件名来进行分辨。file命令则是为了解决此问题,通过分析文件头部信息中的标识来显示文件类型,使用很方便。

语法格式:file [参数] 文件

常用参数:

-b 列出辨识结果时,不显示文件名称 (简要模式)
-c 详细显示指令执行过程
-f 指定名称文件,显示多个文件类型信息
-L 直接显示符号连接所指向的文件类别
-m 指定魔法数字文件
-v 显示版本信息
-z 尝试去解读压缩文件的内容
-i 显示MIME类别

参考实例:

查看某些文件的类型:

[root@jiloc ~]# file anaconda-ks.cfg  
anaconda-ks.cfg: ASCII text [root@jiloc ~]# file /dev/sda 
/dev/sda: block special (8/0) 

[root@jiloc ~]# file Documents 
Documents: directory 

[root@jiloc ~]# file /bin/ls 
/bin/ls: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=937708964f0f7e3673465d7749d6cf6a2601dea2, stripped, too many notes (256)

查看某个文件的类型,但不显示文件名:

[root@jiloc ~]# file -b anaconda-ks.cfg  
ASCII text

以MIME类别来显示某个文件的类型:

[root@jiloc ~]# file -i anaconda-ks.cfg  
anaconda-ks.cfg: text/plain; charset=us-ascii 

查看某个符号链接文件(快捷方式)的类型,会提示目标文件名称:

[root@jiloc ~]# file /dev/cdrom 
/dev/cdrom: symbolic link to sr0 

直接查看某个符号链接文件(快捷方式)所对应的目标文件的文件类型:

[root@jiloc ~]# file -L /dev/cdrom 
/dev/cdrom: block special (11/0) 
链接到文章: https://jiloc.com/48126.html

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注