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