linux命令详解:file命令

前言

file命令可以获取多种文件类型,包括文本文件、脚本文件、源码文件、多媒体文件(音频视频)等。file是通过查看文件的头部内容,来获取文件的类型,而不像Window那样是通过扩展名来确定文件类型的。

命令用法

-z 获取压缩文件的类型(不能是用tar打包过的),比如gzip、zip等压缩过的

-L 获取软链所指向的文件的类型

-f 指定文件列表参数,获取该列表里面的所有文件的类型

常见用法

file FileName

 1: [root@master lianxi]# file test 
 2: test: ASCII text
 1: [root@master lianxi]# > 1
 2: [root@master lianxi]# file 1
 3: 1: empty

file使用通配符,获取多个文件的类型

file *.lua (以.lua结尾的文件)

 1: [root@master lianxi]# file *.lua
 2: 2.lua: ASCII text
 3: 3.lua: ASCII text
 4: 4.lua: ASCII text

数据文件类型(某些程序专用的数据格式)

 1: [root@master lianxi]# file /var/log/lastlog 
 2: /var/log/lastlog: data

Python脚本文件

 1: [root@master lianxi]# file print.py 
 2: print.py: a /bin/python  script text executable

-z 参数,可以获取用gzip、zip压缩过的文件的类型

 1: [root@master lianxi]# gzip print.py 
 2: [root@master lianxi]# ls print.py.gz 
 3: print.py.gz
 4: [root@master lianxi]# zip -r print.py.zip print.py.gz 
 5: adding: print.py.gz (stored 0%)
 6: [root@master lianxi]# ls print.py.zip 
 7: print.py.zip
 8: [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 2013
 10: print.py.zip: Zip archive data, at least v1.0 to extract

-L 获取软链指向的文件的类型。默认是返回软链本身类型

 1: [root@master lianxi]# ln -s test test.soft
 2: [root@master lianxi]# ls -l test*
 3: -rw-r--r-- 1 root root 25 Jun 14 20:09 test
 4: lrwxrwxrwx 1 root root  4 Jun 14 20:51 test.soft -> test
 5: [root@master lianxi]# file test.soft 
 6: test.soft: symbolic link to `test'
 7: [root@master lianxi]# file -L test.soft 
 8: test.soft: ASCII text
 9: [root@master lianxi]# 

-f 获取一个文件名列表的所有文件的类型。1)注意路径是否正确 2)每行一个文件名

 1: [root@master dir]# ls
 2: 1  2  3
 3: [root@master dir]# ls > a
 4: [root@master dir]# cat a
 5: 1
 6: 2
 7: 3
 8: a
 9: [root@master dir]# file -f a
 10: 1: ASCII text
 11: 2: ASCII text
 12: 3: empty
 13: a: ASCII text

特殊说明

1)在Window中,Windows系统对文件系统文件的标识是通过其扩展名。但是Windows上的程序自己也可以通过文件内容来判断文件内容类型

2)file 是通过读取文件头部内容,来获取文件类型,比如BASH脚本文件以#!/bin/bash 或Python脚本以#!/bin/python等,file读取其头部信息判断类型。

3)file可以辨识的文件类型很多,文本文件、脚本文件、数据文件、多媒体文件等。

总结

file命令,获取文件类型。

posted @ 2013-11-05 14:39  许杰的博客  阅读(865)  评论(0编辑  收藏  举报