该命令用来识别文件类型,也可用来辨别一些文件的编码格式。它是通过查看文件的头部信息来获取文件类型,而不是像Windows通过扩展名来确定文件类型的。

执行权限 :All User

指令所在路径:/usr/bin/file

命令语法:

file [ -bchikLnNprsvz ] [ -f namefile ] [ -F separator ] [ -m magicfiles ] file ...

命令参数:

下表列出了部分常用的参数。

参数

长参数

描叙

-b

 

列出文件辨识结果时,不显示文件名称。

-c

 

详细显示指令执行过程,便于排错或分析程序执行的情形

-f

 

列出文件中文件名的文件类型

-F

 

使用指定分隔符号替换输出文件名后的默认的“:”分隔符。

-i

 

输出mime类型的字符串

-L

 

查看对应软链接对应文件的文件类型

-z

 

尝试去解读压缩文件的内容

 

--help

显示命令在线帮助

 

--version

显示命令版本信息

使用示例:

1:查看file命令的帮助信息

[root@DB-Server ~]# file --help
Usage: file [OPTION]... [FILE]...
Determine file type of FILEs.
 
  -m, --magic-file LIST      use LIST as a colon-separated list of magic
                               number files
  -z, --uncompress           try to look inside compressed files
  -b, --brief                do not prepend filenames to output lines
  -c, --checking-printout    print the parsed form of the magic file, use in
                               conjunction with -m to debug a new magic file
                               before installing it
  -f, --files-from FILE      read the filenames to be examined from FILE
  -F, --separator string     use string as separator instead of `:'
  -i, --mime                 output mime type strings
  -k, --keep-going           don't stop at the first match
  -L, --dereference          causes symlinks to be followed
  -n, --no-buffer            do not buffer output
  -N, --no-pad               do not pad output
  -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
      --help                 display this help and exit
      --version              output version information and exit

当然你也可以使用 man file 获取更加详细的帮助文档信息。

2:查看文件类型

例如,如下所示,Temp.txt 文件类型为text,编码为UTF-8 Unicode

[root@DB-Server ~]# file Temp.txt 
 
Temp.txt: UTF-8 Unicode text, with very long lines, with CRLF line terminators
 

3:不输出文件名称,只显示文件格式以及编码

通过下面两个命令对时,就可以清晰的了解参数-b的作用。

[root@DB-Server ~]# file Temp.txt 
Temp.txt: UTF-8 Unicode text, with very long lines, with CRLF line terminators
[root@DB-Server ~]# file -b Temp.txt
UTF-8 Unicode text, with very long lines, with CRLF line terminators

4: 输出mime类型的字符串

[root@DB-Server ~]# file -i Temp.txt 
Temp.txt: text/plain; charset=utf-8

5: 查看文件中的文件名的文件类型

这个参数非常适合shell脚本去查找、判别某种文件类型的数据。

[root@DB-Server ~]# cat >test
/root/install.log 
it is only one test file
 
[2]+  Stopped                 cat > test
[root@DB-Server ~]# file -f  test
/root/install.log:        ASCII text
it is only one test file: ERROR: cannot open `it is only one test file' (No such file or directory)
[root@DB-Server ~]# 
[root@DB-Server ~]# 

5: 使用指定分隔符号替换输出文件名后的默认的“:”分隔符。

感觉这个参数很鸡肋!我搞明白这个参数的作用时,很是纳闷。

6:尝试去解读压缩文件的内容

[root@DB-Server ~]# file -z Temp.txt.gz 
Temp.txt.gz: UTF-8 Unicode text, with very long lines, with CRLF line terminators (gzip compressed data, was "Temp.txt", from Unix, last modified: Tue Jun 24 00:34:15 2014)
[root@DB-Server ~]# 

7: 查看软链接对应文件的文件类型

