Linux系统中file命令的使用详解
命令简介:
该命令用来识别文件类型,也可用来辨别一些文件的编码格式。它是通过查看文件的头部信息来获取文件类型,而不是像windows通过扩展名来确定文件类型的。
执行权限 :all user
指令所在路径:/usr/bin/file
命令语法:
file [ -bchiklnnprsvz ] [ -f namefile ] [ -f separator ] [ -m magicfiles ] file ...
命令参数:
下表列出了部分常用的参数。
使用示例:
1:查看file命令的帮助信息
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:不输出文件名称,只显示文件格式以及编码
通过下面两个命令对时,就可以清晰的了解参数-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.txtutf-8 unicode text, with very long lines, with crlf line terminators
3: 输出mime类型的字符串
[root@db-server ~]# file -i temp.txt temp.txt: text/plain; charset=utf-8
4: 查看文件中的文件名的文件类型
这个参数非常适合shell脚本去查找、判别某种文件类型的数据。
你可以像下面这样使用 file 命令确定文件的类型。下面的截图显示了用 file 命令确定不同文件类型的例子。
tecmint@tecmint ~/linux-tricks $ dir
backup master.zip
crossroads-stable.tar.gz num.txt
edward-maya-2011-2012-new-remix.mp3 reggea.xspf
linux-security-optimization-book.gif tmp-link
tecmint@tecmint ~/linux-tricks $ file backup/
backup/: directory
tecmint@tecmint ~/linux-tricks $ file master.zip
master.zip: zip archive data, at least v1.0 to extract
tecmint@tecmint ~/linux-tricks $ file crossroads-stable.tar.gz
crossroads-stable.tar.gz: gzip compressed data, from unix, last modified: tue apr 5 15:15:20 2011
tecmint@tecmint ~/linux-tricks $ file linux-security-optimization-book.gif
linux-security-optimization-book.gif: gif image data, version 89a, 200 x 259
tecmint@tecmint ~/linux-tricks $ file edward-maya-2011-2012-new-remix.mp3
edward-maya-2011-2012-new-remix.mp3: audio file with id3 version 2.3.0, contains: mpeg adts, layer iii, v1, 192 kbps, 44.1 khz, jntstereo
tecmint@tecmint ~/linux-tricks $ file /dev/sda1
/dev/sda1: block special
tecmint@tecmint ~/linux-tricks $ file /dev/tty1
/dev/tty1: character special
5:尝试去解读压缩文件的内容
[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 ~]#
6: 查看软链接对应文件的文件类型
如下所示,创建一个软链接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 ~]#