每天一命令之file

功能:file是通过查看文件的头部内容,来获取文件的类型。
使用file命令可以知道某个文件究竟是二进制(ELF格式)的可执行文件, 还是Shell Script文件,或者是其它的什么格式。
file能识别的文件类型:目录、Shell脚本、英文文本、二进制可执行文件、C语言源文件、文本文件、DOS的可执行文件。
在Linux系统中,文件类型根据文件的权限以及文件内容类型来划分的。在linux中文件本身是不需要后缀名称的,我们习惯上添加后缀名称仅仅是便于直观了解这是哪种用途类型。
语法:file [选项] [文件]
file命令检验文件类型按以下顺序来完成:
检验文件系统(Filesystem)中支持的文件类型。
检验magic file规则。
检验文件内容的语言和字符集。
检验文件系统(Filesystem)中支持的文件类型
文件系统支持的文件类型指的是通过ls -l 中第一个字符表示的文件类型:
-(regular):正规文件(包括文本文件(ASCII,会打印text),可执行文件(会打印excutable),其他二进制文件(会打印data))
d(directory):目录
l(link):软链接(不包括硬连接,硬链接会以正规文件显示
b(block buffered special):随机存储的设备文件,如硬盘,光盘等存储设备
c(character unbuffered special):持续输入的设备文件,如鼠标,键盘
s(socket):socket文件,最常在/var/run目录下看到这类文件
p(pipe):管道文件(first-in-first-out),它的目的在解决多个程序同时存取一个文件造成的错误问题
检验magic file规则
magic file指的是那些具有特殊文件格式的文件,如C文件,它会有#include字样;tar文件的前几个字节会有特殊的规则。而检验magic file规则就是根据这些特殊的格式去判断一个文件的类型。而这些规则是保存在$HOME/.magic.mgc,$HOME/.magic,/etc/magic.mgc,/etc/magic/usr/share/misc/magic.mgc,/usr/share/misc/magic中。
*/magic文件是文本文件,而*/magic.mgc文件则是由*/magic编译后的二进制文件。同一目录下若存在*/magic.mgc则使用该文件,否则使用*/magic。
这些配置的优先级为$HOME/.magic*>/etc/magic*>/usr/share/misc/magic*。
*/magic文件内容格式
文件中的每行都指定了一个规则测试去检验文件类型,这个规则由4个域指定:
offset:指定由文件起始的第几个byte开始检验。
type:要进行检验的数据类型,即由offset那个byte开始的那个数据类型是什么。具体有哪些数据类型,可以参才magic(5)。常用的数据类型有
byte:一个byte的值
short:两个byte的值
long:四个byte的值
string:字符串。
test:检验值。用于检验offset下的type是否是这个test值。使用C语言的数值或字符表示形式。
message:用于显示检验结果的信息显示
如果type为数值类型,那么其后面可添加&value,表示先与后面的test值进行‘与’操作,再进行比较。如果type为字符串类型,则其后可跟/[Bbc]*,/b表示忽略空格,/c表示忽略字母大小写。
如果test的值为数值类型,可以数值前添加=,<,>,&,^,~,分别表示相等、小于、大于、与操作、异或操作、取反操作。如果test的值为字符串类型,可以在其前添加=、<、>。
file命令返回结果以及含义(常见)
empty 空文件
directory 目录文件
English text 英文正式文件
assembler program text 汇编语言程序的正文文件
ascii text ASCII编码的文本文件
command text 命令语言编写的命令正文程序
c program C语言正文程序
relocation text 用于连接的目标文件
executable 可执行的目标代码文件
data 数据文件
短选项 长选项 涵义
-m –magic-file LIST 指定魔法数字名
-z –uncompress 探测压缩过的文件类型
-b –brief 列出辨识结果时,不显示文件名称
-c –checking-printout 详细显示指令执行过程,便于排错或分析程序执行的情形
-e –exclude TEST 对文件列表排除TEST类型。有效的测试:ascii, apptype, compress, elf, soft, tar, tokens, troff
-f –files-from FILE 指定文件列表参数,获取该列表里面的所有文件的类型
-F –separator STRING 使用字符串作为分隔符而不是“:”
-i –mime 显示MIME类别
–apple 显示Apple CREATOR/TYPE
–mime-type 显示MIME类别
–mime-encoding 显示MIME编码
-k –keep-going 执行命令错误时不终止
-l –list list magic strength
-L –dereference 跟随符号链接(默认)
-h –no-dereference 不跟随符号链接
-n –no-buffer 没有缓冲输出
-N –no-pad do not pad output
-0 –print0 terminate filenames with ASCII NUL
-p –preserve-date preserve access times on files
-r –raw don’t translate unprintable chars to ooo
-s –special-files treat special (block/char devices) files as ordinary ones
-C –compile compile file specified by -m
-d –debug print debugging messages
file 使用实例
显示文件类型
[root@master lianxi]# file test
test: ASCII text
[root@master lianxi]# > 1 使用>输出方法创建新文件1
[root@master lianxi]# file 1
1: empty
[root@web ~]# file install.log
install.log: UTF-8 Unicode text
[root@web ~]# file -b install.log 不显示文件名称
UTF-8 Unicode text
file使用通配符,获取多个文件的类型
[root@master lianxi]# file *.lua
2.lua: ASCII text
3.lua: ASCII text
4.lua: ASCII text
-i参数,显示MIME类型
[root@web ~]# file -i install.log 显示MIME类别
install.log: text/plain; charset=utf-8
[root@web ~]# file -b -i install.log 不显示文件名称
text/plain; charset=utf-8
数据文件类型(某些程序专用的数据格式)
[root@master lianxi]# file /var/log/lastlog
/var/log/lastlog: data
Python脚本文件
[root@master lianxi]# file print.py
print.py: a /bin/python script text executable
-z 参数,可以获取用gzip、zip压缩过的文件的类型
[root@master lianxi]# gzip print.py
[root@master lianxi]# ls print.py.gz
print.py.gz
[root@master lianxi]# zip -r print.py.zip print.py.gz
adding: print.py.gz (stored 0%)
[root@master lianxi]# ls print.py.zip
print.py.zip
[root@master lianxi]# file print.py.*
print.py.gz: gzip compressed data, was “print.py”, from Unix, last modified: Fri Jun 14 20:48:14 2013
print.py.zip: Zip archive data, at least v1.0 to extract
-L 参数,获取软链指向的文件的类型。默认是返回软链本身类型
[root@master lianxi]# ln -s test test.soft
[root@master lianxi]# ls -l test*
-rw-r–r– 1 root root 25 Jun 14 20:09 test
lrwxrwxrwx 1 root root 4 Jun 14 20:51 test.soft -> test
[root@master lianxi]# file test.soft
test.soft: symbolic link to ‘test’
[root@master lianxi]# file -L test.soft
test.soft: ASCII text
[root@rhel55 ~]# ls -l /var/mail
lrwxrwxrwx 1 root root 10 08-13 00:11 /var/mail -> spool/mail
[root@rhel55 ~]# file /var/mail
/var/mail: symbolic link to ‘spool/mail’
[root@rhel55 ~]# file -L /var/mail
/var/mail: directory
[root@rhel55 ~]# file /var/spool/mail
/var/spool/mail: directory
[root@rhel55 ~]# file -L /var/spool/mail
/var/spool/mail: directory
-f 参数,获取一个文件名列表的所有文件的类型
[root@master dir]# ls
1 2 3
[root@master dir]# ls > a
[root@master dir]# cat a
1
2
3
a
[root@master dir]# file -f a
1: ASCII text
2: ASCII text
3: empty
a: ASCII text
file 列出全部文件类型
[root@web186 root]# file *
1.sh: ASCII text
1.txt: empty
20080308xzmf.txt: ASCII text, with CRLF line terminators
448FD15874B0DC51.wav: RIFF (little-endian) data, WAVE audio, ITU G.711 a-law, mono 8000 Hz
449110BA327B23C6.vox: 8086 relocatable (Microsoft)
600d.wav: RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, stereo 44100 Hz
anaconda-ks.cfg: ASCII English text
banner.swf: data
commons-beanutils.jar: Zip archive data, at least v1.0 to extract
cvs_accept.sh: Bourne shell script text executable
data: directory
employer0605.txt: Non-ISO extended-ASCII text
GetFirstFile.class: compiled Java class data, version 49.0
GetFirstFile.java: ASCII Java program text
HELPFILE: C++ program text
httptunnel.zip: Zip archive data, at least v2.0 to extract
iptables_man.txt: ASCII English text, with overstriking
lnx61su_171.tar.gz: gzip compressed data, from Unix
megamgr.bin.filepart: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.0.0, statically linked, stripped
MegaPR_Linux_A02.tar.gz: gzip compressed data, was “MegaPR_Linux_A02.tar”, from Win/32
mess0429.txt: ISO-8859 text, with very long lines
smartmontools-5.36.tar.gz: gzip compressed data, from Unix, max compression
sms.qunfa: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared libs), not stripped
sms.vip.cfg: ISO-8859 text, with very long lines
top.txt: ASCII text, with CRLF, LF line terminators
voice20061128.rar: RAR archive data
vox2amr.sh: Bourne-Again shell script text executable
[root@web186 root]#
from http://www.lampweb.org/linux/2/20.html