如下所示,创建一个软链接sfile,然后分别用file 和带参数的file -L查看

 
[root@DB-Server ~]# ln -s Temp.txt.gz sfile
[root@DB-Server ~]# file sfile 
sfile: symbolic link to `Temp.txt.gz'
[root@DB-Server ~]# file -L sfile 
sfile: gzip compressed data, was "Temp.txt", from Unix, last modified: Tue Jun 24 00:34:15 2014
[root@DB-Server ~]# 

Linux命令之 file命令的更多相关文章

  1. linux file命令小记

    在linux中,所有东西都是文件:而且他并没有后缀名这一概念.Linux的扩展名没有太大的意义, file.tar.gz file.tgz file.tar.bz2 file.rar file.gz ...

  2. 我使用过的Linux命令之file - 检测并显示文件类型

    摘自:http://codingstandards.iteye.com/blog/804463 我使用过的Linux命令之file - 检测并显示文件类型 用途说明 file命令是用来检测并显示文件类 ...

  3. linux命令学习——file

    1.简介 file命令是用来检测并显示文件类型(determine file type).通过file指令,我们得以辨识该文件的类型,例如可以知道动态连接库是32位还是64位. 2.命令格式 file ...

  4. Linux:file命令显示自定义文件类型

    file 命令可以查看文件类型信息,原理见: 非常Linux-file命令与magic file 修改 /ect/magic 文件后,可用 file 命令显示自定义文件类型信息. man magic ...

  5. Linux -- file 命令

    1.file命令含义 file命令用于接受一个文件作为参数并执行某些测试,以确定正确的文件类型 2.file命令参数以及实例 (1)file命令查看文件类型 $ file /etc/irbrc /et ...

  6. 每天一个linux命令:file(11)

    file file命令用来探测给定文件的类型.file命令对文件的检查分为文件系统.魔法幻数检查和语言检查3个过程. 格式 file [选项] [参数] 参数选项 参数 备注 -b 列出辨识结果时,不 ...

  7. linux 命令之file

    Linux file命令用于辨识文件类型. 通过file指令,我们得以辨识该文件的类型. 语法 file [-bcLvz][-f <名称文件>][-m <魔法数字文件>...] ...

  8. Linux— file命令 用于辨识文件类型

    Linux file命令用于辨识文件类型. 通过file指令,我们得以辨识该文件的类型. 语法 file [-bcLvz][-f <名称文件>][-m <魔法数字文件>...] ...

  9. Linux命令学习总结: file命令

    命令简介: 该命令用来识别文件类型,也可用来辨别一些文件的编码格式.它是通过查看文件的头部信息来获取文件类型,而不是像Windows通过扩展名来确定文件类型的. 执行权限 :All User 指令所在 ...

随机推荐

  1. spring的普通类中如何取session和request对像

    在使用spring时,经常需要在普通类中获取session,request等对像. 比如一些AOP拦截器类,在有使用struts2时,因为struts2有一个接口使用org.apache.struts ...

  2. 2017南宁现场赛E The Champion

    Bob is attending a chess competition. Now the competition is in the knockout phase. There are 2^r2r  ...

  3. hashlib模块加密用法

    hashlib 加密模块 hashlib.md5() 构建一个md5的对象,用于调用对象的update方法去加密   例子: import hashlib hash = hashlib.md5() h ...

  4. 网络知识===cookie 、session、JSESSIONID的区别

    cookie .session ? 让我们用几个例子来描述一下cookie和session机制之间的区别与联系.笔者曾经常去的一家咖啡店有喝5杯咖啡免费赠一杯咖啡的优惠,然而一次性消费5杯咖啡的机会微 ...

  5. python基础===修改idle的输入风格

    http://blog.csdn.net/aq_cainiao_aq/article/details/51701861

  6. BZOJ 4522: [Cqoi2016]密钥破解

    http://www.lydsy.com/JudgeOnline/problem.php?id=4522 题目:给你RSA密钥的公钥和密文,求私钥和原文,其中\(N=pq\le 2^{62}\),p和 ...

  7. Android之Activity

    Activity总结: Activity的顶层View是DecorView,而我们在onCreate函数中通过setContentView设置的View只不过是这个DecorView的一部分罢了.De ...

  8. javascript中a标签把href属性设置为“javascript:void(0)”还是会打开空白页面的问题

    在项目中有个位置的点击a标签这里要加一个权限判断,但是之前使用的是js动态添加a标签,href的属性值是一个url,但是我要做权限判断之后,我的url就不能设置在href属性中了,这样的话我可以在a标 ...

  9. MATLAB求函数零点与极值

    1.      roots函数 针对多项式求零点(详见MATLAB多项式及多项式拟合) 2.      fzero函数 返回一元函数在某个区间内的的零点. x0 = fzero(@(x)x.^2-3* ...

  10. 在lua中正确使用uuid的方法:

    -- 参考:http://ju.outofmemory.cn/entry/97724local function guid()        local template ="xxxxxxx ...