file命令可以获取多种文件类型,包括文本文件、脚本文件、源码文件、多媒体文件(音频视频)等。file是通过查看文件的头部内容,来获取文件的类型,而不像Window那样是通过扩展名来确定文件类型的。
命令用法
-z 获取压缩文件的类型(不能是用tar打包过的),比如gzip、zip等压缩过的
-L 获取软链所指向的文件的类型
-f 指定文件列表参数,获取该列表里面的所有文件的类型
常见用法
file FileName
# file test2: test: ASCII text# > 1# file 13: 1: empty
file使用通配符,获取多个文件的类型
file *.lua (以.lua结尾的文件)
# file *.lua2: 2.lua: ASCII text3: 3.lua: ASCII text4: 4.lua: ASCII text
数据文件类型(某些程序专用的数据格式)
# file /var/log/lastlogvar/log/lastlog: dataPython脚本文件
# file print.pyprint.py: a /bin/python script text executable
-z 参数,可以获取用gzip、zip压缩过的文件的类型
# gzip print.py# ls print.py.gzprint.py.gz# zip -r print.py.zip print.py.gzprint.py.gz (stored 0%)# ls print.py.zipprint.py.zip# file print.py.*, from Unix, last modified: Fri Jun 14 20:48:14 2013print.py.zip: Zip archive data, at least v1.0 to extract
-L 获取软链指向的文件的类型。默认是返回软链本身类型
# ln -s test test.soft# ls -l test*3: -rw-r--r-- 1 root root 25 Jun 14 20:09 test4: lrwxrwxrwx 1 root root 4 Jun 14 20:51 test.soft -> test# file test.soft6: test.soft: symbolic link to `test'# file -L test.soft8: test.soft: ASCII text9: [root@master lianxi]#
-f 获取一个文件名列表的所有文件的类型。1)注意路径是否正确 2)每行一个文件名
# ls2: 1 2 3# ls > a# cat a5: 16: 27: 38: a# file -f a10: 1: ASCII text11: 2: ASCII textempty13: a: ASCII text
特殊说明
1)在Window中,Windows系统对文件系统文件的标识是通过其扩展名。但是Windows上的程序自己也可以通过文件内容来判断文件内容类型
2)file 是通过读取文件头部内容,来获取文件类型,比如BASH脚本文件以#!/bin/bash 或Python脚本以#!/bin/python等,file读取其头部信息判断类型。
3)file可以辨识的文件类型很多,文本文件、脚本文件、数据文件、多媒体文件等。
总结
file命令,获取文件类型。